Tuesday, May 8, 2012

Color Dialog

Author: Konjerla Suresh

Color dialog Control is Pallet of colors. This Control is used to select any color from available colors and also used to define the custom colors.  A typical color Dialog control is looks like below.

 The only purpose of color dialog control is to display the available colors and to create the custom colors and apply those colors to our application.

Creating a colorDialog Control:
 We can create a color dialog control in two ways, those are
1)      Using Forms Designer at design time
      2)      Using ColorDialog Control at run time.

Design Time:
To create a ColorDialog control at design-time, you simply drag and drop a ColorDialog control from Toolbox to a Form. 
Run time:

 To create a Color Dialog At run time, you simple create the instance for ColorDialog class. Call the ShowDialog method to display the color dialog
  Syntax: ColorDialog colordlg1 = new ColorDialog ();
Setting ColorDialog Properties:
Open the properties window by pressing F4 or right click on a control and select Properties menu item. It looks like below 

We can create custom define colors only when this property is true only. If this property is false, we can use only available colors.
When this property is true – define Custom colors option will be enabled where you can define your required color by selecting color form colors area or by setting RGB values. 

When this property is false - define Custom colors option will be disabled

Example program:
Create an application that changes form back  ground color and button fore ground color 

1)      Open Windows application.
2)      Drag a button on form.
3)      Double click on the button and write following code in button1 _click event.
    private void button1_Click(object sender, EventArgs e)
            ColorDialog clrDlg = new ColorDialog();
            button1.ForeColor = clrDlg.Color;
            this.BackColor = clrDlg.Color;

Output : 

No comments:

Post a Comment