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 Control.ControlCollection 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. 该示例要求你已在中创建了一个 Panel 、一个 Button 和至少一个其他控件 FormThe example requires that you have created a Panel, a Button, and at least one other control on a Form. 其他控件将添加到 Panel 控件,并将 Panel 控件添加到 FormThe other controls are added to the Panel control, and the Panel control added to the Form. 单击该按钮时,将从中移除面板中包含的第一个控件 Control.ControlCollectionWhen 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.

你还可以 Control 使用或方法删除之前添加的 Remove ClearYou 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.

适用于

另请参阅