473,217 Members | 2,129 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,217 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

Jul 21 '05 #1
9 1346
> 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
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
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
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
Thank you all.

You are lifesavers!

Thanks

Jul 21 '05 #6
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
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


"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

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...
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: 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 }
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: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.