Geeks With Blogs
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 : 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)

            {

 

            }

        }

 

 

 

 

 

Posted on Friday, March 2, 2012 12:09 PM | 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: GeeksWithBlogs.net | Join free