Iterate through enum of system.drawing.color

A

Adam Sandler

Hello:

I'm trying to iterate through an enum of Colors. I'll post a very
basic example of the code for clarity. Here's the declaration:

enum myColor { Red, Blue, Black };

For now, I just have a foreach inside a button event handler:

private void button3_Click(object sender, EventArgs e)
{
foreach (System.Drawing.Color c in Enum.GetNames(typeof
(myColor)))
{
this.BackColor = c;
}
}

This bit of code does not compile. I get an error on the foreach
which says "Cannot convert type 'string' to 'System.Drawing.Color'".

Okay... I kinda get it. But I'm also under the impression that I have
to use System.Enum to parse the collection... this will not work
either: foreach (System.Drawing.Color c in myColor)

So, the question is, how can I iterate through an enum of colors and
then use the results to assign to properties... re: the line
this.BackColor = c ???

Suggestions are greatly appreciated.

Thanks!
 
S

Scott M.

When you call the GetNames method of the enum, you get a string array and
your loop has indicated that you'd be looping over System.Drawing.Color
objects. This is the source of your compile error.

Enums can be assigned a short integer as their type, so you could solve the
problem with a switch statement that checks the enum value and based on the
short integer, assignes the correct color.

-Scott
 
P

Peter Duniho

Hello:

I'm trying to iterate through an enum of Colors. I'll post a very
basic example of the code for clarity. Here's the declaration:

enum myColor { Red, Blue, Black };

For now, I just have a foreach inside a button event handler:

private void button3_Click(object sender, EventArgs e)
{
foreach (System.Drawing.Color c in Enum.GetNames(typeof
(myColor)))
{
this.BackColor = c;
}
}

This bit of code does not compile. I get an error on the foreach
which says "Cannot convert type 'string' to 'System.Drawing.Color'".

Okay... I kinda get it. But I'm also under the impression that I have
to use System.Enum to parse the collection... this will not work
either: foreach (System.Drawing.Color c in myColor)

There are two reasons that won't work, and one of those reasons is the
same reason you can't enumerate the names with a Color variable. It's the
wrong type. Your enumerator variable is of type "Color" and the putative
type of your collection would be "myColor", a completely different type
from Color.

The other reason is that "myColor" is a type, not an object, and so it
can't provide any enumerator that could be used in the "foreach" statement
anyway.
So, the question is, how can I iterate through an enum of colors and
then use the results to assign to properties... re: the line
this.BackColor = c ???

Impossible to say without a code example that shows how you are currently
mapping your enum to the Color type. But I can at least tell you how to
make your enumeration work:

foreach (myColor mc in Enum.GetValues(typeof(myColor)))
{
// do something with 'mc'
}

You still can't assign the value of "mc" to something of type "Color".
But at least that will allow you to enumerate the values of your enum.

Note that it's not clear to me why you have the enum at all. Unless you
have some kind of indexed collection (e.g. array) that contains actual
Color values that you retrieve using the int value of your enum, what's
the point? Why not just have an actual collection of Color values that
you use directly? We can try to provide answers to specific questions,
but I suspect you'll get better value from replies that help address
whatever the underlying problem you're trying to solve is. On the face of
it, you don't appear to be going about it in the most efficient, simple
way.

Pete
 
K

kndg

Adam said:
Hello:

I'm trying to iterate through an enum of Colors. I'll post a very
basic example of the code for clarity. Here's the declaration:

enum myColor { Red, Blue, Black };

For now, I just have a foreach inside a button event handler:

private void button3_Click(object sender, EventArgs e)
{
foreach (System.Drawing.Color c in Enum.GetNames(typeof
(myColor)))
{
this.BackColor = c;
}
}

This bit of code does not compile. I get an error on the foreach
which says "Cannot convert type 'string' to 'System.Drawing.Color'".

Okay... I kinda get it. But I'm also under the impression that I have
to use System.Enum to parse the collection... this will not work
either: foreach (System.Drawing.Color c in myColor)

So, the question is, how can I iterate through an enum of colors and
then use the results to assign to properties... re: the line
this.BackColor = c ???

Suggestions are greatly appreciated.

Thanks!

Hi Adam,

Enum.GetNames() returns an array of string (the name of your enum
values). If you want the above to compile, change your code something
like this,

foreach (string color in Enum.GetNames(typeof(myColor))
{
this.BackColor = Color.FromName(color);
}

but you will get Color.Black if myColor is not within the
Color.KnownColor enumeration.

But just as what Pete said, if you want to limit your color to a set
defined value, better to just have an actual collection of Color values
instead of enum.

HTH.
 
A

Adam Sandler

foreach (string color in Enum.GetNames(typeof(myColor))
{
   this.BackColor = Color.FromName(color);

}


Thanks everyone... I just had read about Color.FromName(string) and
came to reply and see that kndg beat me to the punch.
 
Joined
Feb 6, 2010
Messages
1
Reaction score
0
If anyone needs to iterate through all the named properties of Color structure (that have type Color) such as Color.Red, Color.Yellow, Color.Green... you can use folowwing code:

PropertyInfo[] colors = typeof(Color).GetProperties();

ColorConverter converter = new ColorConverter();

foreach (PropertyInfo pi in colors)

{

if (pi.PropertyType.Name == "Color")

{

Color color =
(Color)converter.ConvertFromString(pi.Name);
// do whatever you want with the color here

}

}


Good thing about this code is that with just a little change it allows you to iterate through all the members of Brushes class (note, it's not a structure, like struct Color!) or any other.

P.S. Dont forget to add two following namespaces to your project:

using System.Drawing;
using System.Reflection;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top