In CMD language you can define all at once like:del C:\randomfiles\*
That Means you select ALL files at once. I have the Code var names = new List<System.Drawing.Color> { Red, DarkRed, 1000 colors later... };
but I dont want to tipe in all colors because that may take years... How can I define all at once like in CMD language?
EDIT: This is not the same as: How do I enumerate an enum? because I want al ist of all colors WITHOUT spending years writing all colors....
Asked
Active
Viewed 106 times
-2

Community
- 1
- 1

SinOfficial
- 536
- 5
- 15
-
I want to list all the colors without writing tons of code because there are over 100 colors. – SinOfficial Jan 02 '17 at 17:31
-
You use an inappropriate example, using wrong terms, to ask something unrelated. Your question has nothing to do with "How do I retrieve a list of all defined colors"? – Panagiotis Kanavos Jan 02 '17 at 17:36
-
no need for list if just a random color is needed http://stackoverflow.com/questions/5805774/how-to-generate-random-color-names-in-c-sharp – Slai Jan 02 '17 at 18:47
-
My mistake, it's not an enum - but there's an exact duplicate question. (But your reasoning in the edit was off - it's not a duplicate of that because `System.Drawing.Color` isn't an enum, not because you want a list without writing them all out...) – Jon Skeet Jan 02 '17 at 18:47
1 Answers
5
To create a list of all colors you can use something like this
List<Color> allColors = new List<Color>();
foreach (KnownColor col in Enum.GetValues(typeof(KnownColor)))
{
Color c = Color.FromKnownColor(col);
if(!c.IsSystemColor)
allColors.Add(c);
}
KnownColor is an enum and this will allow us to traverse all the definitions using a simple foreach.
Of course, if you want also the system colors (WindowText, ControlText, etc...) remove the check on IsSystemColor

Steve
- 213,761
- 22
- 232
- 286
-
It gives me the error:`The Type "System.Collections.Generic.List
" can not be implicitly converted to "System.Drawing.Color".` – SinOfficial Jan 02 '17 at 17:38 -
Uhm, are you sure? Just checked this in LinqPad and there is no such error in the code above. – Steve Jan 02 '17 at 17:40
-
-
You should assign a single color to ForeColor not the whole list. It seems that you have not explained well what are you trying to do here. – Steve Jan 02 '17 at 17:43
-
uhh i forgot about this code sorry `int indexpick = randomcolor.Next(allColors.Count); var name = allColors[indexpick];` now it worked thank you – SinOfficial Jan 02 '17 at 17:47