I am creating a home windows application with C#. I am while using color dialog box for that user to choose one. Let me store that color inside a database, and have the ability to retrieve it afterwards, and have the ability to use that color within the interface.
What approach can you suggest in my experience?
Keep hex color inside a database area
nvarchar(7) ... the input could be
#ffffff for example.
varchar(7) works equally well, and occupy less space inside your DB.
You might do
varchar(6) if you are super worried about db size and append the # symbol in code.
EDIT: if you want to convert it to some control color, you should use
Dim MyColor as string = '[retrieve from database] MyControlColor = System.Drawing.ColorTranslator.FromHtml(MyColor)
Now, this really is untested but you could attempt the next function to transform the machine.Sketching.Colour To some Hex code.
Private Function GetHexColor(colorObj as System.Drawing.Color) as String return "#" & Hex(colorObj.R) & Hex(colorObj.G) & Hex(colorObj.B) End function
I personally use the functions System.Drawing.Color.FromArgb() and System.Drawing.Color.ToArgb() to transform the colour to and from integer, and save it as being int around the database
i discovered a method to obtain the hex code of the selected color with ColorDialog
ColorDialog col = new ColorDialog(); col.ShowDialog(); string color = col.Color.ToArgb().ToString("x"); color = color.Substring(2, 6); color = "#" + color; MessageBox.Show(color); button1.BackColor = System.Drawing.ColorTranslator.FromHtml(color);