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:

Properties of the ColorDialog Control
The following are some of the commonly used properties of the ColorDialog control:
| S.N | Property | Description | 
|---|---|---|
| 1 | AllowFullOpen | Gets or sets a value indicating whether the user can use the dialog box to define custom colors. | 
| 2 | AnyColor | Gets or sets a value indicating whether the dialog box displays all available colors in the set of basic colors. | 
| 3 | CanRaiseEvents | Gets a value indicating whether the component can raise an event. | 
| 4 | Color | Gets or sets the color selected by the user. | 
| 5 | CustomColors | Gets or sets the set of custom colors shown in the dialog box. | 
| 6 | FullOpen | Gets or sets a value indicating whether the controls used to create custom colors are visible when the dialog box is opened | 
| 7 | ShowHelp | Gets or sets a value indicating whether a Help button appears in the color dialog box. | 
| 8 | SolidColorOnly | Gets or sets a value indicating whether the dialog box will restrict users to selecting solid colors only. | 
Methods of the ColorDialog Control
The following are some of the commonly used methods of the ColorDialog control:
| S.N | Method Name & Description | 
|---|---|
| 1 | Reset Resets all options to their default values, the last selected color to black, and the custom colors to their default values. | 
| 2 | RunDialog When overridden in a derived class, specifies a common dialog box. | 
| 3 | ShowDialog Runs a common dialog box with a default owner. | 
Events of the ColorDialog Control
The following are some of the commonly used events of the ColorDialog control:
| S.N | Event | Description | 
|---|---|---|
| 1 | HelpRequest | Occurs when the user clicks the Help button on a common dialog box. | 
Example
In this example, let's change the fore color 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 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 fore color of the text of the label.
 
No comments:
Post a Comment