共用方式為


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)。 你可以用這個方法快速取消選取清單中的所有項目。

適用於

另請參閱