-
[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
My thoughts are to just use FileSystemObject (FSO) to store the backcolor in a text file.
Then have it open the file on Form_Load and read the backcolor and pass it to the forum.
-
Re: please some help with saving and loading changes
Quote:
Originally Posted by Capp
My thoughts are to just use FileSystemObject (FSO) to store the backcolor in a text file.
Then have it open the file on Form_Load and read the backcolor and pass it to the forum.
But how do i use it? :/
-
Re: please some help with saving and loading changes
Something like this to save it:
VB Code:
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()
WriteTextFile(app.path & "\backcolor.txt", form1.backcolor)
End Sub
-
Re: please some help with saving and loading changes
To read the same file:
VB Code:
Private Sub Form_Load()
'Declare variables.
Dim fso As New FileSystemObject
Dim ts As TextStream
'Open file.
Set ts = fso.OpenTextFile(app.path & "\backcolor.txt")
form1.backcolor = ts.ReadLine
'Close the file.
ts.Close
End Sub
-
Re: please some help with saving and loading changes
Or, without the FSO
vb Code:
Private Sub Form_Load()
'check to see if the file exists
'it wont the first time, which would cause
'an error
If Dir$("d:\form1backcolor.txt") <> vbNullString Then
Open "d:\form1backcolor.txt" For Input As #1
lngBkClr = Input(LOF(1), 1)
Close #1
Form1.BackColor = lngBkClr
End If
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Open "d:\form1backcolor.txt" For Output As #1
Print #1, Form1.BackColor
Close #1
End Sub
-
Re: please some help with saving and loading changes
thanx guys for replying so quick but i still have a problem !!
Private Sub SaveBackColor()
WriteTextFile(app.path & "C:\backcolor.txt", form1.backcolor)
End Sub
I put for example that but it says it finds a bug at form1.backcolor)
What do i do now?
-
Re: please some help with saving and loading changes
Quote:
Originally Posted by schinis
I put for example that but it says it finds a bug at form1.backcolor)
:confused: What does this mean?
-
Re: please some help with saving and loading changes
1. app.path & "C:\backcolor.txt"
you already have the path "c:\"
2. i would convert the form1.backcolor to a string
-
Re: please some help with saving and loading changes
ok now, it really started getting on my nerves....
Look, I put 1 cmd button named savebackcolor, and my form is named form1
What do i do?? :/
Im notreally a proffesional guy... sry :/
-
Re: please some help with saving and loading changes
you double click you button (make sure it goes to the click event), and put your code in it.
which in this case would be
SaveBackColor
-
Re: please some help with saving and loading changes
yes i know i double click on it :P... Not so dumb yet :P
i mean that on the 2nd part theres a problem :/
-
Re: please some help with saving and loading changes
Quote:
Originally Posted by schinis
yes i know i double click on it :P... Not so dumb yet :P
i mean that on the 2nd part theres a problem :/
Can you tell us what the problem is? Where it is at? and what it says?
-
Re: please some help with saving and loading changes
are you saying you named ur command button "savebackcolor" or put the code in it?
-
Re: please some help with saving and loading changes
@ Capp it says "compile error: expected: ="
@Billy corner, I didnt really get the question...
-
Re: please some help with saving and loading changes
Quote:
Originally Posted by Billy Conner
are you saying you named ur command button "savebackcolor" or put the code in it?
I think he named his command button "savebackcolor"
If this is the case, I suggest you rename it using proper name convention. It will make coding so much easier for you
rename it cmdSaveBackColor to make life easy. :)
-
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.
-
Re: please some help with saving and loading changes
Quote:
Originally Posted by schinis
@ Capp it says "compile error: expected: ="
@Billy corner, I didnt really get the question...
Compile error? Are you hitting the "Run" button at the top or are you trying to compile it to .exe?
It might just be more useful if you could post the code you do have for us to look at.
-
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
since its a function, try
Call WriteTextFile(app.path & "C:\backcolor.txt", form1.backcolor)
[EDIT]
In the code you posted in post #7
-
Re: please some help with saving and loading changes
In case you missed it because of recent posts... post #17, no need to maintain file.
-
Re: please some help with saving and loading changes
also...
vb Code:
Private Sub SaveBackColor()
savebackcolor(app.path & "\backcolor.txt", form1.backcolor)
End Sub
its invalid
its
vb Code:
Private Sub SaveBackColor()
call WriteTextFile(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 !
-
Re: please some help with saving and loading changes
In the code you posted above, you declared a function inside a Click Event. This will cause you a problem every time.
You need to pull the WriteTextFile function out of the click event and put it by itself.
Then you would call this function on the cmdSaveBackColor_Click Event.
Do this:
vb Code:
'This is the code for the command button
Private Sub cmdSaveBackColor_Click()
SaveBackColor
End Sub
'This is the code for the sub to call every time the button is clicked
Private Sub SaveBackColor()
WriteTextFile(app.path & "\backcolor.txt", form1.backcolor)
End Sub
'This is the code that actually writes the text file information
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
-
Re: please some help with saving and loading changes
vb Code:
Option Explicit
Private Sub cmdSaveBackColor_Click()
SaveBackColor
'or
'Call WriteTextFile(App.Path & "backcolor.txt", Form1.BackColor)
'and omit the SaveBackColor sub
End Sub
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 & "backcolor.txt", Form1.BackColor)
End Sub
[edit] sorry the path was wrong as you had earlier
-
Re: please some help with saving and loading changes
@ leinad31 if i save it to the registry then how am i supposed to run it on form load?
-
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
Quote:
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.
The form starts with the last selected color.
-
Re: please some help with saving and loading changes
what the heck is a commondialog? :/
-
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.
Quote:
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.
-
Re: please some help with saving and loading changes
man, im really stupid !!! I dont understand anything !!!
-
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.
-
1 Attachment(s)
Re: please some help with saving and loading changes
Here, so you won't have to build the sample
-
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
Then update values passed to SaveSetting() accordingly.
In sample Me.BackColor was passed as the value of the registry key.
In your existing code what is assigned to sText, or what is the value written to the file?
-
Re: please some help with saving and loading changes
-
Re: please some help with saving and loading changes
Whats the value in the string? Show the line where your assigning it a value.
-
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... :D