TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

ColorDialog Example: Windows Forms

This C# tutorial uses the ColorDialog control in Windows Forms. A ColorDialog shows a color picker.

ColorDialog displays a color selection window.

By using the ColorDialog, you can enable a visual interface for selecting a specific color. Some options are provided through properties on this control.

Example. To begin, we will add the ColorDialog instance to the form and then use it in some C# code. Most of the work you do with the ColorDialog will require the ShowDialog method and also the Color property.

Tip: When you invoke ShowDialog, please remember to check the DialogResult. We avoid taking an action when the dialog was canceled.

In this code, we show the ColorDialog instance, and then see if the user clicked the OK button to accept. If OK was clicked, the parent form's background color is set to the color selected in the dialog.

Example that uses ColorDialog control: C#

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
	public Form1()
	{
	    InitializeComponent();
	}

	private void Form1_Load(object sender, EventArgs e)
	{
	    // Show the color dialog.
	    DialogResult result = colorDialog1.ShowDialog();
	    // See if user pressed ok.
	    if (result == DialogResult.OK)
	    {
		// Set form background to the selected color.
		this.BackColor = colorDialog1.Color;
	    }
	}
    }
}

Properties. The ColorDialog allows you to open a more complete color picker to the right side with a button by default. If the AllowFullOpen property is set to False, this button is disabled.

And: If FullOpen is set to True, the full color picker is shown automatically when the ColorDialog is opened.

AnyColor and SolidColorOnly properties. These two properties are provided on the ColorDialog, but I could not detect any difference in behavior when they were set to True or False.

Note: It is possible that patterns were once available in the ColorDialog, and these properties could have been used to disable them.

Tip: There is truly a limit to Dot Net Perls and its knowledge. Another site, like MSDN might reveal more here.

CustomColors. You can acquire the custom colors in the ColorDialog that were added by the user. You can also add your own colors there with code. This is an integer array—you can convert those integers into Color types if necessary.

Color

Summary. Probably one of the simplest controls to add to your program, the ColorDialog provides the necessary functionality of a visual color picker. There are only a few useful properties on this control, such as the Color property.

Typically: It is not worth your time to develop a custom color widget unless you are working on a graphics-oriented program.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf