Control.ControlCollection.Add 方法

将指定的控件添加到控件集合中。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
Public Overridable Sub Add ( _
    value As Control _
)
用法
Dim instance As ControlCollection
Dim value As Control

instance.Add(value)
public virtual void Add (
    Control value
)
public:
virtual void Add (
    Control^ value
)
public void Add (
    Control value
)
public function Add (
    value : Control
)

参数

  • value
    要添加到控件集合的 Control

异常

异常类型 条件

Exception

指定的控件是顶级控件,或者如果将该控件添加到控件集合中,将导致循环控件引用。

ArgumentException

分配到 value 参数的对象不是 Control

备注

Add 方法使您能够将 Control 对象添加到控件集合的结尾。

还可以使用 AddRange 方法,向集合添加新的 Control 对象。

若要移除以前添加的 Control,请使用 RemoveRemoveAtClear 方法。

提示

Control 一次只能指定给一个 Control.ControlCollection。如果 Control 已经是另一个控件的子控件,那么在将它添加到另一个控件之前,应将它从该控件中移除。

给继承者的说明 在派生类中重写 Add 时,要确保调用基类的 Add 方法,以保证将控件添加到集合中。

示例

下面的代码示例将一个 Control 添加到派生类 PanelControl.ControlCollection 中。该示例要求已在 Form 上创建了一个 Panel 控件和一个 Button 控件。当单击该按钮时,将把 TextBox 控件添加到面板的 Control.ControlCollection 中。

' 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
// 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.
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.get_Controls().Add(textBox1);
} //addButton_Click

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

Control.ControlCollection 类
Control.ControlCollection 成员
System.Windows.Forms 命名空间
Remove
RemoveAt
AddRange