ColorDialog
is one of the common dialogs in .Net Framework; used to display the available colors and also allows us to define custom colors. Through this article, we are going to discuss, how we use ColorDialog
.
Usually, we have Standard colors and User-defined colors. By modifying the individual color components of the Standard colors, we can define our own custom colors. This dialog provides an interface to display standard colors and allows us to define custom colors.
ColorDialog
class represents ColorDialog
; provides different properties & methods; we are going to discuss commonly used ones, in this article.
Properties of ColorDialog
It has a set of properties to display and control the user interface to allow us to select or define the color. The Color property is used to get or set the color selected. It is of Color
class type.
Color selectedColor = colorDialog.Color;
AnyColor
property is used to control whether ColorDialog
displays all available basic colors under the “Basic colors:” group.
SolidColorOnly
property will restrict the user to select only solid colors. No custom colors are allowed to select. Note that, this property is applicable to systems with 256 or fewer colors. On other systems, this will not give any effect.
AllowFullOpen
property, enables or disables to define custom colors. When this set to false
, “Define Custom Colors >>” button will be disabled in the dialog. This button displays the custom color controls to define the custom colors.
CustomColors
property will be set with the defined custom colors by the dialog when we define them. It is an array of Int32
type. Each color comprises RED, GREEN & BLUE values (RGB). This will be updated, only when allowed to define the custom colors, using AllowFullOpen
property.
When the dialog is opened, initially, custom color controls are not visible. We can control this by setting FullOpen
property. When AllowFullOpen
property is set to false; setting up this property will not give any effect on the dialog.
ShowHelp
property is to show or hide the Help button on the dialog.
Methods of ColorDialog
ShowDialog
method is used to display the ColorDialog
, and allows to select the color. This method returns DialogResult
enum type; of value OK
, Cancel
etc, which is depending on the button selected on the dialog.
if (colorDialog.ShowDialog(this) == DialogResult.OK) // color selected else // no color selected
ColorDialog Events
ColorDialog
triggers an event, HelpRequest,
when we click on the Help button on the dialog. When ShowHelp
property is set to false,
this event will not be triggered as Help
button will not be visible to the user to click.
Now, this is the time to write our code. By putting all together, what we discussed so far, in this article; here is the complete working example;
Here is the screenshot of the Application;
(Raju)