Export (0) Print
Expand All
1 out of 1 rated this helpful - Rate this topic

ListBox.ObjectCollection.RemoveAt Method

Removes the item at the specified index within the collection.

[Visual Basic]
Public Overridable Sub RemoveAt( _
   ByVal index As Integer _
) Implements IList.RemoveAt
[C#]
public virtual void RemoveAt(
 int index
);
[C++]
public: virtual void RemoveAt(
 int index
);
[JScript]
public function RemoveAt(
   index : int
);

Parameters

index
The zero-based index of the item to remove.

Implements

IList.RemoveAt

Exceptions

Exception Type Condition
ArgumentOutOfRangeException The index parameter is less than zero or greater than or equal to the value of the Count property of the ListBox.ObjectCollection class.

Remarks

When you remove an item from the list, the indexes change for subsequent items in the list. All information about the removed item is deleted. You can use this method to remove a specific item from the list by specifying the index of the item to remove from the list. To specify the item to remove instead of the index to the item, use the Remove method. To remove all items from the list, use the Clear method.

Example

[Visual Basic, C#, C++] The following example demonstrates how to use the SelectedIndex property with the TopIndex property to move the currently selected item to the top of the list of items in the display area of the ListBox. The example further demonstrates how to remove items using the RemoveAt method of the System.Windows.Forms.ListBox.ObjectCollection class, and how to clear all item selection using the ClearSelected method. The code first moves the currently selected item in the ListBox to the top of the list. The code then removes all items before the currently selected item and clears all selections in the ListBox. This example assumes that a ListBox containing items is added to a form and that an item is currently selected in the ListBox.

[Visual Basic] 
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 'RemoveTopItems

[C#] 
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();
}

[C++] 
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();
   }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

ListBox.ObjectCollection Class | ListBox.ObjectCollection Members | System.Windows.Forms Namespace | Clear | Remove

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.