Share via


How to: Add Windows Forms Controls at Run Time

This example creates a new instance of a Windows Forms TextBox control at runtime.

Example

private void Form1_Load(object sender, System.EventArgs e)
{
    TextBox MyTextBox = new System.Windows.Forms.TextBox();
    MyTextBox.Location = new System.Drawing.Point(64, 40);
    MyTextBox.Name = "MyTextBox";
    MyTextBox.Size = new System.Drawing.Size(88, 21);
    MyTextBox.TabIndex = 0;
    MyTextBox.Text = "MyTextBox";
    Controls.Add(MyTextBox);

    // Adding the DoubleClick event.
    MyTextBox.DoubleClick += new EventHandler(MyTextBox_DoubleClick);
}
private void MyTextBox_DoubleClick(object sender, System.EventArgs e)
{
    MessageBox.Show("MyTextBox doubleClick event.");
}

Compiling the Code

See Also

Concepts

Designing a User Interface in Visual C#

Other Resources

Creating a Custom UI

Visual C# Guided Tour