问题
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.
回答1:
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);
}
回答2:
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);
回答3:
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;
}
回答4:
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
回答5:
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);
}
}
来源:https://stackoverflow.com/questions/3821174/c-sharp-getting-all-colors-from-color