Control.ControlCollection.Add(Control) 方法

定义

将指定的控件添加到控件集合中。Adds the specified control to the control collection.

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

参数

value
Control

要添加到控件集合的 ControlThe Control to add to the control collection.

异常

指定的控件是顶级控件,或者如果将该控件添加到控件集合中,将导致循环控件引用。The specified control is a top-level control, or a circular control reference would result if this control were added to the control collection.

分配到 value 参数的对象不是 ControlThe object assigned to the value parameter is not a Control.

示例

下面的代码示例将 @no__t 0 添加到派生类 Panel 的 @no__t 中。The following code example adds a Control to the Control.ControlCollection of the derived class Panel. 该示例要求您已在 Form 上创建 @no__t 0 控件和 @no__t 控件。The example requires that you have created a Panel control and a Button control on a Form. 单击该按钮后,将向面板的 @no__t 中添加一个 @no__t 0 控件。When the button is clicked, a TextBox control is added to the panel's Control.ControlCollection.

   // Create a TextBox to add to the Panel.
private:
   TextBox^ textBox1;

   // Add controls to the Panel using the Add method.
   void addButton_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      textBox1 = gcnew TextBox;
      panel1->Controls->Add( textBox1 );
   }
// Create a TextBox to add to the Panel.
private TextBox textBox1 = new TextBox();

// Add controls to the Panel using the Add method.
private void addButton_Click(object sender, System.EventArgs e)
{
   panel1.Controls.Add(textBox1);
}
' Create a TextBox to add to the Panel.
Dim TextBox1 As TextBox = New TextBox()

' Add controls to the Panel using the Add method.
Private Sub AddButton_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles AddButton.Click
    Panel1.Controls.Add(TextBox1)
End Sub

注解

@No__t-0 方法允许您将 @no__t 对象添加到控件集合的末尾。The Add method allows you to add Control objects to the end of the control collection.

你还可以使用 AddRange 方法将新的 @no__t 0 对象添加到集合中。You can also add new Control objects to the collection by using the AddRange method.

若要删除先前添加的 @no__t 0,请使用 @no__t、RemoveAt 或 @no__t 3 方法。To remove a Control that you previously added, use the Remove, RemoveAt, or Clear methods.

备注

一次只能向一个 @no__t 分配一个 @no__t。A Control can only be assigned to one Control.ControlCollection at a time. 如果 @no__t 0 已是另一个控件的子控件,则会将该控件从该控件中删除,然后再将其添加到另一个控件。If the Control is already a child of another control it is removed from that control before it is added to another control.

继承者说明

在派生类中重写 Add(Control) 时,请确保调用基类的 Add(Control) 方法,以确保将控件添加到集合中。When overriding Add(Control) in a derived class, be sure to call the base class's Add(Control) method to ensure that the control is added to the collection.

适用于

另请参阅