Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleUse system colors (active border, etc.) and respond when the system's colors change in VB .NET
Keywordscolor, system color, ActiveBorder, InactiveBorder, ActiveCaption
CategoriesGraphics, VB.NET
 
Use the Color class's shared FromKnownColor method to get the colors. The KnownColor enumeration includes values for ActiveBorder, InactiveBorder, ActiveCaption, and so forth.

A control's SystemColorsChanged event fires when the system's colors change. You can use that event to update your application's colors. (Admitedly this seems like a low-priority exercise for most applications.)

 
Private Sub SetControlColors()
    Label1.BackColor = _
        Color.FromKnownColor(KnownColor.ActiveBorder)
    Label1.Text = "ActiveBorder"

    Label2.BackColor = _
        Color.FromKnownColor(KnownColor.InactiveBorder)
    Label2.Text = "InactiveBorder"

    Label3.BackColor = _
        Color.FromKnownColor(KnownColor.ActiveCaption)
    Label3.Text = "ActiveCaption"

    Label4.BackColor = _
        Color.FromKnownColor(KnownColor.ActiveCaptionText)
    Label4.Text = "ActiveCaptionText"

    Label5.BackColor = _
        Color.FromKnownColor(KnownColor.InactiveCaption)
    Label5.Text = "InactiveCaption"

    Label6.BackColor = _
        Color.FromKnownColor(KnownColor.InactiveCaptionText)
    Label6.Text = "InactiveCaptionText"

    For Each ctl As Control In Controls
        Dim brightness As Integer = _
            CInt(ctl.BackColor.R) + _
            ctl.BackColor.G + _
            ctl.BackColor.B
        If brightness > 300 Then
            ctl.ForeColor = Color.Black
        Else
            ctl.ForeColor = Color.White
        End If
    Next ctl
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
    e As System.EventArgs) Handles MyBase.Load
    SetControlColors()
End Sub

Private Sub Form1_SystemColorsChanged(ByVal sender As _
    Object, ByVal e As System.EventArgs) Handles _
    MyBase.SystemColorsChanged
    SetControlColors()
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated