ListBox.ObjectCollection.RemoveAt(Int32) Метод
Определение
Важно!
Некоторые сведения относятся к предварительной версии продукта, в которую до выпуска могут быть внесены существенные изменения. Майкрософт не предоставляет никаких гарантий, явных или подразумеваемых, относительно приведенных здесь сведений.
Удаляет элемент по указанному индексу в коллекции.
public:
virtual void RemoveAt(int index);
public void RemoveAt(int index);
abstract member RemoveAt : int -> unit
override this.RemoveAt : int -> unit
Public Sub RemoveAt (index As Integer)
Параметры
- index
- Int32
Отсчитываемый от нуля индекс элемента.
Реализации
Исключения
Параметр index меньше нуля или больше или равен значению Count свойства ListBox.ObjectCollection класса.
Примеры
В следующем примере кода показано, как использовать 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
Комментарии
При удалении элемента из списка индексы изменяются для последующих элементов в списке. Удаляется все сведения об удаленном элементе. Этот метод можно использовать для удаления определенного элемента из списка, указав индекс элемента для удаления из списка. Чтобы указать элемент, который нужно удалить вместо индекса, используйте Remove метод. Чтобы удалить все элементы из списка, используйте Clear метод.