Control.ControlCollection.Remove(Control) 方法

定义

从控件集合中移除指定的控件。Removes the specified control from the control collection.

public:
 virtual void Remove(System::Windows::Forms::Control ^ value);
public virtual void Remove (System.Windows.Forms.Control value);
abstract member Remove : System.Windows.Forms.Control -> unit
override this.Remove : System.Windows.Forms.Control -> unit
Public Overridable Sub Remove (value As Control)

参数

value
Control

要从 Control 移除的 Control.ControlCollectionThe Control to remove from the Control.ControlCollection.

示例

下面的代码示例从派生类的 @no__t @no__t (如果它是集合的成员,则从 Panel)中删除一个0。The following code example removes a Control from the Control.ControlCollection of the derived class Panel if it is a member of the collection. 该示例要求您已在 @no__t 3 上创建了一个 Panel、一个 Button 和一个或多个 @no__t 2 控件。The example requires that you have created a Panel, a Button, and one or more RadioButton controls on a Form. 将 @no__t 0 个控件添加到 @no__t 控件,并将 @no__t 2 控件添加到 @no__t。The RadioButton controls are added to the Panel control, and the Panel control is added to the Form. 单击该按钮时,将从 @no__t 中删除名为 radioButton2 的单选按钮。When the button is clicked, the radio button named radioButton2 is removed from the Control.ControlCollection.

   // Remove the RadioButton control if it exists.
private:
   void removeButton_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      if ( panel1->Controls->Contains( removeButton ) )
      {
         panel1->Controls->Remove( removeButton );
      }
   }
// Remove the RadioButton control if it exists.
private void removeButton_Click(object sender, System.EventArgs e)
{
   if(panel1.Controls.Contains(removeButton))
   {
      panel1.Controls.Remove(removeButton);
   }
}
' Remove the RadioButton control if it exists.
Private Sub RemoveButton_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles RemoveButton.Click
    If Panel1.Controls.Contains(RemoveButton) Then
        Panel1.Controls.Remove(RemoveButton)
    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 方法删除 Control,或使用 @no__t 2 方法删除所有控件。You can also remove a Control by using the RemoveAt method, or remove all controls by using the Clear method.

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

继承者说明

在派生类中重写 Remove(Control) 时,请确保调用基类的 Remove(Control) 方法,以确保从集合中移除该控件。When overriding Remove(Control) in a derived class, be sure to call the base class's Remove(Control) method to ensure that the control is removed from the collection.

适用于

另请参阅