Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to keep Color in DB

Status
Not open for further replies.

Andrzejek

Programmer
Jan 10, 2006
8,502
US

I have this small table in ORACLE where I want to keep colors:
[tt]
COLOR_ID COLOR_DESC

0........White
1........Blue
2........Green
3........Salmon[/tt]

I know I can use:[tt]
System.Drawing.ColorTranslator.FromWin32(&HFFFFFF&)[/tt]
and &HFFFFFF& gives me White color (&HFF0000& gives me Blue, etc.)

I want to keep colors somehow in my data base to avoid Select Case statements or a bunch of IF statements in my code.

Two questions:
1. Is that the best way to approach colors?
2. How can I keep Colors in data base? What format should the column be? String? Integer?

Have fun.

---- Andy
 
What about if you add a string field named ColourValue in your table that will keep the value of each colour as &HFFFFFF& for example???
I disagree with integer type... you can convert the type of the field if you need it in your code. You don't have to remove COLOR_DESC field because it makes you clear which value of colour corresponds to the proper colour. The what you will do is to retrieve the value of the field you want each time from your database and you will achieve it via a sql query for example select ColourValue from colours where Colour_Desc = "Blue" you will store the result of the query to a variable. You can use a combo box with the colours you want blue, green, white, etc and if you can compare the value of combobox with the result of query.

Private Sub combo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles combo.SelectedIndexChanged
dim colour, tempColour as string
colour = combo.Items.Item(combo.SelectedIndex))
' here you will put your query and your code to open your recordset etc... I give you the query.
tempColour = 'select ColourValue from Colours where Colour_Desc=' & colour
System.Drawing.ColorTranslator.FromWin32(tempColour)
end sub
so you will get the colour you want each time without use if, or select case statement. Hope it helps. Have fun [dazed]
 
You can get all the known colors to a combobox, listbox or an array

Code:
Imports System.Drawing

Code:
    Sub LoadColorsInAListBox(ByVal lstColor1 As ListBox)
        For kColor = KnownColor.AliceBlue To KnownColor.YellowGreen
            lstColor1.Items.Add(kColor)
        Next
    End Sub

Zameer Abdulla

 
Antzelinaki,

Your code:[tt]
System.Drawing.ColorTranslator.FromWin32(tempColour)[/tt]
gives me an error: Conversion from string "&H00FFFFFF&" to type 'Integer' is not valid.


Have fun.

---- Andy
 

If Integer it wants, the Integer it gets: :)

I have found the equivalents as Integers:

Code:
ID      COLOR_DESC   COLOR_VALUE       COLOR_INT
0       White        &H00FFFFFF&       16777215
1       Yellow       &H0000FFFF&          65535
2       Green        &H0000FF00&          65280
3       Salmon       &H000080FF&          33023

And it works :)

I just may drop COLOR_VALUE column...

Have fun.

---- Andy
 
you can use the val function to convert string to integer too!!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top