Dynamically Adding Controls on a Form
This article explains how to add different controls dynamically on a form using VB.Net
code. It takes an integer value as input for number of a particular control to be generated
at runtime and makes the controls accordingly. It also explains how to properly align the
controls vertically. Controls covered in this article are Label, Button, Checkbox and RadioButton.
Introduction
I have seen people asking in the forums how to add different controls dynamically on a form. Most of the time there is a requirement to make controls according to the value coming from the database or some other source. These values can vary hence we cannot put controls on the form at design time and must add them at runtime only. Hence I decided to pen down an article which can act as a single platform for providing code for generating most common controls (Button, Label, Radio Button and Check Box). After going through this article you can easily make other controls too. Let's make a demo application to understand the concept.Getting Started
In the application we are having a form with a textbox and a button. A number we will input in the textbox. When the button will be clicked, accordingle a particular control will be generated.Step 1
Take a New Visual Basic Project and add a textbox and a button on the form. The user should input only numbers in the textbox. Add the following code on the KeyPress event of the textbox.
Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar >= "0" And e.KeyChar <= "9" Then
e.Handled = False
Else
e.Handled = True
End If
End SubStep 2
On button's click, we will take the value of the textbox, parse it to Int32 and store it in a variable max.
Dim max As Integer
max = Convert.ToInt32(TextBox1.Text)
Note: This value indicated number of controls to be added on the form at runtime hence it can come from the database or some other source.Step 3
Now we will take a for loop for counting purpose. This for loop will start from 1 and will go till the max value. Inside this for loop we will place the code for generating the control. Following code will add Labels on the form.
Dim lbl As New Label
lbl.AutoSize = True
lbl.Location = New System.Drawing.Point(15, 50 + (30 * value))
lbl.Name = "Label " & value
lbl.Size = New System.Drawing.Size(39, 13)
lbl.TabIndex = value
lbl.Text = "Label " & value
Me.Controls.Add(lbl)
Brief Explanation
Dim lbl As New Label
This line created an object lbl of Label class.
lbl.Location = New System.Drawing.Point(15, 50 + (30 * value))
Here we are providing the location for the label. If we provide same location then labels will be added on the form one above the previous which will make just the last label visible. As we have to align them vertically hence we have kept the first value (x-axis) fixed to 15. The second value (y-axis) has been increased in the multiples of 30. This will make the labels vertically aligned. If you want to align them horizontally then keep the y-axiz value fixed and increase the value of x-axis in multiples of any number.
lbl.Name = "Label " & value
This line will give name to each object as Label1, Label2 and so on. Remember each object should have a unique name a should not contain any invalid characters.
lbl.Text = "Label " & value
This line will set the text to be displayed on the label as Label1, Label2, Label3 and so on.
Me.Controls.Add(lbl)
The keyword Me refers to the current form. After assigning values to the instance of the Label class finally we are adding it on the form. If you miss to write this line then the control will not get added on the form. So don't forget to write this line at the end.
Finally the code on Button's click will be as follows.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim max As Integer
max = Convert.ToInt32(TextBox1.Text)
For value As Integer = 1 To max
Dim lbl As New Label
lbl.AutoSize = True
lbl.Location = New System.Drawing.Point(15, 50 + (30 * value))
lbl.Name = "Label " & value
lbl.Size = New System.Drawing.Size(39, 13)
lbl.TabIndex = value
lbl.Text = "Label " & value
Me.Controls.Add(lbl)
Next
End SubCode for Other Controls
Now I am providing code for other most common controls which can be used in place of or along with the code for generating labels at runtime.TextBox
Dim txt As New TextBox
txt.Location = New System.Drawing.Point(60, 50 + (30 * value))
txt.Name = "TextBox " & value
txt.Size = New System.Drawing.Size(100, 20)
txt.TabIndex = value
Me.Controls.Add(txt)Button
Dim btn As New Button
btn.Location = New System.Drawing.Point(60, 50 + (30 * value))
btn.Name = "Button " & value
btn.Size = New System.Drawing.Size(75, 23)
btn.TabIndex = 0
btn.Text = "Button " & value
btn.UseVisualStyleBackColor = True
Me.Controls.Add(btn)RadioButton
Dim rdbtn As New RadioButton
rdbtn.AutoSize = True
rdbtn.Location = New System.Drawing.Point(30, 50 + (30 * value))
rdbtn.Name = "RadioButton " & value
rdbtn.Size = New System.Drawing.Size(90, 17)
rdbtn.TabIndex = value
rdbtn.TabStop = True
rdbtn.Text = "RadioButton " & value
rdbtn.UseVisualStyleBackColor = True
Me.Controls.Add(rdbtn)CheckBox
Dim chk As New CheckBox()
chk.AutoSize = True
chk.Location = New System.Drawing.Point(60, 50 + (20 * value))
chk.Name = "CheckBox" & value
chk.Size = New System.Drawing.Size(81, 17)
chk.TabIndex = value
chk.Text = "CheckBox" & value
chk.UseVisualStyleBackColor = True
Me.Controls.Add(chk)
I am also attaching a demo application which contains all these codes for your reference.
(Don't forget to rate the content and leave your responses.)
thank you very much.. it's very nicely working and very helpful