ListBox.ClearSelected 方法

定義

取消選取 ListBox 中的所有項目。

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

範例

下列程式碼範例示範如何使用 SelectedIndex 屬性搭配 TopIndex 屬性,將目前選取的專案移至 顯示區域中 ListBox 專案清單的頂端。 此範例進一步示範如何使用 類別的 System.Windows.Forms.ListBox.ObjectCollection 方法移除專案 RemoveAt ,以及如何使用 方法清除所有專案選取 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) 。 您可以使用此方法快速取消選取清單中的所有專案。

適用於

另請參閱