
Creating a ColorDialog
Again, you can create a ColorDialog at design-time but It is easier to create a ColorDialog 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 in Visual Studio. After you drag and drop a ColorDialog on a Form, the ColorDialog looks like Figure 2.

Adding a ColorDialog to a Form adds following two lines of code.
- private System.Windows.Forms.ColorDialog colorDialog1;
- this.colorDialog1 = new System.Windows.Forms.ColorDialog();
- ColorDialog colorDlg = new ColorDialog();
ShowDialog method of ColorDialog displays the ColorDialog. The following code snippet sets background color, foreground color, Text, Name, and Font properties of a ColorDialog.
- colorDlg.ShowDialog();
ColorDialog Properties
After you place a ColorDialog control on a Form, the next step is to set properties.The easiest way to set properties is from the Properties Window. You can open Properties window by pressing F4 or right click on a control and select Properties menu item. The Properties window looks like Figure 3.

AllowFullOpen
If you look at Figure 1, you will see a button called Define Custom Colors on the ColorDialog. Clicking on this button opens the custom color editor area where you can define colors by setting RGB color values (between 0 to 255) and can also select a color from the color area as you can see in Figure 4.

AllowFullOpen property makes sure that Define Custom Color option is enabled on a ColorDialog. If you wish to disable this option, you can set AllowFullOpen property to false and your ColorDialog will look like Figure 5.

- colorDlg.AllowFullOpen = false;
Color, AnyColor, and SolidColorOnly
- colorDlg.AnyColor = true;
- colorDlg.SolidColorOnly = false;
- colorDlg.Color = Color.Red;
ColorDialog Code Example

In Figure 6, we have a few Windows Forms controls and clicking on Foreground Color and Background Color buttons will let user select a color and set that color as foreground and background colors of the controls. After selecting foreground and background colors, the Form looks like Figure 7.

- 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;
- }
- }
Summary
A ColorDialog control allows users to launch Windows Color Dialog and let them select a solid color or create a custom color from available colors. In this article, we discussed how to use a Windows Color Dialog and set its properties in a Windows Forms application.

Rushi MehtaPosted Aug 5, 2018, 11:53 PM
Great Article Mahesh...
Mahesh ChandPosted Aug 2, 2018, 10:22 AM
Thanks guys. It was written in year 2000. Just updating and cleaning up old articles. Google doesn't like old articles :)
Bhairab DuttPosted Aug 2, 2018, 9:19 AM
Nice Article :)
Grabsi AminePosted Dec 16, 2012, 11:50 AM
Niiice :)
James ClearyPosted Jun 21, 2012, 11:44 AM
Hi there and thanks for posting this, I was wondering is there a way to return the custom color in real time - as the user changes setting as opposed to having to wait for the OK button to be pressed ? - Thanks
Walter KiessPosted Mar 8, 2012, 9:34 PM
Hi, Great Article, Thanks! Just one question: How do you position the Colordialog on the screen? There are no Top or Left properties... Regards, Walter
Mahesh ChandPosted Jun 28, 2010, 11:17 PM
Code and article updated.