ListBox.ClearSelected 方法

定义

取消选择 ListBox 中的所有项。

public:
 void ClearSelected();
public void ClearSelected ();
member this.ClearSelected : unit -> unit
Public Sub ClearSelected ()

示例

下面的代码示例演示如何将 属性与 TopIndex 属性结合使用SelectedIndex,将当前所选项移到 的显示区域中项列表的ListBox顶部。 该示例进一步演示如何使用 RemoveAt 类的 System.Windows.Forms.ListBox.ObjectCollection 方法删除项,以及如何使用 方法清除所有项选择 ClearSelected 。 该代码首先将 中 ListBox 当前选定的项移动到列表顶部。 然后,该代码删除当前所选项之前的所有项,并清除 中的所有 ListBox选择。 此示例要求 ListBox 将包含项添加到窗体中,并且当前在 中选择 ListBox一项。

private:
   void RemoveTopItems()
   {
      // Determine if the currently selected item in the ListBox 
      // is the item displayed at the top in the ListBox.
      if ( listBox1->TopIndex != listBox1->SelectedIndex )

      // Make the currently selected item the top item in the ListBox.
      listBox1->TopIndex = listBox1->SelectedIndex;

      // Remove all items before the top item in the ListBox.
      for ( int x = (listBox1->SelectedIndex - 1); x >= 0; x-- )
      {
         listBox1->Items->RemoveAt( x );
      }

      // Clear all selections in the ListBox.
      listBox1->ClearSelected();
   }
private void RemoveTopItems()
{
   // Determine if the currently selected item in the ListBox 
   // is the item displayed at the top in the ListBox.
   if (listBox1.TopIndex != listBox1.SelectedIndex)
      // Make the currently selected item the top item in the ListBox.
      listBox1.TopIndex = listBox1.SelectedIndex;

   // Remove all items before the top item in the ListBox.
   for (int x = (listBox1.SelectedIndex -1); x >= 0; x--)
   {
      listBox1.Items.RemoveAt(x);
   }

   // Clear all selections in the ListBox.
   listBox1.ClearSelected();
}
Private Sub RemoveTopItems()
   ' Determine if the currently selected item in the ListBox 
   ' is the item displayed at the top in the ListBox.
   If listBox1.TopIndex <> listBox1.SelectedIndex Then
      ' Make the currently selected item the top item in the ListBox.
      listBox1.TopIndex = listBox1.SelectedIndex
   End If
   ' Remove all items before the top item in the ListBox.
   Dim x As Integer
   For x = listBox1.SelectedIndex - 1 To 0 Step -1
      listBox1.Items.RemoveAt(x)
   Next x

   ' Clear all selections in the ListBox.
   listBox1.ClearSelected()
End Sub

注解

调用此方法等效于将 SelectedIndex 属性设置为负 1 (-1) 。 可以使用此方法快速取消选择列表中的所有项。

适用于

另请参阅