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.
You could take color from KnownColor
or use reflection to avoid color like Menu, Desktop... contain in KnowColor
This is what I think you want:
it will loop through all the standard values for color, and should work for what you need
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):
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.
My way to get colors. I think it is the best way via Reflection library.