I have a listbox that could be filled with values from one of two enums, or both of them. Basically this code:
public enum NamesA { Adam , Albert }
public enum NamesB { Bert , Bob }
List<string> nameList = new List<string>();
if (reason) nameList.AddRange(Enum.GetNames(typeof(NamesA)));
else if (reason) nameList.AddRange(Enum.GetNames(typeof(NamesB)));
else
{
nameList.AddRange(Enum.GetNames(typeof(NamesA)));
nameList.AddRange(Enum.GetNames(typeof(NamesB)));
}
foreach (string name in nameList)
listBox1.Items.Add(name);
string chosenName = listBox1.SelectedItem.ToString();
if (chosenName.Equals(NamesA.Adam.ToString()))
{ /*some stuff happens*/ }
else if (chosenName.Equals(NamesB.Bob.ToString()))
{ /*other stuff happens*/ }
/* ...and so on */
You need to leverage the integer which is the underlying data type of any enum. Have a look at how I've achieved it:
File: CustomListItem.cs
namespace WindowsFormsApplication1
{
public class CustomListItem
{
public string Text { get; set; }
public int EnumValue { get; set; }
}
}
File: Form1.cs
namespace WindowsFormsApplication1
{
public enum NamesA { Adam = 0, Albert = 1 };
public enum NamesB { Bert = 2, Bob = 3 }
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
List <CustomListItem> nameList = new List<CustomListItem>();
int reason = 1;
if (reason == 1)
foreach (NamesA item in Enum.GetValues(typeof(NamesA)))
nameList.Add(new CustomListItem { Text = item.ToString(), EnumValue = (int)item });
else if (reason == 2)
{
foreach (NamesB item in Enum.GetValues(typeof(NamesB)))
nameList.Add(new CustomListItem { Text = item.ToString(), EnumValue = (int)item });
}
else
{
foreach (NamesA item in Enum.GetValues(typeof(NamesA)))
nameList.Add(new CustomListItem { Text = item.ToString(), EnumValue = (int)item });
foreach (NamesB item in Enum.GetValues(typeof(NamesB)))
nameList.Add(new CustomListItem { Text = item.ToString(), EnumValue = (int)item });
}
listBox1.DataSource = nameList;
listBox1.DisplayMember = "Text";
listBox1.ValueMember = "EnumValue";
}
private void button_Click(object sender, EventArgs e)
{
int chosenNameEnumValue = (int) listBox1.SelectedValue;
switch (chosenNameEnumValue)
{
case (int) NamesA.Adam:
/*some stuff happens*/
break;
case (int) NamesA.Albert:
/*other stuff happens*/
break;
case (int)NamesB.Bert:
/*some stuff happens*/
break;
case (int)NamesB.Bob:
/*other stuff happens*/
break;
default:
/* ...and so on */
break;
}
}
}
}