SelectionMode 枚举 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
指定列表框的选定行为。
public enum class SelectionMode[System.Runtime.InteropServices.ComVisible(true)]
public enum SelectionModepublic enum SelectionMode[<System.Runtime.InteropServices.ComVisible(true)>]
type SelectionMode = type SelectionMode = Public Enum SelectionMode- 继承
- 属性
字段
| 名称 | 值 | 说明 | 
|---|---|---|
| MultiExtended | 3 | 可以选择多项,并且用户可使用 Shift、Ctrl 和箭头键来进行选择。 | 
| MultiSimple | 2 | 可以选择多项。 | 
| None | 0 | 无法选择项。 | 
| One | 1 | 只能选择一项。 | 
示例
下面的示例演示如何使用 GetSelected 方法确定 选择了 中的 ListBox 哪些项,以便选择未选择的项并取消选择选定的项。 该示例还演示了如何使用 SelectionMode 属性使 ListBox 具有多个选定项,并使用 Sorted 属性演示如何自动对 中的 ListBox 项进行排序。 此示例假定 ListBox已将名为 listBox1的 添加到窗体中, InitializeMyListBox 并且从 Load 窗体的 事件调用示例中定义的 方法。
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;
   }
   void InvertMySelection()
   {
      // Loop through all items the ListBox.
      for ( int x = 0; x < listBox1->Items->Count; x++ )
      {
         // Select all items that are not selected,
         // deselect all items that are selected.
         listBox1->SetSelected( x,  !listBox1->GetSelected( x ) );
      }
      listBox1->TopIndex = 0;
   }
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;
}
Private Sub 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
End Sub
Private Sub InvertMySelection()
   Dim x As Integer
   ' Loop through all items the ListBox.
   For x = 0 To listBox1.Items.Count - 1
      ' Determine if the item is selected.
      If listBox1.GetSelected(x) = True Then
         ' Deselect all items that are selected.
         listBox1.SetSelected(x, False)
      Else
         ' Select all items that are not selected.
         listBox1.SetSelected(x, True)
      End If
   Next x
   ' Force the ListBox to scroll back to the top of the list.
   listBox1.TopIndex = 0
End Sub
注解
此枚举由 和 CheckedListBox等ListBox类使用。