views:

543

answers:

3

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?

+2  A: 

Store the hex color in a database field nvarchar(7) ... the input would be #ffffff as an example.

EDIT: varchar(7) would work just as well, and take up less space in your DB.

You could also do varchar(6) if you're super concerned about db size and append the # symbol in code.

EDIT: if you need to convert it to a control color, you can use System.Drawing.ColorTranslator.FromHtml("")

Dim MyColor as string = '[retrieve from database]
MyControlColor = System.Drawing.ColorTranslator.FromHtml(MyColor)

Now, this is untested but you could try the following function to convert the System.Drawing.Color TO a Hex code.

Private Function GetHexColor(colorObj as System.Drawing.Color) as String
    return "#" & Hex(colorObj.R) & Hex(colorObj.G) & Hex(colorObj.B)
End function
rockinthesixstring
+1 - although I believe a `varchar` would suffice since you're not worried about unicode for the RGB values.
CAbbott
@CAbbot, that's true... force of habit for me since I set everything up for localization... my bad.
rockinthesixstring
but then how do I retrieve those values and set them to control colors?
jello
@jello - See Jhonny's answer
Richard Szalay
The problem is converting the Color to #FFFFFF format and back easily. Color does not have a Static method like FromArgb that takes a hex string.
Nick
@jello... I updated my answer
rockinthesixstring
@rockinthesixstring I figured there was a static method out there somewhere...
Nick
cool thx it works rock
jello
@Nick. Yup, I like this solution simply because of the readability factor. The other answer is good too, but I'd personally hate looking at a DB table with a big INT as a color :S
rockinthesixstring
@jello, cool, glad it works... be sure to mark the answer ;)
rockinthesixstring
+1 for `nvarchar`! Why mess around with converting the perfectly good UTF-16 that C#, the .Net Framework, and Windows are using back-and-forth to some 1980s-era 8-bit codepage chosen at random when this particular database instance was installed?
Jeffrey L Whitledge
one more thing, I'm using the ColorDialog to get the color from the user. how do I change the result to a hexadecimal value?
jello
@Jeffrey, besides the fact that if you're worried about THIS TYPE of data filling up a db, then you must be dealing with a HUGE number of records. SQL Server Express will bottom out on it's record number limit before it bottoms out on data size on nvarchar(7). And SQL Server Standard doesn't have these arbitrary limits, and Hard Drive space is CHEAP CHEAP CHEAP!!!
rockinthesixstring
@jello... I updated my answer again.
rockinthesixstring
@rock: seems like C# has no Hex function
jello
did you try an import the system.drawing or the system.drawing.color namespace? I don't have Visual Studio in front of me to test this... sorry.
rockinthesixstring
system.drawing is there. but I don't think that has anything to do with Hex right? anyway, i found a way to get the hex code. i'll post it in my answer
jello
ok, sounds good. Sorry I'm not much of a C# guy.
rockinthesixstring
thx anyway, you helped for half of my solution.
jello
+1  A: 

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

Jhonny D. Cano -Leftware-
This too is a good method, however, I suppose I prefer to be able to look at the db and understand the content.
rockinthesixstring
I agree... is kind of a trade-off disk space vs readability
Jhonny D. Cano -Leftware-
A: 

i found a way to get the hex code of a 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);
jello