ColorDialog : How to use?


Setting color of controls was not an easy task in VC++. In C#, setting colors of controls is piece of cake. The ColorDialog class does everything for you. The ColorDialog is derived from common dialog. ShowDialog displays color dialog and let you pick a color. Color property of the ColorDialog returns the current selected color.



In this sample example, I have two buttons. Foreground Color button sets foreground color of all controls while Background Color sets the background color of all the controls. Here is the corresponding code:

protected void button2_Click (object sender, System.EventArgs e)
{
ColorDialog colorDlg =
new ColorDialog();
colorDlg.ShowDialog();
textBox1.BackColor = colorDlg.Color;
listBox1.BackColor = colorDlg.Color;
btn.BackColor = colorDlg.Color;
}
protected void button1_Click (object sender, System.EventArgs e)
{
ColorDialog colorDlg =
new ColorDialog();
colorDlg.ShowDialog();
textBox1.ForeColor = colorDlg.Color;
listBox1.ForeColor = colorDlg.Color;
btn.ForeColor = colorDlg.Color;
}