BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Windows Programming
.NET 1.1+

The Colour Selector Dialog Box

The ColorDialog class generates one of the .NET framework's standard dialog boxes. This common dialog box allows the user to select a colour from a predefined palette and edit that palette to add their own reusable colours.

Custom Colours

The dialog box does not remember the custom colours. If you add a colour, close the dialog and re-open it, all of the custom colour squares are white. You need to store the colours yourself and reload them into the CustomColors property before showing the dialog box.

CustomColors holds an array of integer values. Each contains three significant bytes that represent the amount of blue, green and red in the colour, using a BGR colour model. To record the custom colours, you can read the integers on closing the dialog box, before it is disposed. You then set the array before opening the dialog box again.

Let's add a new field to the form's class, outside of any member, to hold the colours.

int[] _customColours = new int[16];

Update the click event code to set the property before showing the window and to store the integers after it is closed.

private void PickColourButton_Click(object sender, EventArgs e)
{
    using (ColorDialog cd = new ColorDialog())
    {
        cd.CustomColors = _customColours;

        if (cd.ShowDialog() == DialogResult.OK)
        {
            BackColor = cd.Color;
        }

        _customColours = cd.CustomColors;
    }
}

Run the program again and try adding custom colours. Close and reopen the dialog box to see that the custom colours are retained. In a real-world application, you could store the colours in memory, or hold them in a configuration file or in the registry if you wish to remember them after closing and restarting the application.

Controlling Expansion

There are some further options for colour selection dialog boxes. Two useful properties control the expansion of the dialog box. The first is FullOpen. This Boolean value can be set to true to automatically expand the dialog box when it is opened, showing the colour picker tools immediately.

Update the code to set the flag to true, as shown below. Run the program to see the results.

private void PickColourButton_Click(object sender, EventArgs e)
{
    using (ColorDialog cd = new ColorDialog())
    {
        cd.CustomColors = _customColours;
        cd.FullOpen = true;

        if (cd.ShowDialog() == DialogResult.OK)
        {
            BackColor = cd.Color;
        }

        _customColours = cd.CustomColors;
    }
}

Another option is to prevent the dialog box from being expanded. This is useful if you wish to restrict the available colours. To disable the "Define Custom Colours" button, set the AllowFullOpen property to false.

private void PickColourButton_Click(object sender, EventArgs e)
{
    using (ColorDialog cd = new ColorDialog())
    {
        cd.AllowFullOpen = false;

        if (cd.ShowDialog() == DialogResult.OK)
        {
            BackColor = cd.Color;
        }
    }
}
31 July 2015