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 the CommonDialog control to select a color
KeywordsCommonDialog, color, get color, select color, ShowColor
CategoriesGraphics, Controls
 
Use the dialog's ShowColor method.

To determine whether the user canceled, set the dialog's CancelError property to True, use On Error Resume Next, and watch for the cdlCancel error.

 
Private Sub Command1_Click()
    dlgColor.CancelError = True
    On Error Resume Next

    dlgColor.ShowColor
    If Err.Number = cdlCancel Then
        Exit Sub
    ElseIf Err.Number <> 0 Then
        MsgBox "Error " & Format$(Err.Number) & _
            " selecting color." & vbCrLf & Err.Description
        Exit Sub
    End If

    BackColor = dlgColor.Color
    ShowColors
End Sub
 
This example also displays the color's red, green, and blue components. It shows how to use the GetSysColor API function to get the RGB color value for system colors.
 
Private Sub ShowColors()
Dim clr As OLE_COLOR

    ' If the color is a system color, get its RGB value.
    clr = BackColor
    lblColor.Caption = Hex$(clr)
    If clr And &H80000000 Then clr = GetSysColor(clr And _
        &HFFFFFF)

    ' Display the hex value.
    lblRGB.Caption = Hex$(clr)

    ' Display the color component values.
    lblRed.Caption = Hex$(clr And &HFF&)
    lblGreen.Caption = Hex$((clr And &HFF00&) \ &H100&)
    lblBlue.Caption = Hex$((clr And &HFF0000) \ &H10000)
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated