Share via


How to: Display a Color Palette

You can use the built-in ColorDialog component to display a color dialog box instead of creating your own color palette. For example, you can enable users to select a color to apply to a Windows form when they click a button on the form.

To display the color dialog box

  1. On the File menu, click New Project.

    The New Project dialog box appears.

  2. Click Windows Forms Application and then click OK.

  3. From the Toolbox, drag a Button control to the form, and change the following properties in the Properties window:

    Property

    Value

    Name

    formColor

    Text

    Color

  4. Drag a ColorDialog component from the Dialogs tab of the Toolbox to the form.

    colorDialog1 appears in the component tray.

  5. Double-click the Color button to create the default event handler in the Code Editor.

  6. In the formColor_Click event handler, add the following code to display the color dialog box and change the background color of the form according to the user's choice.

    if (colorDialog1.ShowDialog() == DialogResult.OK)
    {
        this.BackColor = colorDialog1.Color;
    }
    
  7. Press F5 to run the code.

  8. When the form opens, click Color, click a color in the resulting dialog box, and then click OK.

  9. Verify that the chosen color is applied to the form.

  10. Close the application.

See Also

Concepts

Using Built-in Dialog Boxes in Your Application

Designing a User Interface in Visual C#

Other Resources

Dialog Boxes (Visual C#)

Visual C# Guided Tour