I have 84 combo box inside 12 group box, in a group of 12. Combo Box are named as cb1,cb2,cb3,cb4,cb5,cb6,...... Group Box are named as gBox1,gBox2,gBox3,gBox4,gBox5,.... Now I want to save the item name in a variable when I select or change item of any combo box. I know that if I use SelectedIndexChanged for each combo box then I can get what I want, I am wondering that is the only way to do.... OR there is any simplified way to achieve the above mentioned purpose.

I welcome any suggestion/advice/guidance.

Regards to all

Recommended Answers

All 2 Replies

That's the only way to do it as far as I know.

You can create just one handler method though and have all the controls subscribe to the same one so you don't have to make 84 handlers. That is assuming they all do the same thing and you can achieve what you are after with one method.

You can also cast the sender object to see which control it came from.

Also the control will have a Parent property if you need to know which groupbox it is in.

If your controls are added at design time, a couple of nested for each loops will allow you to add the same handler to each combobox.

foreach GroupBox gb in Me.Controls.OfType<GroupBox>()
{
    foreach Combobox cb in gb.Control.OfType<Combobox>()
    {
        cb.Click += new System.EventHandler(MyCBClickHandler);
    }
}

Did this on the fly, some of the syntax might not be 100% right but you should get the idea.

commented: Excellent! I was going to mention the same, but got lazy :P +5
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.