Export (0) Print
Expand All

How to: Show a Color Palette with the ColorDialog Component

The ColorDialog component displays a palette of colors and returns a property containing the color the user has selected.

To choose a color using the ColorDialog component

  1. Display the dialog box using the ShowDialog method.

  2. Use the DialogResult property to determine how the dialog box was closed.

  3. Use the Color property of the ColorDialog component to set the chosen color.

    In the example below, the Button control's Click event handler opens a ColorDialog component. When a color is chosen and the user clicks OK, the Button control's background color is set to the chosen color. The example assumes your form has a Button control and a ColorDialog component.

    private void button1_Click(object sender, System.EventArgs e)
    {
       if(colorDialog1.ShowDialog() == DialogResult.OK)
       {
          button1.BackColor = colorDialog1.Color;
       }
    }
    

    (Visual C#, Visual C++) Place the following code in the form's constructor to register the event handler.

    this.button1.Click += new System.EventHandler(this.button1_Click);
    
Show:
© 2014 Microsoft