The ColorDialog control class represents a common dialog box that displays available colors along with controls that enable the user to define custom colors. It lets the user select a color.
The main property of the ColorDialog control is Color, which returns a Color object.
Following is the Color dialog box −
The following are some of the commonly used properties of the ColorDialog control −
|Sr.No.||Property & Description|
Gets or sets a value indicating whether the user can use the dialog box to define custom colors.
Gets or sets a value indicating whether the dialog box displays all available colors in the set of basic colors.
Gets a value indicating whether the component can raise an event.
Gets or sets the color selected by the user.
Gets or sets the set of custom colors shown in the dialog box.
Gets or sets a value indicating whether the controls used to create custom colors are visible when the dialog box is opened.
Gets or sets a value indicating whether a Help button appears in the color dialog box.
Gets or sets a value indicating whether the dialog box will restrict users to selecting solid colors only.
The following are some of the commonly used methods of the ColorDialog control −
|Sr.No.||Method Name & Description|
Resets all options to their default values, the last selected color to black, and the custom colors to their default values.
When overridden in a derived class, specifies a common dialog box.
Runs a common dialog box with a default owner.
The following are some of the commonly used events of the ColorDialog control −
|Sr.No.||Event & Description|
Occurs when the user clicks the Help button on a common dialog box.
In this example, let's change the forecolor of a label control using the color dialog box. Take the following steps −
Drag and drop a label control, a button control and a ColorDialog control on the form.
Set the Text property of the label and the button control to 'Give me a new Color' and 'Change Color', respectively.
Change the font of the label as per your likings.
Double-click the Change Color button and modify the code of the Click event.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click If ColorDialog1.ShowDialog <> Windows.Forms.DialogResult.Cancel Then Label1.ForeColor = ColorDialog1.Color End If End Sub
When the application is compiled and run using Start button available at the Microsoft Visual Studio tool bar, it will show the following window −
Clicking on the Change Color button, the color dialog appears, select a color and click the OK button. The selected color will be applied as the forecolor of the text of the label.