I am trying to write an application that allow the user to select prefered colour from displayed colour dialog, but when the colour selected and the application is closed and reopened it doesn't open with the selected colour. I tried to write the code in the form load event as well but it gives an error message that no value is selected. The code below is the code i used in changing the form background and other components on the form which work successfully but making permanent is a problem. I will appreciate it if some out there can give me a clue on how to retain the selected colour. Thank you (I am working on visual studio 2003)

dlgcolor.SolidColorOnly = True
If dlgcolor.ShowDialog = Windows.Forms.DialogResult.OK Then
myform2.BackColor = dlgcolor.Color
GroupBox1.BackColor = dlgcolor.Color
dtp1.CalendarMonthBackground = dlgcolor.Color
dtp1.CalendarTitleBackColor = dlgcolor.Color
txtevent.BackColor = dlgcolor.Color
txtdate.BackColor = dlgcolor.Color
txttime.BackColor = dlgcolor.Color
txtvenue.BackColor = dlgcolor.Color
End If

Member Avatar for iamthwee

I am trying to write an application that allow the user to select prefered colour from displayed colour dialog, but when the colour selected and the application is closed and reopened it doesn't open with the selected colour

It's pretty simple, write their last choice to a text file, and then when they open up the program again read in that last value.

It's pretty simple, write their last choice to a text file, and then when they open up the program again read in that last value.

There was a project where i had to use themes for my application. What i did was create a class file, and in that class file i have written a code where the colors that i change are being saved in the registry. So i used to call the class where ever its need by declaring an object in particular form. And then when changes are made they wil be saved in the registry.

Il give you a bit of a sample code

#Region "Template1 Settings"   'The template1 = theme1
    Public Sub settemp1BackColor(ByVal pstrBackColor As String)
        Dim myRegKey As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.LocalMachine
        Try
            myRegKey = myRegKey.CreateSubKey("SOFTWARE\DrawMachineSettings\Template1") 'myRegKey.OpenSubKey("SOFTWARE\DrawMachineSettings", True)
            If Not IsNothing(myRegKey) Then
                myRegKey.SetValue("BackColor", pstrBackColor) 'pstrbackcolor is a value which is been used in a particular form
            Else
                myRegKey = myRegKey.CreateSubKey("SOFTWARE\DrawMachineSettings\Template1")
                myRegKey.SetValue("BackColor", pstrBackColor)
            End If
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        Finally
            myRegKey = Nothing
        End Try
    End Sub

 Public Function gettemp1BackColor() As String ' Method to get the back color from the registry
        Dim myRegKey As RegistryKey = Registry.LocalMachine
        Dim lstrReturn As String
        Try
            myRegKey = myRegKey.OpenSubKey("SOFTWARE\DrawMachineSettings\Template1", True)
            If Not IsNothing(myRegKey) Then
                lstrReturn = Trim(myRegKey.GetValue("BackColor").ToString())
                If (Trim(lstrReturn) <> "") Then
                    Return lstrReturn
                Else
                    Return Nothing
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
            Return False
        Finally
            myRegKey = Nothing
        End Try
    End Function

This is for the back color of that particular form.This is been saved in the registry. This code should be put in a class file.

this code is for the form

Public mobjConfiginfo As New (class file name)

Use the

ColorTranslator.FromHtml(txtBackColor.Text) to get the color 
mobjConfiginfo.settemp1BackColor(txtBackColor.Text)

to get it saved in the registry

And when you want to load the particular color.. call it in the getevent from the class file

i know this is quite confusing.. but if you are using for a on a large color change scale.. this is an easy method..

commented: Daft idea, there is no need to mess with the registry? -2
commented: What you wrote was hypocritic. -3

In VB.NET you can use the Resource and Settings manager to save things to my.settings and my.resources

Yes that's another way to do it. But my solution is a permanent one.

That is permanent. The VB.NET me.settings is stored in the users Documents&Settings\Application Data folder

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.