SelectionMode 枚举

定义

指定列表框的选定行为。

C#
[System.Runtime.InteropServices.ComVisible(true)]
public enum SelectionMode
C#
public enum SelectionMode
继承
SelectionMode
属性

字段

名称 说明
MultiExtended 3

可以选择多项,并且用户可使用 Shift、Ctrl 和箭头键来进行选择。

MultiSimple 2

可以选择多项。

None 0

无法选择项。

One 1

只能选择一项。

示例

下面的示例演示如何使用 GetSelected 方法确定 选择了 中的 ListBox 哪些项,以便选择未选择的项并取消选择选定的项。 该示例还演示了如何使用 SelectionMode 属性使 ListBox 具有多个选定项,并使用 Sorted 属性演示如何自动对 中的 ListBox 项进行排序。 此示例假定 ListBox已将名为 listBox1的 添加到窗体中, InitializeMyListBox 并且从 Load 窗体的 事件调用示例中定义的 方法。

C#
private void InitializeMyListBox()
{
   // Add items to the ListBox.
   listBox1.Items.Add("A");
   listBox1.Items.Add("C");
   listBox1.Items.Add("E");
   listBox1.Items.Add("F");
   listBox1.Items.Add("G");
   listBox1.Items.Add("D");
   listBox1.Items.Add("B");

   // Sort all items added previously.
   listBox1.Sorted = true;

   // Set the SelectionMode to select multiple items.
   listBox1.SelectionMode = SelectionMode.MultiExtended;

   // Select three initial items from the list.
   listBox1.SetSelected(0,true);
   listBox1.SetSelected(2,true);
   listBox1.SetSelected(4,true);

   // Force the ListBox to scroll back to the top of the list.
   listBox1.TopIndex=0;
}

private void InvertMySelection()
{
   // Loop through all items the ListBox.
   for (int x = 0; x < listBox1.Items.Count; x++)
   {
      // Determine if the item is selected.
      if(listBox1.GetSelected(x) == true)
         // Deselect all items that are selected.
         listBox1.SetSelected(x,false);      
      else
         // Select all items that are not selected.
         listBox1.SetSelected(x,true);
   }
   // Force the ListBox to scroll back to the top of the list.
   listBox1.TopIndex=0;
}

注解

此枚举由 和 CheckedListBoxListBox类使用。

适用于

产品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

另请参阅