C# getting all colors from Color

2020-02-23 06:46发布

I want to make a ComboBox filled with all the colors from System.Drawing.Color

But I can't seem to collect all the colors from that collection

I've already tried using a foreach to do the job like this:

foreach (Color clr in Color)
     {

     }

But all I get is an error.

So how can I loop trough all the colors?

Any help will be appreciated.

5条回答
Luminary・发光体
2楼-- · 2020-02-23 06:59

You could take color from KnownColor

KnownColor[] colors  = Enum.GetValues(typeof(KnownColor));
foreach(KnownColor knowColor in colors)
{
  Color color = Color.FromKnownColor(knowColor);
}

or use reflection to avoid color like Menu, Desktop... contain in KnowColor

Type colorType = typeof(System.Drawing.Color);
// We take only static property to avoid properties like Name, IsSystemColor ...
PropertyInfo[] propInfos = colorType.GetProperties(BindingFlags.Static | BindingFlags.DeclaredOnly | BindingFlags.Public);
foreach (PropertyInfo propInfo in propInfos) 
{
  Console.WriteLine(propInfo.Name);
}
查看更多
神经病院院长
3楼-- · 2020-02-23 07:02

This is what I think you want:

foreach (Color color in new ColorConverter().GetStandardValues())
{
    MessageBox.Show(color.ToString());
}

it will loop through all the standard values for color, and should work for what you need

查看更多
姐就是有狂的资本
4楼-- · 2020-02-23 07:09

Similar to @madgnome’s code, but I prefer the following since it doesn’t require parsing the string names (a redundant indirection, in my opinion):

foreach (var colorValue in Enum.GetValues(typeof(KnownColor)))
    Color color = Color.FromKnownColor((KnownColor)colorValue);
查看更多
我欲成王,谁敢阻挡
5楼-- · 2020-02-23 07:09

The requirement was to have a list of the system colors to chose from, a list of the "web" colors, AKA the professional colors, and then RGB via R,G,B syntax, and finally the use of the color picker control for completeness.

I save the list of colors and system color properties for use later. The ReduceName(color) removes the "Color [Name]" components from the string. If you don't maintain a running list of the colors, you will have them show up twice in the second list. There is probably a more elegant approach to handling that, but time was more important than perfect, as is often the case.

_ListAllColors = new List<Color>();
_SystemColorProperties = typeof(SystemColors).GetProperties();
foreach (PropertyInfo propertyInfo in _SystemColorProperties)
{
    object colorObject = propertyInfo.GetValue(null, null);
    Color color = (Color)colorObject;
    if (!_ListAllColors.Contains(color))
    {
        systemColorsComboBox.Items.Add(ReduceName(color));
        _ListAllColors.Add(color);
    }
}

foreach (KnownColor colorValue in Enum.GetValues(typeof(KnownColor)))
{
    Color color = Color.FromKnownColor(colorValue);

    if (!_ListAllColors.Contains(color))
    {
        professionalColorsComboBox.Items.Add(ReduceName(color));
        _ListAllColors.Add(color);
    }
}

System Colors

Professional Colors

查看更多
女痞
6楼-- · 2020-02-23 07:21

My way to get colors. I think it is the best way via Reflection library.

private List<Color> GetAllColors()
{
    List<Color> allColors = new List<Color>();

    foreach (PropertyInfo property in typeof(Color).GetProperties())
    {
        if (property.PropertyType == typeof(Color))
        {
            allColors.Add((Color)property.GetValue(null));
        }
    }

    return allColors;
}
查看更多
登录 后发表回答