SelectionMode SelectionMode SelectionMode SelectionMode Enum

定义

指定列表框的选择行为。Specifies the selection behavior of a list box.

public enum class SelectionMode
[System.Runtime.InteropServices.ComVisible(true)]
public enum SelectionMode
type SelectionMode = 
Public Enum SelectionMode
继承
SelectionModeSelectionModeSelectionModeSelectionMode
属性

字段

MultiExtended MultiExtended MultiExtended MultiExtended 3

可以选择多个项,并且用户可以使用 SHIFT、 CTRL 和箭头键进行选择Multiple items can be selected, and the user can use the SHIFT, CTRL, and arrow keys to make selections

MultiSimple MultiSimple MultiSimple MultiSimple 2

可以选择多个项。Multiple items can be selected.

None None None None 0

可以不选择任何项。No items can be selected.

One One One One 1

可以选择一项。Only one item can be selected.

示例

下面的示例演示如何使用GetSelected方法,以确定哪些项在ListBox为了选择了未选定的项,并取消选择选定的项选择。The following example demonstrates how to use the GetSelected method to determine which items in a ListBox are selected in order to select the items that are not selected and deselect the items that are selected. 该示例还演示了如何使用SelectionMode属性来启用ListBox若要有多个选定项,并使用Sorted属性来演示如何对中的项进行排序ListBox自动。The example also demonstrates using the SelectionMode property to enable a ListBox to have more than one selected item and uses the Sorted property to demonstrate how to sort items in a ListBox automatically. 此示例假定ListBox、 命名listBox1,已添加到窗体,并且InitializeMyListBox示例中定义的方法调用从Load窗体的事件。This example assumes that a ListBox, named listBox1, has been added to a form and that the InitializeMyListBox method defined in the example is called from the Load event of the form.

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

注解

此枚举如由类ListBoxCheckedListBoxThis enumeration is used by classes such as ListBox and CheckedListBox.

适用于

另请参阅