ListBox.ObjectCollection.RemoveAt 方法

此方法支持 .NET 基础结构,但不适合在代码中直接使用。

移除集合中指定索引处的项。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
Public Sub RemoveAt ( _
    index As Integer _
)
用法
Dim instance As ObjectCollection
Dim index As Integer

instance.RemoveAt(index)
public void RemoveAt (
    int index
)
public:
virtual void RemoveAt (
    int index
) sealed
public final void RemoveAt (
    int index
)
public final function RemoveAt (
    index : int
)

参数

  • index
    从零开始的索引(属于要移除的项)。

异常

异常类型 条件

ArgumentOutOfRangeException

index 参数或者小于零,或者大于或等于 ListBox.ObjectCollection 类的 Count 属性的值。

备注

从列表中移除项时,列表中后面的项的索引将发生变化。有关移除的项的所有信息均会被删除。可以使用此方法通过指定要从列表中移除的项的索引来从列表中移除特定项。要指定要移除的项而不是该项的索引,请使用 Remove 方法。若要从列表中移除所有项,请使用 Clear 方法。

示例

下面的代码示例演示如何使用 SelectedIndex 属性和 TopIndex 属性,将当前选定的项移到 ListBox 显示区域中的项列表的顶部。该示例还演示了如何使用 System.Windows.Forms.ListBox.ObjectCollection 类的 RemoveAt 方法移除项以及如何使用 ClearSelected 方法清除所有选择的项。这段代码首先将 ListBox 中当前选定的项移到列表的顶部。然后移除当前选定的项之前的所有项并清除 ListBox 中的所有选择。此示例要求已将包含项的 ListBox 添加到窗体,而且当前已在 ListBox 中选择了一个项。

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
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 void RemoveTopItems()
{
    // Determine if the currently selected item in the ListBox 
    // is the item displayed at the top in the ListBox.
    if (listBox1.get_TopIndex() != listBox1.get_SelectedIndex()) {
        // Make the currently selected item the top item in the ListBox.
        listBox1.set_TopIndex(listBox1.get_SelectedIndex());
    }
    // Remove all items before the top item in the ListBox.
    for (int x = listBox1.get_SelectedIndex() - 1; x >= 0; x--) {
        listBox1.get_Items().RemoveAt(x);
    }
    // Clear all selections in the ListBox.
    listBox1.ClearSelected();
} //RemoveTopItems

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

ListBox.ObjectCollection 类
ListBox.ObjectCollection 成员
System.Windows.Forms 命名空间
Clear
Remove