Creating controls at runtime using Windows Forms 2.0
In this article, we will explore how to add controls to a form at runtime. We will also explore how to bind events to these runtime controls.
Many a times, in our projects, we are in a situation where a control needs to be created at runtime. One example I could think of is a person filling in his family details is given the choice to enter the number of members in his family. In this case, we cannot determine how many family members will be entered by the user. So we have to accept the number of members and then generate those many textboxes. In this article, I will demonstrate you how to create controls at runtime and also bind events to these controls.
Step 1: Create a new project. File > New > Project. In the Project Type pane, select your language (Visual C# or Visual Basic) and then select ‘Windows Application’ in the ‘Visual Studio installed templates’ pane.
Step 2: Type a name for the project ‘CreateRuntimeControls’ and click OK.
Step 3: Declare two variables of type TextBox and Button at the class level in Form1.
public partial class Form1 : Form
// Create controls at runtime.
TextBox txtRun = null;
Button btnRun = null;
Public Partial Class Form1
' Create controls at runtime.
Private txtRun As TextBox = Nothing
Private btnRun As Button = Nothing
Public Sub New()
Step 4: Double click on Form1 and in the Form1_Load() event write this code:
private void Form1_Load(object sender, EventArgs e)
// Initialize the text box control and add it to the form
txtRun = new TextBox();
txtRun.Name = "txtDynamic";
txtRun.Location = new System.Drawing.Point(20, 18);
txtRun.Size = new System.Drawing.Size(200, 25);
// Add the textbox control to the form's control collection
// Initialize the button control and add it to the form
btnRun = new Button();
btnRun.Location = new System.Drawing.Point(20, 44);
btnRun.Size = new System.Drawing.Size(100, 25);
btnRun.Text = "Click Me";
// Add the button control to the form's control collection
// Bind the button to an event handler
btnRun.Click += new System.EventHandler(DisplayText);
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
' Initialize the text box control and add it to the form
txtRun = New TextBox()
txtRun.Name = "txtDynamic"
txtRun.Location = New System.Drawing.Point(20, 18)
txtRun.Size = New System.Drawing.Size(200, 25)
' Add the textbox control to the form's control collection
' Initialize the button control and add it to the form
btnRun = New Button()
btnRun.Location = New System.Drawing.Point(20, 44)
btnRun.Size = New System.Drawing.Size(100, 25)
btnRun.Text = "Click Me"
' Add the button control to the form's control collection
' Bind the button to an event handler
AddHandler btnRun.Click, AddressOf DisplayText
In the above code, we are creating controls and specifying some control properties like Name, Location and Size. The control is then added to the form’s control collection. This step is important for the control to be visible on the form.
In order to bind the button to an event handler, we use new System.EventHandler(DisplayText). You can mention any method instead of ‘DisplayText’. The code for DisplayText will look as mentioned below:
private void DisplayText(object sender, System.EventArgs e)
if (txtRun != null)
txtRun.Text = "I was created dynamically";
Private Sub DisplayText(ByVal sender As Object, ByVal e As System.EventArgs)
If Not txtRun Is Nothing Then
txtRun.Text = "I was created dynamically"
That’s it. Run the form. You will see that the textbox and button control have been generated at runtime. On clicking the button, the textbox is populated with the text ‘I was created dynamically’.
Having understood how to create controls dynamically, you can now experiment and try and create new controls on the button click. Try out the family member example I stated in the beginning of the article. Accept the number at runtime and create textboxes to fill in the family member’s name.
I hope this article was useful and I thank you for viewing it.
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!