ColorDialog Control in VB.NET

The Windows Forms ColorDialog component is a pre-configured dialog box that allows the user to select a color from a palette and to add custom colors to that palette.
  • 8582
 
The Windows Forms ColorDialog component is a pre-configured dialog box that allows the user to select a color from a palette and to add custom colors to that palette. It is the same dialog box that you see in other Windows applications to select colors. Use it within your Windows application as a simple solution in lieu of configuring your own dialog box.

Introduction to the Windows Forms ColorDialog Component

The Windows Forms ColorDialog component is a pre-configured dialog box that allows the user to select a color from a palette and to add custom colors to that palette. It is the same dialog box that you see in other Windows applications to select colors. Use it within your Windows application as a simple solution in lieu of configuring your own dialog box.

The color selected in the dialog box is returned in the Color property. If the AllowFullOpen property is set to false, the "Define Custom Colors" button is disabled and the user is restricted to the predefined colors in the palette. If the SolidColorOnly property is set to true, the user cannot select dithered colors. To display the dialog box, you must call its ShowDialog method.

Changing the Appearance of the Windows Forms ColorDialog Component
 
You can configure the appearance of the Windows Forms ColorDialog component with a number of its properties. The dialog box has two sections - one that shows basic colors and one that allows the user to define custom colors.

Most of the properties restrict what colors the user can select from the dialog box. If the AllowFullOpen property is set to true, the user is allowed to define custom colors. The FullOpen property is true if the dialog box is expanded to define custom colors; otherwise the user must click a "Define Custom Colors" button. When the AnyColor property is set to true, the dialog box displays all available colors in the set of basic colors. If the SolidColorOnly property is set to true, the user cannot select dithered colors; only solid colors are available to select.

If the ShowHelp property is set to true, a Help button appears on the dialog box. When the user clicks the Help button, the ColorDialog component's HelpRequest event is raised.

To configure the appearance of the color dialog box

  • Set the AllowFullOpen, AnyColor, SolidColorOnly, and ShowHelp properties to the desired values.

            ColorDialog1.AllowFullOpen = True
            ColorDialog1.AnyColor = True
            ColorDialog1.SolidColorOnly = False
            ColorDialog1.ShowHelp = True

Showing a Color Palette with the ColorDialog Component

The ColorDialog component displays a palette of colors and returns a property containing the color the user has selected.

To choose a color using the ColorDialog component

  1. Display the dialog box using the ShowDialog method.
  2. Use the DialogResult property to determine how the dialog box was closed.
  3. Use the Color property of the ColorDialog component to set the chosen color.

    In the example below, the Button control's Click event handler opens a ColorDialog component. When a color is chosen and the user clicks OK, the Button control's background color is set to the chosen color. The example assumes your form has a Button control and a ColorDialog component.

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If ColorDialog1.ShowDialog() = DialogResult.OK Then
                Button1.BackColor = ColorDialog1.Color
            End If
        End Sub

    Output :

    colordialog.gif

    Source Code attach in the Zip File.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.