0

I am trying to store the Color value from ColorDialog into Registry. I am using below code in my utility. However, I notice that when I rerun the utility the Color that get assigned to the Button1 (value read from Registry on Form_Load) is different than what I had stored in Registry in first place.

The Main application for which I have developed this utility has in-built similar feature to save/recall color settings from Registry. I checked that the code in my utility which returns the color from Registry on form load is fine and the color match to what I set through the Main Application.

Could someone please check below code and let me know what's wrong?

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim CLR = ColorDialog1.Color.GetHashCode.ToString 

    If Me.ColorDialog1.ShowDialog = DialogResult.OK Then 
        Dim regKey As RegistryKey
        regKey = Registry.CurrentUser.OpenSubKey("Software\MyApp\Settings\Tags", True)
        regKey.SetValue("DefaultColor", CLR, RegistryValueKind.DWord)
        regKey.Close()

        Button1.BackColor = Me.ColorDialog1.Color
    End If
End Sub

Code used to recall color from Registry:

If My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\MyApp\Settings\Tags", "DefaultColor", Nothing) Is Nothing Then
    MsgBox("Value does not exist.")
    'creates the DWORD value if not found
    My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\MyApp\Settings\Tags", "DefaultColor", 0, RegistryValueKind.DWord)
Else
    Dim HEX = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\MyApp\Settings\Tags", "DefaultColor", Nothing)
    Dim myColor As Color = System.Drawing.ColorTranslator.FromWin32(HEX.ToString)
    TagLeaderCLRButton.BackColor = myColor
End If
2
  • An obvious mistake is that the "CLR" value is obtained before you display the dialog. Writing the wrong value to the registry, the old one. Move Dim CLR inside the If statement. And use Color.ToArgb() Aug 27, 2014 at 11:21
  • Thanks for the suggestion ..I have changed in the code ...
    – DK2014
    Aug 27, 2014 at 14:09

1 Answer 1

2

Hash code doesn't represent color. Why GetHashCode() matters?

Dim CLR = ColorDialog1.Color.GetHashCode.ToString

Instead, you should use:

Dim clr = ColorDialog1.Color.ToARGB()

And if possible, use My.settings to store Color instead of registry. You can directly store color by creating a setting in project properties. :)

Edit:
Integer.Parse(HEX.ToString) is somewhat better than just HEX.ToString() right? :)
I don't see anything wrong. If this is your real code, you can change it to something like this if necessary. (just an idea):

Dim path = "HKEY_CURRENT_USER\Software\MyApp\Settings\Tags"
Dim defColor = My.Computer.Registry.GetValue(path, "DefaultColor", Nothing)

If defColor Is Nothing Then
        MsgBox("Value does not exist.")
        'creates the DWORD value if not found
        My.Computer.Registry.SetValue(path, "DefaultColor", &HFF00BFFF, RegistryValueKind.DWord)
        defColor = &HFF00BFFF
End If
Button1.BackColor = Color.FromArgb(Integer.Parse(defColor.ToString))

With this, button1 back color is automatically set to my fav color from the first click. he he.

1
  • Thanks a lot for this. Could you kindly have a look at the other code I am using to retrieve the stored color value on form load which gets assigned to button backcolor... I will add this code in main post..
    – DK2014
    Aug 27, 2014 at 14:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.