Control.ControlCollection.RemoveAt(Int32) 方法

定义

在指定的索引位置从控件集合移除控件。Removes a control from the control collection at the specified indexed location.

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

要移除的 Control 的索引值。The index value of the Control to remove.

实现

示例

下面的代码示例将删除派生类的 Control.ControlCollection 中的第一个 Control Panel 如果集合的计数大于零,则为。The following code example removes the first Control in the Control.ControlCollection of the derived class Panel if the count of the collection is greater than zero. 该示例要求您已在 Form上创建了一个 Panel、一个 Button和至少一个其他控件。The example requires that you have created a Panel, a Button, and at least one other control on a Form. 其他控件将添加到 Panel 控件,并将 Panel 控件添加到 Form中。The other controls are added to the Panel control, and the Panel control added to the Form. 单击该按钮时,面板中包含的第一个控件将从 Control.ControlCollection中移除。When the button is clicked, the first control contained in the panel is removed from the Control.ControlCollection.

   // 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 );
      }
   }
// 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);
   }
}
' Remove the first control in the collection.
Private Sub RemoveAtButton_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles RemoveAtButton.Click
    If (Panel1.Controls.Count > 0) Then
        Panel1.Controls.RemoveAt(0)
    End If
End Sub

注解

从控件集合中移除 Control 时,所有后续控件都将在集合中上移一个位置。When a Control is removed from the control collection, all subsequent controls are moved up one position in the collection.

你还可以使用 RemoveClear 方法删除之前添加的 ControlYou can also remove a Control that you previously added by using the Remove or Clear methods.

若要向集合添加新的 Control 对象,请使用 AddAddRange 方法。To add new Control objects to the collection, use the Add or AddRange methods.

适用于

另请参阅