473,387 Members | 1,882 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,387 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 1351
> 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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.