By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,361 Members | 1,824 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,361 IT Pros & Developers. It's quick & easy.

Trickey problem with editing enum based properties

P: n/a
Hi all,

I have a problem that I havent really come accross before.

I need to be able to allow the user to set a dropdown control to a particular
colour value. The permissable values come from the Color enumeration and
unfortunately I can't change this implementation detail.

My question is, how can I load the values into the dropdown using the enumeration,
and more importantly, when a user selects an entry in the dropdown, how do
I reconcile the selected value back into a Color from the enumeration?

The only solution that I could really think of was quite a messy approach
using lots of conditional statements and I'm thinking there must be a more
elegant way.

Quite why it was implemented using the Color enum I don't know. I would have
chosen a string personally.

Many thanks to anybody who could offer advice on how to approach this

Kindest Regards

thechaosengine

Jul 21 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
> My question is, how can I load the values into the dropdown using the
enumeration,

Dim enumType As Type = GetType(Your_Enum)
Dim names() As String = [Enum].GetNames(enumType)
Dim i As Integer

For i = 0 To names.Length - 1
ComboBox1.Items.Add(names(i))
Next
and more importantly, when a user selects an entry in the dropdown, how do
I reconcile the selected value back into a Color from the enumeration?


'Retrieve value of the selected item.
Dim enumType As Type = GetType(Your_Enum)
Dim selection As String = DirectCast(ComboBox1.SelectedItem, String)
Dim value As Frequency = DirectCast([Enum].Parse(enumType, selection),
Your_Enum)

Hope this helps

Jul 21 '05 #2

P: n/a
Use the static method GetNames() and GetValues.

Array enumValues = Enum.GetValues(enumType);
string [] enumNames = Enum.GetNames(enumType);

for (int i = 0; i<enumNames.Length; ++i)
{
comboBox.AddItem(enumNames[i]);
addedItem.Tag = enumValues.GetValue(i);//Associate value with
that item.
}

You can then simply cast the Tag field of the selected item back to the
enum. If you don't feel comfortable using the Tag field, you can use
Enum.Parse() and pass the text of the selected item.

Regards
Senthil

Jul 21 '05 #3

P: n/a
thechaosengine wrote:

Quite why it was implemented using the Color enum I don't know. I
would have chosen a string personally.

Many thanks to anybody who could offer advice on how to approach this


Color is not an enum, its a struct.

You could use reflection to do what you want, look for the public
static properties of type Color, or alternatively you could use the
KnownColor enumeration i.e.

private void button1_Click(object sender, System.EventArgs e)
{
int[] allColors = (int[])Enum.GetValues(typeof(KnownColor));
foreach(int i in allColors)
{
Color col = Color.FromKnownColor((KnownColor)i);
comboBox1.Items.Add(col.Name);
}
}

I would be inclined to create a wrapper class, load it up with the
color, overload the ToString() method to display the color name and
store the whole thing in the combo, rather than muck around with the
tag property.
Regards Tim.
Jul 21 '05 #4

P: n/a
Tim Jarvis wrote:

private void button1_Click(object sender, System.EventArgs e)
{
int[] allColors = (int[])Enum.GetValues(typeof(KnownColor));
foreach(int i in allColors)
{
Color col = Color.FromKnownColor((KnownColor)i);
comboBox1.Items.Add(col.Name);
}
}
Regards Tim.


Actually, this would be more elegant...

private void button1_Click(object sender, System.EventArgs e)
{
string[] allColors = (string[])Enum.GetNames(typeof(KnownColor));
comboBox1.Items.AddRange(allColors);
}

then create the color when you need it, i.e.

Color c = Color.FromName(**the name from the combo box**);

Cheers Tim.
Jul 21 '05 #5

P: n/a
Thank you all.

You are lifesavers!

Thanks

Jul 21 '05 #6

P: n/a
this.comboBox1.Items.AddRange ( new object[]
{
System.Drawing.Color.Red ,
System.Drawing.Color.Yellow ,
System.Drawing.Color.Blue
} ) ;

this.comboBox1.DisplayMember = "Name" ;

Jul 21 '05 #7

P: n/a
Color is an Enum, I guess you've already realized that.. Using
AddRange() is a good idea indeed. But what is KnownColor???
And you don't need a Color.FromName, that's what the Enum.Parse()
method is for.

Regards
Senthil

Jul 21 '05 #8

P: n/a


"PIEBALD" wrote:
this.comboBox1.Items.AddRange ( new object[]
{
System.Drawing.Color.Red ,
System.Drawing.Color.Yellow ,
System.Drawing.Color.Blue
} ) ;

this.comboBox1.DisplayMember = "Name" ;


Each item _is_ a color, so...

private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
this.comboBox1.BackColor = (System.Drawing.Color)
this.comboBox1.SelectedItem ;
}

Jul 21 '05 #9

P: n/a
sadhu wrote:
Color is an Enum, I guess you've already realized that.. Using
AddRange() is a good idea indeed. But what is KnownColor???
And you don't need a Color.FromName, that's what the Enum.Parse()
method is for.

Regards
Senthil


Nope, it's not. Have a look in reflector if you don't believe me.

Jul 21 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.