Home > DeveloperSection > Articles > HOW TO USE COLOR DIALOG BOX IN C#

HOW TO USE COLOR DIALOG BOX IN C#


C# C# 
Ratings:
0 Comment(s)
 13109  View(s)
Rate this:

How to use color dialog box in c#

Program states that in one form if we want to place one button, while clicking on the button next form is appear with change color and having a display button like change color in a new window button, if we click new window button we will take color dialog box along with button which looks like button (…) along with the textbox. If we click on (…)button color dialog box is appear and if we choose color on that dialog box the back color of the textbox will be filled like that.

Open new project and create a Form1 with one button having text property click to change color as shown below.

COLOR DIALOG BOX IN C#

Place a code on the double click on the button event.

public partial class color : Form

    {

        Boolean check = false;//declare check for conditions

        public color()

        {

            InitializeComponent();

        }

 

        private void BtnColour_Click(object sender, EventArgs e)

        {

            if (check == false)//checking conditions if check is false

//then executes this statement.

            {

                this.BackColor = Color.Red;//color of the form is red.

                BtnColour.Text = "new window";//resetting button text

                check = true;

            }

            else

            {

                Form1 fm = new Form1();//create object of the form as fm

                fm.ShowDialog();

 

            }

           

Next create another page with the help of add existing item,as shown below:

COLOR DIALOG BOX IN C#

   As soon as we click on the button(…)we can see a colour dialog box as shown below.

COLOR DIALOG BOX IN C#

        FIG 1

As soon as we select color in the colordialogbox it will come in the textbox.

Now place a piece of code on the button (…) as below.

 

private void btncolor_Click(object sender, EventArgs e)

        {

            colorDialog1.ShowDialog();//dialogbox will appear.

            textBox1.BackColor = colorDialog1.Color;//in textbox1backcolour filled with the help of colour dialog box as shown in above figure.           

        }

 

Similarly for the second color dialog button (…)

private void button1_Click(object sender, EventArgs e)

        {

            colorDialog2.ShowDialog();

            txtBrowser2.BackColor = colorDialog2.Color;

        }

The output will be appear like

OUTPUT OF COLOR DIALOG BOX IN C#

As soon as we click color with the help of color dialog box we will see particular color in the textbox. As shown below in the snapshot.

 


Don't want to miss updates? Please click the below button!

Follow MindStick