[RESOLVED] please some help with saving and loading changes
hey, i just came up with a bright new idea of how to change the backcolor of a form but i want the program when i close it to save custom changes on a text file, and then load it on startup of the program... Can someone help?? This is really important !!!
Re: please some help with saving and loading changes
You can use the registry instead since its just one property
Code:
Option Explicit
'sample has common dialog control and command button on form
Private Sub Command1_Click() 'change form backcolor
CommonDialog1.ShowColor
Me.BackColor = CommonDialog1.Color
End Sub
Private Sub Form_Load() 'retrieve setting, use default value if reg key doesn't exist
Me.BackColor = CLng(GetSetting("AppName", "Section", "BackColor", &H8000000F))
End Sub
Private Sub Form_Unload(Cancel As Integer) 'save setting for subsequent GetSetting()
SaveSetting "AppName", "Section", "BackColor", Me.BackColor
End Sub
Of course you'll have to use more meaningful labels and values than the ones in the sample above.
Last edited by leinad31; Feb 27th, 2007 at 03:20 PM.
Re: please some help with saving and loading changes
i hit run....
wait ill show you exactly what i write....
Private Sub cmdSaveBackColor_Click()
Private Function WriteTextFile(fName As String, _
sText As String) As Boolean
Dim FSO As New FileSystemObject
Dim FSTR As Scripting.TextStream
On Error Resume Next
Set FSTR = FSO.OpenTextFile(fName, ForWriting, _
Not FSO.FileExists(fName))
FSTR.Write sText
WriteTextFile = True
FSTR.Close
If Err.Number Then WriteTextFile = False
On Error GoTo 0
Set FSTR = Nothing
Set FSO = Nothing
End Function
Private Sub SaveBackColor()
savebackcolor(app.path & "\backcolor.txt", form1.backcolor)
End Sub
Re: please some help with saving and loading changes
ok so now i did it:
Private Sub cmdSaveBackColor_Click()
Private Function WriteTextFile(fName As String, _
sText As String) As Boolean
Dim FSO As New FileSystemObject
Dim FSTR As Scripting.TextStream
On Error Resume Next
Set FSTR = FSO.OpenTextFile(fName, ForWriting, _
Not FSO.FileExists(fName))
FSTR.Write sText
WriteTextFile = True
FSTR.Close
If Err.Number Then WriteTextFile = False
On Error GoTo 0
Set FSTR = Nothing
Set FSO = Nothing
End Function
Private Sub SaveBackColor()
Call WriteTextFile(app.path & "C:\backcolor.txt", form1.backcolor)
End Sub
it accpets it when i hit run, but when i press the button it End sub expected !
Last edited by schinis; Feb 27th, 2007 at 03:29 PM.
Re: please some help with saving and loading changes
You can also use the "Write/Get PrivateProfileStruct" APIs. Although the file isn't readable (unless you're fluent in hex), it is a text file. Post 12 of Re: Writing Colors to ini for later use
Re: please some help with saving and loading changes
Originally Posted by schinis
@ leinad31 if i save it to the registry then how am i supposed to run it on form load?
The sample loads the last saved backcolor on form load.
Try it, make a project with a common dialog control and a button and copy the code I posted (update the labels and registry parameters accordingly with more meaningful/appropriate values). Change the form's backcolor by clicking the button which shows the color selection dialog. btw there's additional coding needed to handle user selecting cancel. After pressing ok the form changes color, exit the application and run the project again.
Re: please some help with saving and loading changes
Right click on an empty area on the toolbox, select components in the popup.
Under controls tab look for "microsoft common dialog control" and check it, it now appears on the toolbox.
Create an instance on the form, it will be named "CommonDialog1" by default.
Originally Posted by MSDN
Remarks
The CommonDialog control provides an interface between Visual Basic and the routines in the Microsoft Windows dynamic-link library Commdlg.dll. To create a dialog box using this control, Commdlg.dll must be in your Microsoft Windows SYSTEM directory.
You use the CommonDialog control in your application by adding it to a form and setting its properties. The dialog displayed by the control is determined by the methods of the control. Atrun time, a dialog box is displayed or the help engine is executed, when the appropriate method is invoked; atdesign time, the CommonDialog control is displayed as an icon on a form. This icon can't be sized.
The CommonDialog control can display the following dialogs using the specified method.
Method -- Dialog Displayed
ShowOpen -- Show Open Dialog Box
ShowSave -- Show Save As Dialog Box
ShowColor -- Show Color Dialog Box
ShowFont -- Show Font Dialog Box
ShowPrinter -- Show Print or Print Options Dialog Box
ShowHelp -- Invokes the Windows Help Engine
I used the commondialog.showcolor method for purposes of demonstration... if you already have your own means of selecting and setting the form's background color you can continue using it.
Last edited by leinad31; Feb 27th, 2007 at 04:06 PM.
Re: please some help with saving and loading changes
I used the commondialog.showcolor method for purposes of demonstration... if you already have your own means of selecting and setting the form's background color you can continue using it.
Re: please some help with saving and loading changes
i managed to find out what you were telling me leinard, thanx but, i only want it save the colour that they chose but the way i made it for them to choose..
Re: please some help with saving and loading changes
the whole idea is that iv got 1 txt box and 1 cmd... when i write e.g "green" in the txtbox and press the cmd, it changes the backcolor of the form to green..
now all i want is to make it save the last color i chose, and load it when i open the prog...
Re: please some help with saving and loading changes
At the moment I see three ways to implement it depending on how you coded everything...
1) you could save the value "green" to the registry and on form load get "green" from the registry and pass it to the existing procedure procedure that translates the string "green" into a value that can be assigned to form.backcolor...
2) or if your using enumerations you can save the numerical equivalent of green in the enumeration and again pass this to the appropriate procedure that converts the enumeration value to a value that can be assigned to form.backcolor
3) lastly, skip the translations/conversions from "green" and store the color value assigned to form.background like what I did with the demo. Another way to look at it would be limiting the conversion to user inputs/display (eg. "green"), after user input all internal processing would deal with the actual color value... it would be translated back to "green" only when you need to display such to the user.
Re: please some help with saving and loading changes
man leinard thanx a lot :P.. I did it and works fine...
What i did was simple !! I added another Cmd button, named it cmd1 and converted this:
Private Sub Form_Unload(Cancel As Integer) 'save setting for subsequent GetSetting()
SaveSetting "AppName", "Section", "BackColor", Me.BackColor
End Sub
into this:
Private Sub cmd1_click() 'save setting for subsequent GetSetting()
SaveSetting "AppName", "Section", "BackColor", Me.BackColor
End Sub
Now when i press that button, the user can save the colour he chose before...