ListBox and Datasource - prevent first item from being selected

≯℡__Kan透↙ 提交于 2019-12-30 08:06:26

问题


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


回答1:


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;
    



回答2:


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;



回答3:


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;
   }
}



回答4:


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




回答5:


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.




回答6:


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



来源:https://stackoverflow.com/questions/2975635/listbox-and-datasource-prevent-first-item-from-being-selected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!