Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello
In form1, i added comboBox1 that has 3 items (10, 20 and 30) and a textBox1
My goal is to copy the selected item from comboBox to the textBox1 by using SelectedIndexChanged (usi.ng the mouse), the program works fine
However, when the program start the SelectedIndexChanged event is called from somewhere else. I tried to avoid this undesirable call by 
adding a variable Flag and make it equal zero in formload and in SelectedIndexChanged event I added if else statement hopping
skip that call ..I could not


What I have tried:

   private void comboSelctIndChanged(object sender, EventArgs e)
        {
//in formload i added variable Flag=0 and added all items to combobox
            if (Flag == 0)
            {
                MessageBox.Show("Flag=", Flag.ToString());
                Flag = 1;
                MessageBox.Show("Now Flag=", Flag.ToString());
            }
            else
            {
                MessageBox.Show("Flag=", Flag.ToString());
                ComboBox senderComboBox = (ComboBox)sender;
                MessageBox.Show("sender ", senderComboBox.Text);
                textBox1.Text = senderComboBox.Text;               
            }
        }
Posted
Updated 30-Oct-19 0:09am

1 solution

Maybe your comboSelctIndChanged() fires for each added item when the comboBox is initialized.
You can set a Flag after
InitializeComponent();

However normally this should not occur, so search your code where the comboBox is being used and see what causes this strange behaviour.

If you can't find the problem, here is a dirty trick: you can delete the line from the form .Designer.cs
this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
And put it in your code after you loaded all items.
 
Share this answer
 
v3
Comments
Engineer khalid 30-Oct-19 8:01am    
thank you very much

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