Blue Theme Orange Theme Green Theme Red Theme
 
Discover the top 5 tips for understanding .NET Interop
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
DevExpress UI Controls
Search :       Advanced Search »
Home » Windows Forms C# » ColorDialog in C#

ColorDialog in C#

A ColorDialog control is used to select a color from available colors and also define custom colors. This article demonstrates how to use a ColorDialog in Windows Forms and C#.

Author Rank :
Page Views : 52033
Downloads : 390
Rating :
 Rate it
Level : Intermediate
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
ColorDialogSample.zip
 
 
Mindcracker MVP Summit 2012
Become a Sponsor
DevExpress Free UI Controls
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


Color Dialog
A ColorDialog control is used to select a color from available colors and also define custom colors. A typical Color Dialog looks like Figure 1 where you can see there is a list of basic solid colors and there is an option to create custom colors.

ColorDialogImg1.jpg
Figure 1

Creating a ColorDialog

We can create a ColorDialog control using a Forms designer at design-time or using the ColorDialog class in code at run-time (also known as dynamically). Unlike other Windows Forms controls, a ColorDialog does not have and not need visual properties like others. The only purpose of ColorDialog to display available colors, create custom colors and select a color from these colors. Once a color is selected, we need that color in our code so we can apply it on other controls.

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.

ColorDialogImg2.jpg
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();

Run-time

Creating a ColorDialog control at run-time is merely a work of creating an instance of ColorDialog class, set its properties and add ColorDialog class to the Form controls.

First step to create a dynamic ColorDialog is to create an instance of ColorDialog class. The following code snippet creates a ColorDialog control object.

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();

 

Once the ShowDialog method is called, you can pick colors on the dialog.

 

Setting 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.

ColorDialogImg3.jpg
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.

ColorDialogImg4.jpg
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.

ColorDialogImg5.jpg
Figure 5
 

The following code snippet sets the AllowFullOpen property to false.

colorDlg.AllowFullOpen = false;

Color, AnyColor, and SolidColorOnly

Color property is used to get and set the color selected by the user in a ColorDialog.

AnyColor is used to get and set whether a ColorDialog displays all available colors in the set of basic colors.

SolidColorOnly is used to get and set whether a ColorDialog restricts users to selecting solid colors only.

The following code snippet sets these properties.

colorDlg.AnyColor = true;

colorDlg.SolidColorOnly = false;

colorDlg.Color = Color.Red;

Using ColorDialog in Applications

Now let's create an application that will use a ColorDialog to set colors of bunch of controls. The Windows Forms application looks like Figure 6.

ColorDialogImg6.jpg
Figure 6

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.

ColorDialogImg7.jpg
Figure 7

The following code snippet is the code for Foreground Color and Background Color buttons click event handlers.

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.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Mahesh Chand
Mahesh is the founder of C# Corner and Mindcracker Network, an author of several .NET programming books and a Microsoft MVP for 6 consecutive years. In his day to day work, Mahesh is a Senior Software Consultant with over 14 years of IT industry experience building systems for Financial and Banking, Engineering & Architectural, Imaging, Construction, Biological & Pharmaceuticals, Healthcare and Education industries. His expertise is Windows Forms, ASP.NET, Silverlight, WPF, WCF, Visual Studio 2010, SQL Server, and Oracle.  If you are looking for a Sharepoint, Windows Forms, ASP.NET, WPF, Silverlight, C#, VB.NET, Oracle, and SQL Server Consultant in Philadelphia area or remote location, drop me a line at MAHESH [AT] C-SHARPCORNER [DOT] COM.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the top 5 tips for understanding .NET
Ricky Leeks presents the top 5 tips for understanding .NET Interoperability. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Nevron Chart
Become a Sponsor
 Comments
Updated to VS 2010 by Mahesh On June 28, 2010
Code and article updated.
Reply | Email | Modify 
Team Foundation Server Hosting
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.