16

Hey. I've got the following code that populates my list box

UsersListBox.DataSource = GrpList;

However, after the box is populated, the first item in the list is selected by default and the "selected index changed" event fires. How do I prevent the item from being selected right after the list box was populated, or how do I prevent the event from firing?

Thanks

6 Answers 6

24

To keep the event from firing, here are two options I have used in the past:

  1. Unregister the event handler while setting the DataSource.

    UsersListBox.SelectedIndexChanged -= UsersListBox_SelectedIndexChanged;
    UsersListBox.DataSource = GrpList;
    UsersListBox.SelectedIndex = -1; // This optional line keeps the first item from being selected.
    UsersListBox.SelectedIndexChanged += UsersListBox_SelectedIndexChanged;
    
  2. Create a boolean flag to ignore the event.

    private bool ignoreSelectedIndexChanged;
    private void UsersListBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ignoreSelectedIndexChanged) return;
        ...
    }
    ...
    ignoreSelectedIndexChanged = true;
    UsersListBox.DataSource = GrpList;
    UsersListBox.SelectedIndex = -1; // This optional line keeps the first item from being selected.
    ignoreSelectedIndexChanged = false;
    
5

Well, it looks like that the first element is automatically selected after ListBox.DataSource is set. Other solutions are good, but they doesn't resolve the problem. This is how I did resolve the problem:

// Get the current selection mode
SelectionMode selectionMode = yourListBox.SelectionMode;

// Set the selection mode to none
yourListBox.SelectionMode = SelectionMode.None;

// Set a new DataSource
yourListBox.DataSource = yourList;

// Set back the original selection mode
yourListBox.SelectionMode = selectionMode;
0
1

i using the following, seems to work for me:

List<myClass> selectedItemsList = dataFromSomewhere

//Check if the selectedItemsList and listBox both contain items
if ((selectedItemsList.Count > 0) && (listBox.Items.Count > 0))
{
   //If selectedItemsList does not contain the selected item at 
   //index 0 of the listBox then deselect it
   if (!selectedItemsList.Contains(listBox.Items[0] as myClass))
   {
      //Detach the event so it is not called again when changing the selection
      //otherwise you will get a Stack Overflow Exception
      listBox.SelectedIndexChanged -= listBox_SelectedIndexChanged;
      listBox.SetSelected(0, false);
      listBox.SelectedIndexChanged += listBox_SelectedIndexChanged;
   }
}
1

set IsSynchronizedWithCurrentItem="False" and Also SelectedIndex=-1 and every thing should work for you

1
  • This is WPF. The OP was asking about Winforms.
    – amonroejj
    Sep 28, 2023 at 18:32
-2

If you just want to clear the selected value, you can use ClearSelected after you set the DataSource. But if you dont want the event to fire, then you'll have to use one of Joseph's methods.

1
  • This is not a relevant answer.
    – Sellorio
    Jun 14, 2013 at 5:53
-4

Perhaps in DataSourceChanged you could check the state of SelectedIndex, if your lucky you could then just force SelectedIndex = -1.

1
  • Say what? If your lucky? And I can't even understand what your answer is actually saying.
    – Sellorio
    Jun 14, 2013 at 5:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.