0

Below is my code , List_sheet_name is a combo box

 List_sheet_name.Items.Clear();
            List_sheet_name.Items.Insert(0,"Select a table from Sheet");
            for (int i = 0; i < Sheets.Rows.Count; i++)
            {
                List_sheet_name.Items.Add(Sheets.Rows[i]["TABLE_NAME"].ToString());
            }
            List_sheet_name.SelectedIndex = 0;
            //List_sheet_name.SelectedIndex = List_sheet_name.FindStringExact("Select a table from Sheet");

After setting the SelectIndex to zero (in last two lines) it is automatically invoking the SelectedIndexChanged event too. Can someone tell me why this is happening ?

4
  • What are you trying to achieve? Commented Mar 20, 2017 at 10:43
  • An easy task - setting the "Select a table from Sheet" as default value Commented Mar 20, 2017 at 10:49
  • So, using SelectIndex is a valid option, what do you have in SelectedIndexChanged that you don't want it to be raised? Commented Mar 20, 2017 at 10:51
  • On SelectedIndexChanged my code is running to process the records , Which is getting invoked in advance by setting List_sheet_name.SelectedIndex = 0; Commented Mar 20, 2017 at 10:56

2 Answers 2

1

According to the MSDN, the event reacts to all SelectedIndex changes. If you change the index to a new value which is equal to the previous one, the event won't be fired again, you need to call it manually.

If you don't want the SelectedIndexChanged event to be invoked when setting the SelectedIndex, you should check in the SelectedIndexChanged event whether the sender object (which is the ComboBox) is focused.

private void YourComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
    if (!(sender as ComboBox).Focused)
        return;

    // ...your code
}
Sign up to request clarification or add additional context in comments.

4 Comments

How to check whether the sender object is focused ?
You are wrong. By changing from 0 to 0 SelectedindexChanged will not be raised. To check, whether the control is focused is not reliable in this case
Thank you @Rekshino, I wasn't entirely sure about that and had to do a little research. I corrected it.
@Saurabh : If my solution satisfies your needs, please select it as an answer. I'm happy to help with any of your additional questions.:)
1

Initially, it is -1. If you set to 0, then SelectedIndexChanged will be invoked.

Each time you set it to ANOTHER value, the SelectedIndexChanged will be raised. For set from codebehind you can use a variable bool _codeBehind, set it before you change SelectedIndex and evaluate in an Event handler

private bool _cmbSelIdxIntern = false;

void YourMeth()
{
    _cmbSelIdxIntern = true;
    cmbTest.SelectedIndex = 0;
    _cmbSelIdxIntern = false;
}

private void cmbTest_SelectedIndexChanged(object sender, EventArgs e)
{
    if (_cmbSelIdxIntern)
    {
        return;
    }
}

1 Comment

Tried to set it on -1 , but it does not set the desired value

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.