C# Color Dialog Box -Trickcode

The ColorDialog Control,The ColorDialog (System.Windows.Forms.ColorDialog) is used when you want to pick different colors. For example, when you want to pick for color of the font or a background color for the form, you can use the ColorDialog control
Share it:

The ColorDialog Control

The ColorDialog (System.Windows.Forms.ColorDialog) is used when you want to pick different colors. For example, when you want to pick for color of the font or a background color for the form, you can use the ColorDialog control.


The ColorDialog Control

The following are some of the useful properties of the ColorDialog control.

PropertiesDescription
AllowFullOpenSpecifies whether the user can choose custom colors.
ColorThe color that the user selected.
CustomColorsA collection of custom colors picked by the user.
FullOpen
Specifies whether the part used to pick custom colors are automatically open.


The window initially composed of predefined color palettes. You can click the Define Custom Colors button to reveal more colors where you can pick every color you can think of. You can even provide the Hue, Saturation, Luminance values, or RGB values. The main window allows you to choose colors while the right slider adjusts the brightness of the color. You can click the Add Custom Colors button to put the selected color to the Custom Colors pallet so you can reuse it later.


The following example demonstrates the use of the ColorDialog. Create a similar form shown below.
ColorDialog
Drag a ColorDialog control to the form. It will not be visible to the form but you can find it a the bottom section of the Designer.


Drag a ColorDialog control


To change properties of the ColorDialog control, click it in the designer and proceed to Properties Window.

Double click the button to create an event handler for its Click event. Use the following code for the event handler.

private void button1_Click(object sender, EventArgs e)
{
    DialogResult result = colorDialog1.ShowDialog();

    if (result == DialogResult.OK)
    {
        this.BackColor = colorDialog1.Color;
    }
}


The first line of code calls the ColorDialog's ShowDialog static method. This method shows up the color dialog where the user can pick a color. This method returns a System.Windows.Forms.DialogResult value which indicates whether the user clicked the OK or the Cancel button of the Dialog. If the user clicked color and pressed the OK button, the dialog closes and the color the user picked is stored in the Color property of the control. We test the value of the result to determine if the user clicks the OK button. If so, we changed the background color of the form to the color the user picked using the value of the Color property.
ColorDialog's ShowDialog

Run the program and click the button. The Color dialog will open up. Select a color and click OK. Watch as the color of the form changes to the color you have picked.

Do you know:-


If you like the tutorial, then please share this tutorial with your friends on social media.
Share it:

C#

Windows Forms

Post A Comment:

0 comments: