Color dialog box in c#


private void ForegroundButton_Click(object sender, EventArgs e)  
{  
    ColorDialog colorDlg = new ColorDialog();  
    colorDlg.AllowFullOpen = false;  
    colorDlg.AnyColor = true;  
    colorDlg.SolidColorOnly = false;  
    colorDlg.Color = Color.Red;                      
  
    if (colorDlg.ShowDialog() == DialogResult.OK)  
    {  
        textBox1.ForeColor = colorDlg.Color;  
        listBox1.ForeColor = colorDlg.Color;  
        button3.ForeColor = colorDlg.Color;  
    }  
}  
  
  
private void BackgroundButton_Click(object sender, EventArgs e)  
{  
    ColorDialog colorDlg = new ColorDialog();  
    if (colorDlg.ShowDialog() == DialogResult.OK)  
    {  
        textBox1.BackColor = colorDlg.Color;  
        listBox1.BackColor = colorDlg.Color;  
        button3.BackColor = colorDlg.Color;  
    }  
} 

Color Dialog

We can make a ColorDialog control utilizing a Forms fashioner at configuration time or utilizing the ColorDialog class in code at run-time (otherwise called progressively) Color dialog box. Not at all like different Windows Forms controls, a ColorDialog doesn’t have and not need visual properties like others. The main motivation behind ColorDialog to show accessible hues, make custom hues, and select a shading from these hues. When shading is chosen, we need that shading in our code so we can apply it to different controls.

Once more, you can make a ColorDialog at configuration time however It is simpler to make a ColorDialog at run-time.

Configuration time

To make a ColorDialog control at configuration time, you basically intuitive a ColorDialog control from Toolbox to a Form in Visual Studio. After you simplified a ColorDialog on a Form, the ColorDialog looks like Figure 2.

Adding a ColorDialog to a Form includes following two lines of code.

private System.Windows.Forms.ColorDialog colorDialog1;

this.colorDialog1 = new System.Windows.Forms.ColorDialog();

Run-time Color dialog box

Making a ColorDialog control at run-time is simply a work of making an occasion of ColorDialog class, set its properties and add ColorDialog class to the Form controls.

Initial step to make a dynamic ColorDialog is to make an occasion of ColorDialog class. The accompanying code piece makes a ColorDialog control object.

ColorDialog colorDlg = new ColorDialog();

ShowDialog technique for ColorDialog shows the ColorDialog. The accompanying code piece sets foundation shading, closer view shading, Text, Name, and Font properties of a ColorDialog.

colorDlg.ShowDialog();

When the ShowDialog strategy is called, you can pick hues on the discourse.

ColorDialog Properties

After you place a ColorDialog control on a Form, the subsequent stage is to set properties.

The least demanding approach to set properties is from the Properties Window. You can open Properties window by squeezing F4 or right snap on a control and select Properties menu thing.

AllowFullOpen

In the event that you take a gander at Figure 1, you will see a catch called Define Custom Colors on the ColorDialog. Tapping on this catch opens the custom shading editorial manager region where you can characterize hues by setting RGB shading esteems (between 0 to 255) and can likewise choose a shading from the shading zone as should be obvious in Figure 4.

AllowFullOpen property ensures that Define Custom Color choice is empowered on a ColorDialog. In the event that you wish to debilitate this alternative, you can set AllowFullOpen property to bogus and your ColorDialog will look like Figure 5.

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *

Categories

%d bloggers like this: