Form.ControlCollection.Add(Control) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将控件添加到窗体中。
public:
override void Add(System::Windows::Forms::Control ^ value);
public override void Add (System.Windows.Forms.Control value);
public override void Add (System.Windows.Forms.Control? value);
override this.Add : System.Windows.Forms.Control -> unit
Public Overrides Sub Add (value As Control)
参数
例外
不能向多文档界面 (MDI) 父窗体添加控件。
示例
下面的代码示例将一个 TextBox 和 Label 控件添加到窗体的控件集合中。 该示例要求已创建和命名 Form1
窗体。
public:
void AddMyControls()
{
TextBox^ textBox1 = gcnew TextBox;
Label^ label1 = gcnew Label;
// Initialize the controls and their bounds.
label1->Text = "First Name";
label1->Location = Point( 48, 48 );
label1->Size = System::Drawing::Size( 104, 16 );
textBox1->Text = "";
textBox1->Location = Point(48,64);
textBox1->Size = System::Drawing::Size( 104, 16 );
// Add the TextBox control to the form's control collection.
Controls->Add( textBox1 );
// Add the Label control to the form's control collection.
Controls->Add( label1 );
}
public void AddMyControls()
{
TextBox textBox1 = new TextBox();
Label label1 = new Label();
// Initialize the controls and their bounds.
label1.Text = "First Name";
label1.Location = new Point(48,48);
label1.Size = new Size (104, 16);
textBox1.Text = "";
textBox1.Location = new Point(48, 64);
textBox1.Size = new Size(104,16);
// Add the TextBox control to the form's control collection.
Controls.Add(textBox1);
// Add the Label control to the form's control collection.
Controls.Add(label1);
}
Public Sub AddMyControls()
Dim textBox1 As New TextBox()
Dim label1 As New Label()
' Initialize the controls and their bounds.
label1.Text = "First Name"
label1.Location = New Point(48, 48)
label1.Size = New Size(104, 16)
textBox1.Text = ""
textBox1.Location = New Point(48, 64)
textBox1.Size = New Size(104, 16)
' Add the TextBox control to the form's control collection.
Controls.Add(textBox1)
' Add the Label control to the form's control collection.
Controls.Add(label1)
End Sub
注解
可以使用此方法将控件添加到窗体。 如果要将一组已创建的控件添加到窗体中,请使用 Control.ControlCollection.AddRange 类的方法 Control.ControlCollection 。