Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

storing and retrieving colors with database, C# windows forms application

I'm making a windows application with C#. I'm using the color dialog box for the user to select a color. I'd like to store that color in a database, and be able to retrieve it later on, and be able to use that color in the user interface.

What approach would you suggest to me?

like image 320
jello Avatar asked Feb 16 '10 20:02

jello


2 Answers

The best way will be to store the hex color in a database field nvarchar(7) ... the input would be #ffffff as an example. varchar(6) would work just as well, and take up less space in your DB. Just be sure to append the # in your code.

Since you need to convert it to/from a control color, you can use System.Drawing.ColorTranslator.FromHtml(someHexColor)

// Hex to Control Color
var myColor = "#[color from database]";
var myControlColor = System.Drawing.ColorTranslator.FromHtml(myColor);

// Control Color to Hex
var colorBlue = System.Drawing.Color.Blue;
var hexBlue = System.Drawing.ColorTranslator.ToHtml(colorBlue);
like image 152
Chase Florell Avatar answered Sep 17 '22 19:09

Chase Florell


I use the functions System.Drawing.Color.FromArgb() and System.Drawing.Color.ToArgb() to convert the color from and to integer, and save it as int on the database

like image 42
Jhonny D. Cano -Leftware- Avatar answered Sep 19 '22 19:09

Jhonny D. Cano -Leftware-