Change a button's backcolor through code

sfx

Well-known member
Joined
Jan 31, 2006
Messages
46
Programming Experience
Beginner
Hello All,

When changing the backcolor for a button through code, how do I select the colours located in the system tab of the properties window instead of having to use the default colours, like say, "button1.backcolor = color.white" etc...?

Cheers,

sfx
 
You find these in the KnownColor enumeration, use with Color.FromKnownColor method.
 
actually there are two color sets:

Color
SystemColors

if you want the colors on the system tab, just use the SystemColors

like SystemColors.Window or SystemColors.WindowText
 
Yes, SystemColors is better to use if that is all you want, and as stated in library it is better performance to directly use a SystemPens or a SystemBrushes when a pen or brush is needed.

How about enumeration? Anyone know how to separate listing of ordinary and system colors?
 
Hi Juggalo,

Thanks alot. SystemColors is even easier to work with!

Thanks also to John for your swift replies.

Cheers,

sfx
 
JohnH said:
How about enumeration? Anyone know how to separate listing of ordinary and system colors?

Something like:
VB.NET:
[COLOR=#0000ff]Public[/COLOR] [COLOR=#0000ff]Class[/COLOR] SystemColorsEnumerator
    [COLOR=#0000ff]Private[/COLOR] [COLOR=#0000ff]Shared[/COLOR] listKnown [COLOR=#0000ff]As[/COLOR] List([COLOR=#0000ff]Of[/COLOR] KnownColor)
    [COLOR=#0000ff]Private[/COLOR] [COLOR=#0000ff]Shared[/COLOR] listColor [COLOR=#0000ff]As[/COLOR] List([COLOR=#0000ff]Of[/COLOR] Color)
 
    [COLOR=#0000ff]Public[/COLOR] [COLOR=#0000ff]Shared[/COLOR] [COLOR=#0000ff]Function[/COLOR] Colors() [COLOR=#0000ff]As[/COLOR] IEnumerable([COLOR=#0000ff]Of[/COLOR] Color)
        [COLOR=#0000ff]Return[/COLOR] listColor
    [COLOR=#0000ff]End[/COLOR] [COLOR=#0000ff]Function[/COLOR]
 
    [COLOR=#0000ff]Public[/COLOR] [COLOR=#0000ff]Shared[/COLOR] [COLOR=#0000ff]Function[/COLOR] KnownColors() [COLOR=#0000ff]As[/COLOR] IEnumerable([COLOR=#0000ff]Of[/COLOR] KnownColor)
        [COLOR=#0000ff]Return[/COLOR] listKnown
    [COLOR=#0000ff]End[/COLOR] [COLOR=#0000ff]Function[/COLOR]
 
    [COLOR=#0000ff]Shared[/COLOR] [COLOR=#0000ff]Sub[/COLOR] [COLOR=#0000ff]New[/COLOR]()
        listKnown = [COLOR=#0000ff]New[/COLOR] List([COLOR=#0000ff]Of[/COLOR] KnownColor)
        listColor = [COLOR=#0000ff]New[/COLOR] List([COLOR=#0000ff]Of[/COLOR] Color)
        [COLOR=#0000ff]For[/COLOR] [COLOR=#0000ff]Each[/COLOR] known [COLOR=#0000ff]As[/COLOR] KnownColor [COLOR=#0000ff]In[/COLOR] [Enum].GetValues([COLOR=#0000ff]GetType[/COLOR](KnownColor))
            [COLOR=#0000ff]Dim[/COLOR] c [COLOR=#0000ff]As[/COLOR] Color = Color.FromKnownColor(known)
            [COLOR=#0000ff]If[/COLOR] c.IsSystemColor [COLOR=#0000ff]Then[/COLOR]
                listKnown.Add(known)
                listColor.Add(c)
            [COLOR=#0000ff]End[/COLOR] [COLOR=#0000ff]If[/COLOR]
        [COLOR=#0000ff]Next[/COLOR]
    [COLOR=#0000ff]End[/COLOR] [COLOR=#0000ff]Sub[/COLOR]
 
[SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2]()
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]

Or the simpler method:
Compare KnownColor with the "magic" values. If value < 27 (Color.TransParent) or value > 167 (YellowGreen) then it's a SystemColor.
(Apparently this seems to be hardcoded in the Color class).
 
Excellent Don Delegate, thank you !
 
Back
Top