Dear all,

I have a form with 36 comboboxes, and I use this fucntion to update them:

private void controlBox(string boxName)
        {
            foreach (Control frmControl in this.Controls)
            {
                if (frmControl is ComboBox)
                {
                    if (frmControl.Name == boxName)
                    {
                        ComboBox _bx = (ComboBox)frmControl;
                        _bx.Items.Clear();
                        _bx.Enabled = true;
                        _bx.Items.Add("It Works!");
                    }
                }
            }
        }

My question is, what is the best way to pass the boxName string to this funtion when a box has a selected item.

Let me explain better, when I select an item on box1, it should call the funtcion controlBox("box2"), so it will enable box2 and add the items I want (it will be an query associated with the selected value from box1).

+-------+-------+-------+
| box1 | box2 | box3 |.....


But my problem is, I have 36 comboboxes, so if I use the simplest way I´ll have 36 events handler like:
this.Indice11.SelectedIndexChanged += new System.EventHandler(this.Indice11_SelectedIndexChanged);

and a function Indice11_SelectedIndexChanged calling controlBox(....) .

Is there any kind of event that detects when a combobox on the form is changed and gets it´s name? or I´ll have to do the 36 events handlers?

Thanks,

Fernando França

Recommended Answers

All 2 Replies

hi there.

all you need to do is to create an eventhandler for one of the comboboxes and use that same one for the others (search for the event and select from the dropdown list the combobox_selectedindexchange eventhander). Then you can do something like this to get it's name (you need to cast the sender object to a Combobox and get it's name):

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox cb = sender as ComboBox;
            string cbName = cb.Name;

            foreach (Control frmControl in this.Controls)
            {
                if (frmControl is ComboBox)
                {
                    if (frmControl.Name == cbName) label1.Text = cbName;

                }
            }
        }

This should work :) I hope it helped!

Thanks YOU!

This really makes my code better =)

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.