Geeks With Blogs


Post Categories

Rodney Vinyard - .NET & SQL Developer When all is said and done, more will be said than done

Get index of an enum via string

Thanks to by serkan sendur :

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






        private void btn_Click(object sender, EventArgs e)




                HashSet<string> hashSet_Checked = new HashSet<string>();

                foreach(Control c in this.Controls)


                    if(c is CheckBox)   


                        CheckBox chk = (CheckBox)c;

                        if (chk.Checked)







//another class can work with the hashset & enum

                foreach (string chkName in hashSet_Checked)


                    chkSelectedenum flag = (chkSelectedenum)Enum.Parse(typeof(chkSelectedenum), chkName);





            catch (Exception ex)










Posted on Friday, March 2, 2012 12:09 PM WinForms | Back to top

Comments on this post: Get index of an enum via string

# re: Get index of an enum via string
Requesting Gravatar...
I can't solve this problem, i am working on large current slip rings
Left by slip ring on Jan 20, 2013 10:10 AM

Your comment:
 (will show your gravatar)

Copyright © Rodney Vinyard | Powered by: | Join free