getting a color out of a string

jnash

Well-known member
Joined
Oct 20, 2006
Messages
111
Programming Experience
Beginner
ive saved a form to a database and its backcolour as a string im now tryin to recreate the form and get back the color, now as its a string i need to convert it to a color that vb recognizes e.g i have a string

"color [white]" and i want to convert it into a color (system.drawing.color)
so i can attach it to the forms .backcolor property

thanks in advance, i hope i have made sense!
 
Instead of saving the string from Color.Tostring method you save the known name string:
VB.NET:
Dim c As Color = Color.FromName("Red")
MsgBox(c.ToKnownColor.ToString) 'outputs "Red"
This requires that the color was a known color, like what you posted "Color [Red]"
 
this dynamic and depends on what colour the user has clicked isnt there another way?

this is how it saves at the moment

VB.NET:
        While i < xx
            bco = forms.Item(i).backcolor().ToString
            fon = forms.Item(i).name().ToString
            order = i + 1
 
Yes, ColorTranslator class will let you store and retrieve the color value as an Integer (ToWin32/FromWin32 methods) or the same hex String used by webpages (ToHtml/FromHtml methods)
 
i was playing around with that, i just cant get it all to work

the datatype in the database of the field is a string


i have "bco" which is of value of the backcolour so its a colour

save it to the db , ("which gets a error field is not valid")

and then try to open it with the objectreader , either with .getvalue or .getstring, but there are always errors here also


and then save it to "bk" which is a colour

any segments of code you can send this way, this little think is taking me ages!

thanks
 
Did you look at the ColorTranslator class? It's ToHtml function returns a string. If the color is a known color such as Chocolate or BurlyWood, ToHtml returns that name; if not, ToHtml will return a hex string like "#C04000". You can save that to the database. Then to retrieve: after you have read it from the database, use ColorTranslator.FromHtml to convert it to a color.
 
Back
Top