Click here to Skip to main content
16,019,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi team,
below is my code for adding 2 combo-boxes in my c# winform on a button's click event:

C#
private void button1_Click(object sender, EventArgs e)
        {
           // creatXtraCombo();
            ComboBox cmb1 = new ComboBox();
            ComboBox cmb2 = new ComboBox();
            //Button bt1 = new Button();
            //bt1.Text = "More ?";
            //bt1.Click = new EventHandler(bt1_click);
            cmb1.Size = new System.Drawing.Size(200, 21);
            cmb2.Size = new System.Drawing.Size(200, 21);

            cmb1.Location = new Point(cmbAccessories.Location.X, cmbAccessories.Location.Y + 25);
            cmb2.Location = new Point(cmbAccessories.Location.X, cmbAccessories.Location.Y + 50);
            cmb1.BackColor = Color.Pink;
            cmb2.BackColor = Color.Pink;
            cmb1.Text = "Please Select Accessories if Any";
            cmb2.Text = "Please Select Accessories if Any";


            Program.connect2server();
            MySqlCommand cmd6 = new MySqlCommand("select * from accessories", Program.con);
            MySqlDataReader dr6 = cmd6.ExecuteReader();
            while (dr6.Read())
            {
                cmb1.Items.Add(dr6[1].ToString());
                cmb2.Items.Add(dr6[1].ToString());
            }
            Program.con.Close();

            cmb1.SelectedIndexChanged += new EventHandler(cmb1_SelectedIndexChanged);
            groupBox3.Controls.Add(cmb1);
            cmb2.SelectedIndexChanged += new EventHandler(cmb2_SelectedIndexChanged);
            groupBox3.Controls.Add(cmb2);
            //groupBox3.Controls.Add(bt1);
            button1.Visible = false;

        }


and this is my dynamic event for combo-box i.e
C#
cmb1
which i have used as object above.

C#
private void cmb1_SelectedIndexChanged(object sender, EventArgs e)
        {
            MessageBox.Show(cmb1.Text);
        }


my problem is whenever i select any value from combo-box cmb1 i should get its value in messagebox but it is showing error saying:
C#
The name 'cmb1' does not exist in the current context


please help me in this regard and wish you happy new year to all coders here.
Posted
Comments
dev_assault 30-Dec-14 7:48am    
declare your combo boxes globally.....otherwise you are doin it correct

like
class A
{
here you declare and create the events in some method or in the constructor and release the events when no object references them
}
OriginalGriff 30-Dec-14 8:00am    
No. There is no need to, and particularly when you create them in a button click handler: if you "globalize" them to the form, then what happens when the user presses the button twice?
dev_assault 30-Dec-14 8:58am    
i feel nothing would happen, i mean that seems to be the solution......or please enlighten me with a better approach...
OriginalGriff 30-Dec-14 9:48am    
Think about it: the first time you click it, you get a combobox, store it in the class level variable, and the event handler works.
Then the user presses it again. You add a second combobox (so the user can now see two) and store it in the class level variable - overwriting the first combo. When the SelectIndexChanged event happens, you don't know if it came from the first or second combobox, and you can't access the first one via the class level variable.

So how do you get at the "right" combobox?

The solution is to not use a class level "global" variable, but to use the sender argument of the event handler method - which contains the instance of the control which raised the event.
dev_assault 31-Dec-14 1:18am    
what i meant , is that u make cmb1 global.....which means u declare the those combo boxes at class level or global or whatever.....

The problem is that you declare the variables 'cmb1 and'cmb2 inside the scope (context) of a method, the Button Click EventHandler; outside of that method, therefore, the instances of the ComboBoxes you created do not exist (well, they exist, in a way, until they are garbage collected automatically).

Just move the declaration of your ComboBoxes out of the method, and place them in the scope (context) of the Form:
C#
ComboBox cmb1 = new ComboBox();
ComboBox cmb2 = new ComboBox();

private void button1_Click(object sender, EventArgs e)
{
    // ...
}
 
Share this answer
 
Change your handler:
C#
private void cmb1_SelectedIndexChanged(object sender, EventArgs e)
        {
        ComboBox cmb = sender as ComboBox;
        if (cmb != null)
            {
            MessageBox.Show(cmb.Text);
            }
        }


The variable cmb1 is local to the button click handler, and goes out of scope at the end of the method.
The sender parameter is always the class instance that signalled the event - in this case the combobox.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900