ColorDialog in C#

Introduction

ColorDialog is a control of windows form in C#. Using ColorDialog one can change color of the windows forms, buttons, textboxes, labels etc. One can customize color of windows forms, buttons, labels using the ColorDialog control in C#.

When use ColorDialog

If we want to design the windows form then we will use ColorDialog. ColorDialog is very useful for designing purpose.

How to use ColorDialog

If we want to change the color of windows form without ColorDialog then we will coding on windows form like this..

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace color_dialog
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {           
            this.BackColor = Color.Red;
//change color of form without using ColorDialog           
        }
    }
}

Output

colordialog1.gif

If we want to change the color of windows form using ColorDialog then we will take ColorDialog from toolbox  on windows form likes below image.

colordialog5.gif

Then we will coding on form load.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace colordialog2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            colorDialog1.ShowDialog();
            this.BackColor = colorDialog1.Color;
//change color of form using ColorDialog
        }
     }
}

Output

colordialog3.gif

Now we will choose the color then press ok button.

colordialog2.gif

If we want to change the backcolor and forecolor of button or label and windows form. Then we will take three ColorDialog and one button or label from toolbox on windows form likes below image.

colordialog6.gif

Now we will coding on button click.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace colordialog3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            colorDialog1.ShowDialog();
            button1.BackColor = colorDialog1.Color;//change backcolor of button using ColorDialog
            colorDialog2.ShowDialog();
            button1.ForeColor = colorDialog2.Color;//change forecolor of button using ColorDialog
            colorDialog3.ShowDialog();
            this.BackColor = colorDialog3.Color;//change color of form when click the button using ColorDialog
        }
    }
}

Output

colordialog7.gif

Click the OK button then choose color for button and form.


colordialog8.gif

Now we will choose the color then press ok button.

colordialog9.gif

Summary

So ColorDialog is very useful for designing of windows form.


Ebook Download
View all
Learn
View all