[RESOLVED] Problems loading .backcolor using color codes
I've some pictureboxes where I select the color from a colordialog and then I save the color in a settings.ini file.
When I load the program, the instruction Form1_Shown loads the colors from the settings.ini file.
When I save the colors to the ini file I transform then from System.Drawing.Color to a String (hex).
The problem when loading the colors is that some colors are loaded correctly and others no. The other pictureboxes controls don't change the color.
System.Drawing.Color to Hex
Code:
Private Function ColorConversion(colorObj As System.Drawing.Color) As String
Return "#" & Hex(colorObj.R) & Hex(colorObj.G) & Hex(colorObj.B)
End Function
Hex to System.Drawing.Color (for loading the colors in the picboxes)
Code:
PictureBox1.BackColor = System.Drawing.ColorTranslator.FromHtml("#FFFFFF")
in the ini file I've seen that some color codes in hex are less than 6 characters long:
#FF00, #0FF0
Re: Problems loading .backcolor using color codes
Don't use Hex. You're using VB.NET so use VB.NET:
Code:
Private Function ColorConversion(colorObj As System.Drawing.Color) As String
Return String.Format("#{0:X2}{1:X2}{2:X2}", colorObj.R, colorObj.G, colorObj.B)
End Function
Re: Problems loading .backcolor using color codes
Why bother with hex? Color has methods for converting to and from integers.
Example
Code:
Dim foo As Color = Color.MistyRose
'convert to string for saving
Dim fooAsString As String = foo.ToArgb.ToString
'convert string back to color from stored integer
foo = Color.FromArgb(Integer.Parse(fooAsString))
Re: Problems loading .backcolor using color codes