Control.ControlCollection.RemoveAt(Int32) 方法

定义

在指定的索引位置从控件集合移除控件。

C#
public void RemoveAt(int index);

参数

index
Int32

要移除的 Control 的索引值。

实现

示例

如果集合的计数大于零,下面的代码示例将删除派生类Panel的 中的第一ControlControl.ControlCollection个 。 该示例要求你已在 上创建了一个 PanelButton、 和至少一个Form其他控件。 其他控件将添加到 控件,Panel并将Panel控件添加到 。Form 单击按钮时,面板中包含的第一个控件将从 Control.ControlCollection中删除。

C#
// Remove the first control in the collection.
private void removeAtButton_Click(object sender, System.EventArgs e)
{
   if (panel1.Controls.Count > 0)
   {
      panel1.Controls.RemoveAt(0);
   }
}

注解

Control从控件集合中删除 时,所有后续控件都将在集合中向上移动一个位置。

还可以使用 RemoveClear 方法删除Control之前添加的 。

若要向集合添加新 Control 对象,请使用 AddAddRange 方法。

适用于

产品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

另请参阅