Is it Possible to Read a Color Value from a Data Source and Use it toSet the Color of a Button?

  • Thread starter Thread starter Patrick A
  • Start date Start date
P

Patrick A

All,

I have a form with 20 buttons on it, and I would like to give the user
the ability to change the color of any individual button and have it
"saved" so that the next time they use the app, the button is the
color they set it to. At present, the user would make the change by
selecting a color value on a "data entry" form.

I know how to set the color of a button by hard coding it:
NewButton.BackColor = Color.AliceBlue

I know how to get the color value (in hex, rgb or name, e.g.
AliceBlue) from my DB or INI file.

But for the life of me, I can not figure out how to get VB to use it
to set the color.

With my color coming in as tp!butColor, I have tried using the
variations of the hex, rgb and name, and variations of;

NewButton.BackColor = tp!butColor
NewButton.BackColor = ColorTranslator.FromOle(tp!butColor)
NewButton.BackColor = System.Drawing.ColorConverter(tp!butcolor(0))

But I get one "cast is not valid" or "conversion" error after another.

Can anyone point me in the right direction? Which value should I
read, and how should I set it and cast it?

Thanks,

Patrick
 
try something like:

NewButton.BackColor = (Color) Enum.Parse (typeof (Color), "AliceBlue")

this is C# syntax, but should easily transalate to VB.
the "AliceBlue" string in quote can be a variable containing the string
representation from the enum name.

/LM
 
Luc,

Thanks for the reply. No luck though - but I should admit I have no
idea how to translate the C# to VB.


NewButton.BackColor = (Color) Enum.Parse (typeof (Color), tp!
butColor)

Gives me errors on Color, Enum and Parse...I'll keep hunting.

Patrick
 
Thanks, Mike

No matter what I do - whether I feed it RGB, hex, or color name, it
always winds up in the "catch" section (though I don't see anything in
the Debug window...which is odd.).

Still struggling...

Patrick


try
    NewButton.BackColor = CType([enum].Parse(GetType(Color), tp!butColor),
Color)
catch
    Debug.Print((tp!butColor).ToString() & " not a member of System.Color")
end try

Mike.


Luc,

Thanks for the reply.  No luck though - but I should admit I have no
idea how to translate the C# to VB.

NewButton.BackColor = (Color) Enum.Parse (typeof (Color), tp!
butColor)

Gives me errors on Color, Enum and Parse...I'll keep hunting.

Patrick

try something like:
NewButton.BackColor = (Color) Enum.Parse (typeof (Color), "AliceBlue")
this is C# syntax, but should easily transalate to VB.
the "AliceBlue" string in quote can be a variable containing the string
representation from the enum name.
- Show quoted text -- Hide quoted text -

- Show quoted text -
 
Can you believe it is this simple (to read the color names):


NewButton.BackColor = Color.FromName(tp!butColor)

Thanks very much, Chuck Lucas, and everyone else who worked with me on
this.

Patrick
 
Back
Top