How can I add a control to a Window Form at runtime?

To add a control at runtime, you do three steps:

1. Create the control
2. Set control properties
3. Add the control to the Forms Controls collection

Here are code snippets that create a textBox at runtime.

//step 1
TextBox tb = new TextBox();

//step2
tb.Location = new Point( 10, 10);
tb.Size = new Size(100, 20);
tb.Text = "I was created at runtime";

//step3
this.Controls.Add(tb);

Need Help? Contact Us.

Log in

*
*

Forgot password?

*

New User

*
*
*
*