Visual Basic Concepts

Using the Color Dialog Box

See Also

The Color dialog box allows the user to select a color from a palette or to create and select a custom color. At run time, when the user chooses a color and closes the dialog box, you use the Color property to get the selected color.

Figure 7.13   The Color dialog box

To display the Color dialog box

  1. Set the Flags property for the common dialog control to the Visual Basic constant cdlCCRGBInit.

  2. Use the ShowColor method to display the dialog box.

Use the Color property to get the RGB value of the color the user selects. The following code displays the Color dialog box when a user clicks the Command1 command button:

Private Sub Command1_Click ()
   ' Set Cancel to True.
   CommonDialog1.CancelError = True
   On Error GoTo ErrHandler
   ' Set the Flags property.
   CommonDialog1.Flags = cdlCCRGBInit
   ' Display the Color dialog box.
   CommonDialog1.ShowColor 
   ' Set the form's background color to the selected 
   ' color.
   Form1.BackColor = CommonDialog1.Color
   Exit Sub

ErrHandler:
   ' User pressed Cancel button.
   Exit Sub
End Sub

For More Information   See "CommonDialog Control Constants" for a complete list of Flag property constants.