Backcolor saving

Hi,

Using VB.net 2002 version.

How do I save backcolor changes?

Result required- When the program is restarted the backcolor is the modified color, not the original?

Cheers
Paulvb

Comments

  • seancampbellseancampbell Pennsylvania, USA
    Assuming you already know how to edit your background color. All you need to do is write 2 functions:

    SaveBGColor and RestoreBGColor

    These functions will write a .txt file that has the color's rgb value stored in it so you can restore it. The code I am attaching I wrote to restore the background of the Form from what it was last time the program was openned. It stores the ARGB value of the color into a txt file (filename set with a constant) and upon openning it checks for that file and loads from it.

    Note: I call SaveBGColor on the MyBase.Closing event and I call the RestoreBGColor on the MyBase.Load event

    [Code]

    Const SettingsFile As String = "BackColor.txt"

    Sub RestoreBGColor()

    'Aquire the full pathname for the application
    'This allows us to keep the settings file in the
    'same directory as the application
    'This may not be needed, but it's a habit from VB 6.0
    Dim appPath As String = Application.StartupPath

    'This appends a on the path if it doesn't have one on it
    If appPath.Substring(appPath.Length - 1, 1) <> "" Then appPath &= ""

    'Check if SettingsFile exists
    If IO.File.Exists(appPath & SettingsFile) Then

    'Set F to the next available open file number
    Dim F As Integer = FreeFile()

    'Open the file for Input mode (from a file into program)
    FileOpen(F, appPath & SettingsFile, OpenMode.Input)

    'Check if the file is empty, if it is, close the connection and exit the sub
    If EOF(F) Then
    FileClose(F)
    Exit Sub
    End If

    'Create an integer object to load the ARGB value into
    Dim I As Integer

    'Best to put file manipulation stuff into a try catch and have it
    'tell you the error it caught (msgbox ex.message)
    Try
    'This takes the integer value from the file into variable I
    Input(F, I)
    Catch ex As Exception
    MsgBox(ex.Message)
    End Try


    'Set the background color to .FromARGB(I)
    Me.BackColor.FromArgb(I)

    'Close the file connection
    FileClose(F)

    End If

    End Sub


    Sub SaveBGColor()

    Dim appPath As String = Application.StartupPath
    If appPath.Substring(appPath.Length - 1, 1) <> "" Then appPath &= ""

    Dim F As Integer = FreeFile()

    'Open the file for output mode (storing a value into a flat file)
    FileOpen(F, appPath & SettingsFile, OpenMode.Output)

    'Best to put file manipulation stuff into a try catch and have it
    'tell you the error it caught (msgbox ex.message)
    Try
    'Print, PrintLine, Write, and WriteLine all output to a file
    Print(F, Me.BackColor.ToArgb)
    Catch ex As Exception
    MsgBox(ex.Message)
    End Try


    'Close the file connection
    FileClose(F)

    End Sub

    [/code]
  • Hi,

    My code goes like this:-
    Private sub Button_Click-----
    if ddlg.ShowDialog()=DialogResult.OK then
    m_Color = cdlg.color
    Button1.BackColor = ddlg.color

    [b]I need to save new backcolor?[/b][u][/u]


    End If

  • seancampbellseancampbell Pennsylvania, USA
    Ok... Well this code should still work for you. Put these functions into your form code and add the routines to your load and closing functions...

    Change the line of code Me.BackColor to whatever object you want to store the color from. Button1.BackColor or whatever. When you close the program, it will store the current color of your Button and when you open it back up it will reload that color.
  • seancampbellseancampbell Pennsylvania, USA
    : Ok... Well this code should still work for you. Put these functions
    : into your form code and add the routines to your load and closing
    : functions...
    :
    : Change the line of code Me.BackColor to whatever object you want to
    : store the color from. Button1.BackColor or whatever. When you close
    : the program, it will store the current color of your Button and when
    : you open it back up it will reload that color.
    :
    If you having problems getting it to work, keep in mind that you have to close the program with the [X] button not the Stop button in VS.
  • The sample above does not work for me. can any one help.

    I have a dialog box which changes the back ground color of a button. I need to either modifie the properties box for this button, or save the color to a file?

    I can change the color to a string, and store to a text file. But can change back to a color?

    Regards
    Paulvb
  • seancampbellseancampbell Pennsylvania, USA
    I was astonished that my code wasn't working, but after reading over it over and over and over I finally realized what was going on. Attached is the code I have on a Form (Called Form15) with a button on it (Called Button1)

    [Code]
    Const SettingsFile As String = "BackColor.txt"

    Private Sub Form15_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Me.Show()

    RestoreBGColor()

    End Sub

    Sub RestoreBGColor()

    'Aquire the full pathname for the application
    'This allows us to keep the settings file in the
    'same directory as the application
    'This may not be needed, but it's a habit from VB 6.0
    Dim appPath As String = Application.StartupPath

    'This appends a on the path if it doesn't have one on it
    If appPath.Substring(appPath.Length - 1, 1) <> "" Then appPath &= ""

    'Check if SettingsFile exists
    If IO.File.Exists(appPath & SettingsFile) Then

    'Set F to the next available open file number
    Dim F As Integer = FreeFile()

    'Open the file for Input mode (from a file into program)
    FileOpen(F, appPath & SettingsFile, OpenMode.Input)

    'Check if the file is empty, if it is, close the connection and exit the sub
    If EOF(F) Then
    FileClose(F)
    Exit Sub
    End If

    'Create an integer object to load the ARGB value into
    Dim Str As String

    'Best to put file manipulation stuff into a try catch and have it
    'tell you the error it caught (msgbox ex.message)
    Try
    'This takes the integer value from the file into variable I
    Input(F, Str)
    Catch ex As Exception
    MsgBox(ex.Message)
    End Try

    'Set the background color to .FromARGB(I)
    Button1.BackColor = Color.FromArgb(CInt(Trim(Str)))

    'Close the file connection
    FileClose(F)

    End If

    End Sub

    Private Sub Form15_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing

    SaveBGColor()

    End Sub

    Sub SaveBGColor()

    Dim appPath As String = Application.StartupPath
    If appPath.Substring(appPath.Length - 1, 1) <> "" Then appPath &= ""

    Dim F As Integer = FreeFile()

    'Open the file for output mode (storing a value into a flat file)
    FileOpen(F, appPath & SettingsFile, OpenMode.Output)

    'Best to put file manipulation stuff into a try catch and have it
    'tell you the error it caught (msgbox ex.message)
    Try
    'Print, PrintLine, Write, and WriteLine all output to a file
    Print(F, Button1.BackColor.ToArgb)
    Catch ex As Exception
    MsgBox(ex.Message)
    End Try


    'Close the file connection
    FileClose(F)

    End Sub
    [/code]

    That should work. Only thing that got changed (besides some variable names and types) was the line Button1.BackColor = Color.FromArgb(Integer) where I had mistakenly put Button1.BackColor.FromArgb(Integer) instead.
  • Hi,

    Thankyou it works well.

    Maybe you can help me with another question, I can work with RS232 inputs,but have found no information working with Digital Input/Output cards?

    Best Regards
    Paulvb
  • seancampbellseancampbell Pennsylvania, USA
    Yeah I don't know about that, submit another forum topic with hardware information and I will look into it when I get back from vacation monday.

    -Sean
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories