【问题标题】:ListBox and Datasource - prevent first item from being selectedListBox 和 Datasource - 防止第一个项目被选中
【发布时间】:2016-08-19 19:31:00
【问题描述】:

嘿。我有以下代码填充我的列表框

UsersListBox.DataSource = GrpList;

但是,填充框后,默认选择列表中的第一项,并触发“选定的索引已更改”事件。如何防止在填充列表框后立即选择项目,或者如何防止触发事件?

谢谢

【问题讨论】:

    标签: c# winforms listbox


    【解决方案1】:

    为了防止事件触发,这里有两个我过去使用过的选项:

    1. 在设置 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. 创建一个布尔标志以忽略该事件。

      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】:

      嗯,看起来第一个元素是在设置 ListBox.DataSource 之后自动选择的。其他解决方案很好,但它们并不能解决问题。这就是我解决问题的方法:

      // 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】:

        我使用以下,似乎对我有用:

        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】:

          设置IsSynchronizedWithCurrentItem="False"SelectedIndex=-1,一切都应该适合你

          【讨论】:

            【解决方案5】:

            如果只是想清除选中的值,可以在设置DataSource后使用ClearSelected。但如果您不想触发该事件,则必须使用 Joseph 的一种方法。

            【讨论】:

            • 这不是一个相关的答案。
            【解决方案6】:

            也许在 DataSourceChanged 中,您可以检查 SelectedIndex 的状态,如果幸运的话,您可以强制 SelectedIndex = -1。

            【讨论】:

            • 说什么?如果你幸运?而且我什至无法理解您的回答实际上在说什么。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2021-09-21
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2022-01-09
            相关资源
            最近更新 更多