473,322 Members | 1,510 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Trickey problem with editing enum based properties

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

Nov 16 '05 #1
9 1312
> 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

Nov 16 '05 #2
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

Nov 16 '05 #3
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.
Nov 16 '05 #4
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.
Nov 16 '05 #5
Thank you all.

You are lifesavers!

Thanks

Nov 16 '05 #6
this.comboBox1.Items.AddRange ( new object[]
{
System.Drawing.Color.Red ,
System.Drawing.Color.Yellow ,
System.Drawing.Color.Blue
} ) ;

this.comboBox1.DisplayMember = "Name" ;

Nov 16 '05 #7
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

Nov 16 '05 #8


"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 ;
}

Nov 16 '05 #9
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.

Nov 16 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: thechaosengine | last post by:
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...
0
by: keith bannister via .NET 247 | last post by:
(Type your message here) -------------------------------- From: keith bannister Hi, I'm new to .net (as of last week) but here goes. I want to serialize/deserialize a file the conforms...
5
by: Keith Bannister | last post by:
I'm new to .net so here goes. I'm tying to deserialize a class that is associated with an XML schema. I created the C# class with xsd.exe as below: xsd.exe /c /n:somenamespace...
2
by: Dennis Ruppert | last post by:
I created a routine to read and edit the description properties of tables, (the one you see in the database window). It works just fine. This is the basic code behind it, I substituted all my...
90
by: Jhon smith | last post by:
Hi all,Just wondering are there any problems with learning c from older books,as I have picked up some from 1988,1994,1997,1998. By using books of this age(Im on a tight budget)am I going to...
0
by: Ewart MacLucas | last post by:
generated some WMI managed classes using the downloadable extensions for vs2003 from mircrosoft downloads; wrote some test code to enumerate the physicall processors and it works a treat, but a...
4
by: veerleverbr | last post by:
Suppose having define an enum like this: public enum SomeEnum { Something, SomethingElse }
4
by: Confused Newbie | last post by:
I'm converting an app written in VB 2003 to 2005 and need advice for how to deal with this situation: The app has a number of "manager" classes that handle the data access. They all have...
2
by: Ken Fine | last post by:
Hi, I have what I think is a simple type conversion problem. I am building user controls. In the VS.NET IDE I want to be able to use enumerations when assigning properties. I do this by building...
4
by: Nathan Sokalski | last post by:
I have a System.Web.UI.Control which has a property of type System.Drawing.Font. When editing this control in an *.aspx file, how do I assign a value to this property? If I recall correctly, the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.