VB.NET

Moderators: seancampbell
Number of threads: 4020
Number of posts: 10026

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Backcolor saving Posted by paulvb on 10 Aug 2007 at 3:15 AM
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
Report
Re: Backcolor saving Posted by seancampbell on 10 Aug 2007 at 5:28 AM
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


    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

Report
Re: Backcolor saving Posted by paulvb on 10 Aug 2007 at 6:50 AM
Hi,

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

I need to save new backcolor?


End If

Report
Re: Backcolor saving Posted by seancampbell on 10 Aug 2007 at 7:34 AM
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.
Report
Re: Backcolor saving Posted by seancampbell on 10 Aug 2007 at 7:35 AM
: 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.
Report
Re: Backcolor saving Posted by paulvb on 10 Aug 2007 at 12:02 PM
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
Report
Re: Backcolor saving Posted by seancampbell on 10 Aug 2007 at 12:58 PM
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)

    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


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.
Report
Re: Backcolor saving Posted by paulvb on 10 Aug 2007 at 1:16 PM
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
Report
Re: Backcolor saving Posted by seancampbell on 10 Aug 2007 at 1:26 PM
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



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.