Trickey problem with editing enum based properties

  • Thread starter Thread starter thechaosengine
  • Start date Start date
T

thechaosengine

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
 
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
 
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);
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
 
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.
 
Tim said:
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.
 
this.comboBox1.Items.AddRange ( new object[]
{
System.Drawing.Color.Red ,
System.Drawing.Color.Yellow ,
System.Drawing.Color.Blue
} ) ;

this.comboBox1.DisplayMember = "Name" ;
 
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
 
PIEBALD said:
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 ;
}
 
sadhu said:
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.
 
Back
Top