Get index of an enum via string
Thanks to by serkan sendur : http://www.daniweb.com/software-development/csharp/code/217261
I have a bunch of checkboxes on a Windows form
and I put the exact checkbox names in an enum chkSelectedenum
I want to collect the checkbox names into a hashset
I will pass the hashset to another class that also can see chkSelectedenum
So that
1. we do not have to hardcode checkbox names into if..then.else..switch statements
2. another class can work with the hashset & enum
enum chkSelectedenum
{
Chk1,
Chk2,
Chk3
}
private void btn_Click(object sender, EventArgs e)
{
try
{
HashSet<string> hashSet_Checked = new HashSet<string>();
foreach(Control c in this.Controls)
{
if(c is CheckBox)
{
CheckBox chk = (CheckBox)c;
if (chk.Checked)
hashSet_Checked.Add(chk.Name);
}
}
}
//another class can work with the hashset & enum
foreach (string chkName in hashSet_Checked)
{
chkSelectedenum flag = (chkSelectedenum)Enum.Parse(typeof(chkSelectedenum), chkName);
Debug.WriteLine(flag.ToString());
}
}
catch (Exception ex)
{
}
}