Control.ControlCollection.RemoveAt(Int32) 方法


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

 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)



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



下面的代码示例将删除派生 @no__t 类的 @no__t 中的第一个 Control,如果集合的计数大于零,则为-2。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. 该示例要求您已在 @no__t 2 上创建了一个 Panel、一个 Button 和至少一个其他控件。The example requires that you have created a Panel, a Button, and at least one other control on a Form. 将其他控件添加到 @no__t 0 控件,并将 @no__t 1 控件添加到 @no__t。The other controls are added to the Panel control, and the Panel control added to the Form. 单击该按钮时,面板中包含的第一个控件将从 @no__t 中移除。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.
   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)
' 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
    End If
End Sub


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

你还可以使用 @no__t 或 @no__t 2 方法删除以前添加的 @no__t 0。You can also remove a Control that you previously added by using the Remove or Clear methods.

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