ListBox.ClearSelected Метод
Определение
Важно!
Некоторые сведения относятся к предварительной версии продукта, в которую до выпуска могут быть внесены существенные изменения. Майкрософт не предоставляет никаких гарантий, явных или подразумеваемых, относительно приведенных здесь сведений.
Отменяет выбор всех элементов в элементе ListBox.
public:
void ClearSelected();
public void ClearSelected();
member this.ClearSelected : unit -> unit
Public Sub ClearSelected ()
Примеры
В следующем примере кода показано, как использовать SelectedIndex свойство со TopIndex свойством для перемещения текущего выбранного элемента в начало списка элементов в области 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). Этот метод можно использовать для быстрого отмены выбора всех элементов в списке.