SelectionMode 枚举

指定列表框的选定行为。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
<ComVisibleAttribute(True)> _
Public Enumeration SelectionMode
用法
Dim instance As SelectionMode
[ComVisibleAttribute(true)] 
public enum SelectionMode
[ComVisibleAttribute(true)] 
public enum class SelectionMode
/** @attribute ComVisibleAttribute(true) */ 
public enum SelectionMode
ComVisibleAttribute(true) 
public enum SelectionMode

成员

  成员名称 说明
MultiExtended 可以选择多项,并且用户可使用 Shift 键、Ctrl 键和箭头键来进行选择。  
MultiSimple 可以选择多项。 
None 无法选择项。 
One 只能选择一项。 

备注

此枚举由类(如 ListBoxCheckedListBox)使用。

示例

下面的示例演示了如何使用 GetSelected 方法来确定已选择了 ListBox 中的哪些项,以便能够选择那些尚未被选择的项和撤销那些已选择的项。该示例还演示了如何使用 SelectionMode 属性以使 ListBox 能够有多个选定的项,并使用 Sorted 属性演示了如何对 ListBox 中的项进行自动排序。本示例假定已将名为 listBox1ListBox 添加到窗体,并假定从该窗体的 Load 事件调用在示例中定义的 InitializeMyListBox 方法。

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
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:
   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.get_Items().Add("A");
    listBox1.get_Items().Add("C");
    listBox1.get_Items().Add("E");
    listBox1.get_Items().Add("F");
    listBox1.get_Items().Add("G");
    listBox1.get_Items().Add("D");
    listBox1.get_Items().Add("B");

    // Sort all items added previously.
    listBox1.set_Sorted(true);

    // Set the SelectionMode to select multiple items.
    listBox1.set_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.set_TopIndex(0);
} //InitializeMyListBox

private void InvertMySelection()
{
    // Loop through all items the ListBox.
    for (int x = 0; x < listBox1.get_Items().get_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.set_TopIndex(0);
} //InvertMySelection

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

System.Windows.Forms 命名空间
ListBox 类
CheckedListBox 类