ListBox.GetSelected 方法

返回一个值,该值指示是否选定了指定的项。

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

语法

声明
Public Function GetSelected ( _
    index As Integer _
) As Boolean
用法
Dim instance As ListBox
Dim index As Integer
Dim returnValue As Boolean

returnValue = instance.GetSelected(index)
public bool GetSelected (
    int index
)
public:
bool GetSelected (
    int index
)
public boolean GetSelected (
    int index
)
public function GetSelected (
    index : int
) : boolean

参数

  • index
    项的从零开始的索引,根据该索引确定该项是否被选定。

返回值

如果当前在 ListBox 中选定了指定的项,则为 true;否则为 false

异常

异常类型 条件

ArgumentOutOfRangeException

index 参数或者小于零,或者大于或等于 ListBox.ObjectCollection 类的 Count 属性的值。

备注

可以使用此方法快速确定是否选定了指定的项。如果选定多重选择 ListBox 中的特定项时需要执行特定操作,则此方法很有用。

示例

下面的代码示例演示如何使用 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 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

请参见

参考

ListBox 类
ListBox 成员
System.Windows.Forms 命名空间