Results 1 to 40 of 40

Thread: [RESOLVED] please some help with saving and loading changes

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2006
    Location
    Cyprus, Limassol
    Posts
    49

    Resolved [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 !!!

  2. #2
    Hyperactive Member Capp's Avatar
    Join Date
    May 2005
    Location
    Texas
    Posts
    409

    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.
    AmazingAntivirus.com
    Remote Data Backups


    Please Mark your Thread "Resolved", if the query is solved...

    If a post has helped you then Please Rate it!

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2006
    Location
    Cyprus, Limassol
    Posts
    49

    Talking 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? :/

  4. #4
    Hyperactive Member Capp's Avatar
    Join Date
    May 2005
    Location
    Texas
    Posts
    409

    Re: please some help with saving and loading changes

    Something like this to save it:

    VB Code:
    1. Private Function WriteTextFile(fName As String, _
    2.   sText As String) As Boolean
    3.  
    4.   Dim FSO As New FileSystemObject
    5.   Dim FSTR As Scripting.TextStream
    6.   On Error Resume Next
    7.   Set FSTR = FSO.OpenTextFile(fName, ForWriting, _
    8.     Not FSO.FileExists(fName))
    9.   FSTR.Write sText
    10.   WriteTextFile = True
    11.   FSTR.Close
    12.   If Err.Number Then WriteTextFile = False
    13.   On Error GoTo 0
    14.   Set FSTR = Nothing
    15.   Set FSO = Nothing
    16. End Function
    17.  
    18.  
    19. Private Sub SaveBackColor()
    20.   WriteTextFile(app.path & "\backcolor.txt", form1.backcolor)
    21. End Sub
    AmazingAntivirus.com
    Remote Data Backups


    Please Mark your Thread "Resolved", if the query is solved...

    If a post has helped you then Please Rate it!

  5. #5
    Hyperactive Member Capp's Avatar
    Join Date
    May 2005
    Location
    Texas
    Posts
    409

    Re: please some help with saving and loading changes

    To read the same file:

    VB Code:
    1. Private Sub Form_Load()
    2.         'Declare variables.
    3.         Dim fso As New FileSystemObject
    4.         Dim ts As TextStream
    5.         'Open file.
    6.         Set ts = fso.OpenTextFile(app.path & "\backcolor.txt")
    7.           form1.backcolor =  ts.ReadLine
    8.         'Close the file.
    9.         ts.Close
    10.       End Sub
    AmazingAntivirus.com
    Remote Data Backups


    Please Mark your Thread "Resolved", if the query is solved...

    If a post has helped you then Please Rate it!

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: please some help with saving and loading changes

    Or, without the FSO
    vb Code:
    1. Private Sub Form_Load()
    2. 'check to see if the file exists
    3. 'it wont the first time, which would cause
    4. 'an error
    5. If Dir$("d:\form1backcolor.txt") <> vbNullString Then
    6.    Open "d:\form1backcolor.txt" For Input As #1
    7.     lngBkClr = Input(LOF(1), 1)
    8.    Close #1
    9.    Form1.BackColor = lngBkClr
    10. End If
    11. End Sub
    12.  
    13. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    14. Open "d:\form1backcolor.txt" For Output As #1
    15.    Print #1, Form1.BackColor
    16. Close #1
    17. End Sub

  7. #7

    Thread Starter
    Member
    Join Date
    Jul 2006
    Location
    Cyprus, Limassol
    Posts
    49

    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?

  8. #8
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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)
    What does this mean?

  9. #9
    Hyperactive Member
    Join Date
    Aug 2006
    Location
    TeXaS
    Posts
    497

    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

  10. #10

    Thread Starter
    Member
    Join Date
    Jul 2006
    Location
    Cyprus, Limassol
    Posts
    49

    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 :/

  11. #11
    Hyperactive Member
    Join Date
    Aug 2006
    Location
    TeXaS
    Posts
    497

    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

  12. #12

    Thread Starter
    Member
    Join Date
    Jul 2006
    Location
    Cyprus, Limassol
    Posts
    49

    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 :/

  13. #13
    Hyperactive Member Capp's Avatar
    Join Date
    May 2005
    Location
    Texas
    Posts
    409

    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?
    AmazingAntivirus.com
    Remote Data Backups


    Please Mark your Thread "Resolved", if the query is solved...

    If a post has helped you then Please Rate it!

  14. #14
    Hyperactive Member
    Join Date
    Aug 2006
    Location
    TeXaS
    Posts
    497

    Re: please some help with saving and loading changes

    are you saying you named ur command button "savebackcolor" or put the code in it?

  15. #15

    Thread Starter
    Member
    Join Date
    Jul 2006
    Location
    Cyprus, Limassol
    Posts
    49

    Re: please some help with saving and loading changes

    @ Capp it says "compile error: expected: ="
    @Billy corner, I didnt really get the question...

  16. #16
    Hyperactive Member Capp's Avatar
    Join Date
    May 2005
    Location
    Texas
    Posts
    409

    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.
    AmazingAntivirus.com
    Remote Data Backups


    Please Mark your Thread "Resolved", if the query is solved...

    If a post has helped you then Please Rate it!

  17. #17
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    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.

  18. #18
    Hyperactive Member Capp's Avatar
    Join Date
    May 2005
    Location
    Texas
    Posts
    409

    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.
    AmazingAntivirus.com
    Remote Data Backups


    Please Mark your Thread "Resolved", if the query is solved...

    If a post has helped you then Please Rate it!

  19. #19

    Thread Starter
    Member
    Join Date
    Jul 2006
    Location
    Cyprus, Limassol
    Posts
    49

    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

  20. #20
    Hyperactive Member
    Join Date
    Aug 2006
    Location
    TeXaS
    Posts
    497

    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

  21. #21
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    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.

  22. #22
    Hyperactive Member
    Join Date
    Aug 2006
    Location
    TeXaS
    Posts
    497

    Re: please some help with saving and loading changes

    also...
    vb Code:
    1. Private Sub SaveBackColor()
    2. savebackcolor(app.path & "\backcolor.txt", form1.backcolor)
    3. End Sub
    its invalid
    its
    vb Code:
    1. Private Sub SaveBackColor()
    2. call WriteTextFile(app.path & "\backcolor.txt", form1.backcolor)
    3. End Sub

  23. #23

    Thread Starter
    Member
    Join Date
    Jul 2006
    Location
    Cyprus, Limassol
    Posts
    49

    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.

  24. #24
    Hyperactive Member Capp's Avatar
    Join Date
    May 2005
    Location
    Texas
    Posts
    409

    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:
    1. 'This is the code for the command button
    2. Private Sub cmdSaveBackColor_Click()
    3. SaveBackColor
    4. End Sub
    5.  
    6. 'This is the code for the sub to call every time the button is clicked
    7. Private Sub SaveBackColor()
    8. WriteTextFile(app.path & "\backcolor.txt", form1.backcolor)
    9. End Sub
    10.  
    11. 'This is the code that actually writes the text file information
    12. Private Function WriteTextFile(fName As String, _
    13. sText As String) As Boolean
    14.  
    15. Dim FSO As New FileSystemObject
    16. Dim FSTR As Scripting.TextStream
    17. On Error Resume Next
    18. Set FSTR = FSO.OpenTextFile(fName, ForWriting, _
    19. Not FSO.FileExists(fName))
    20. FSTR.Write sText
    21. WriteTextFile = True
    22. FSTR.Close
    23. If Err.Number Then WriteTextFile = False
    24. On Error GoTo 0
    25. Set FSTR = Nothing
    26. Set FSO = Nothing
    27.  
    28. End Function
    AmazingAntivirus.com
    Remote Data Backups


    Please Mark your Thread "Resolved", if the query is solved...

    If a post has helped you then Please Rate it!

  25. #25
    Hyperactive Member
    Join Date
    Aug 2006
    Location
    TeXaS
    Posts
    497

    Re: please some help with saving and loading changes

    vb Code:
    1. Option Explicit
    2.  
    3. Private Sub cmdSaveBackColor_Click()
    4.     SaveBackColor
    5.     'or
    6.     'Call WriteTextFile(App.Path & "backcolor.txt", Form1.BackColor)
    7.     'and omit the SaveBackColor sub
    8. End Sub
    9. Private Function WriteTextFile(fName As String, _
    10. sText As String) As Boolean
    11.  
    12.     Dim FSO As New FileSystemObject
    13.     Dim FSTR As Scripting.TextStream
    14.     On Error Resume Next
    15.     Set FSTR = FSO.OpenTextFile(fName, ForWriting, _
    16.     Not FSO.FileExists(fName))
    17.     FSTR.Write sText
    18.     WriteTextFile = True
    19.     FSTR.Close
    20.     If Err.Number Then WriteTextFile = False
    21.     On Error GoTo 0
    22.     Set FSTR = Nothing
    23.     Set FSO = Nothing
    24. End Function
    25.  
    26. Private Sub SaveBackColor()
    27.     Call WriteTextFile(App.Path & "backcolor.txt", Form1.BackColor)
    28. End Sub

    [edit] sorry the path was wrong as you had earlier
    Last edited by Billy Conner; Feb 27th, 2007 at 03:35 PM.

  26. #26

    Thread Starter
    Member
    Join Date
    Jul 2006
    Location
    Cyprus, Limassol
    Posts
    49

    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?

  27. #27
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    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

  28. #28
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    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.

  29. #29

    Thread Starter
    Member
    Join Date
    Jul 2006
    Location
    Cyprus, Limassol
    Posts
    49

    Re: please some help with saving and loading changes

    what the heck is a commondialog? :/

  30. #30
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    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.
    Last edited by leinad31; Feb 27th, 2007 at 04:06 PM.

  31. #31

    Thread Starter
    Member
    Join Date
    Jul 2006
    Location
    Cyprus, Limassol
    Posts
    49

    Re: please some help with saving and loading changes

    man, im really stupid !!! I dont understand anything !!!

  32. #32
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    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.

  33. #33
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: please some help with saving and loading changes

    Here, so you won't have to build the sample
    Attached Files Attached Files

  34. #34

    Thread Starter
    Member
    Join Date
    Jul 2006
    Location
    Cyprus, Limassol
    Posts
    49

    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..

  35. #35
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    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?
    Last edited by leinad31; Feb 27th, 2007 at 04:30 PM.

  36. #36

    Thread Starter
    Member
    Join Date
    Jul 2006
    Location
    Cyprus, Limassol
    Posts
    49

    Re: please some help with saving and loading changes

    its string

  37. #37
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: please some help with saving and loading changes

    Whats the value in the string? Show the line where your assigning it a value.

  38. #38

    Thread Starter
    Member
    Join Date
    Jul 2006
    Location
    Cyprus, Limassol
    Posts
    49

    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...

  39. #39
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    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.

  40. #40

    Thread Starter
    Member
    Join Date
    Jul 2006
    Location
    Cyprus, Limassol
    Posts
    49

    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...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width