Reputation: 181
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
Upvotes: 0
Views: 1142
Reputation: 1191
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.
Upvotes: 2