hawbsl
hawbsl

Reputation: 16003

Winforms combobox containing system color names?

What's the easiest way to list system drawing color names in a combobox? (we don't need a full blown color picker or to see the any coloring, just the color names in black and white)

Upvotes: 1

Views: 1396

Answers (2)

Carlo Bos
Carlo Bos

Reputation: 3293

You can use linq like so to fill the ComboBox:

var colorComboBox = new ComboBox();

colorComboBox.DataSource = Enum.GetValues(typeof (KnownColor))
                .Cast<KnownColor>()
                .Where(c => !Color.FromKnownColor(c).IsSystemColor)
                .Select(kc => Enum.GetName(typeof (KnownColor), kc))
                .ToList();

You can then select the color using:

colorComboBox.Text = "AliceBlue";

Upvotes: 1

bentsai
bentsai

Reputation: 3133

Something like this:

ComboBox combo = new ComboBox();
foreach (KnownColor knownColor in Enum.GetValues(typeof(KnownColor)))
{
    Color color = Color.FromKnownColor(knownColor);
    if (!color.IsSystemColor)
    {
        combo.Items.Add(color);
    }
}

The !color.IsSystemColor check excludes the "colors" that Windows uses for various UI elements (e.g. Menu, WindowFrame).

Upvotes: 5

Related Questions