Saturday, 14 September 2013

VB.Net - ColorDialog Control

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:
VB.Net Color Dialog

Properties of the ColorDialog Control

The following are some of the commonly used properties of the ColorDialog control:
S.NPropertyDescription
1AllowFullOpenGets or sets a value indicating whether the user can use the dialog box to define custom colors.
2AnyColorGets or sets a value indicating whether the dialog box displays all available colors in the set of basic colors.
3CanRaiseEventsGets a value indicating whether the component can raise an event.
4ColorGets or sets the color selected by the user.
5CustomColorsGets or sets the set of custom colors shown in the dialog box.
6FullOpenGets or sets a value indicating whether the controls used to create custom colors are visible when the dialog box is opened
7ShowHelpGets or sets a value indicating whether a Help button appears in the color dialog box.
8SolidColorOnlyGets 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.NMethod Name & Description
1Reset
Resets all options to their default values, the last selected color to black, and the custom colors to their default values.
2RunDialog
When overridden in a derived class, specifies a common dialog box.
3ShowDialog
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.NEventDescription
1HelpRequestOccurs 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:
  1. Drag and drop a label control, a button control and a ColorDialog control on the form.
  2. Set the Text property of the label and the button control to 'Give me a new Color' and 'Change Color' respectively.
  3. Change the font of the label as per your likings
  4. 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:
VB.Net Color Dialog Result
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