Create dynamic Textbox and Label in vb.net

In this article we will learn how to create TextBox and Label control at run time in vb.net.
There are many situations in windows application development when we need to create windows controls at run time.

The following example shows how to create and add controls to group box(create textbox and label control in groupbox dynamically).

Create textbox and label control at runtime in vb.net

[Create dynamic Textbox]

Private Sub btnCreateTextbox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateTextbox.Click
        Dim textbox1 As New TextBox
        textbox1.Name = "Textbox1"
        textbox1.Size = New Size(170, 20)
        textbox1.Location = New Point(167, 32)
        GroupBox1.Controls.Add(textbox1)
  End Sub

[Code explanation]
On the button click procedure write the code to create an object of TextBox class, and set the properties like name, Size and location, and then we add this control to GroupBox control with the help of Controls.Add() function.

[Create Dynamic Label]

    Private Sub lblCreateLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblCreateLabel.Click
        Dim label1 As New Label
        label1.Name = "label1"
        label1.Text = "Enter Name"
        label1.AutoSize = True
        label1.Location = New Point(80, 33)
        GroupBox1.Controls.Add(label1)
    End Sub

[Add event at run time]

From the above we create controls at run time and now we move to next step- create event handler at run time and attach that event to the control.
For the example, Suppose we need to validate entered data by user in above created TextBox, for it we need to TextChanged event of textbox1 so we use the AddHandler statement, AddHandler statement allow us to start event handling at any time during program execution means it associates an event with an event handler at run time. we can use this statement as :

AddHandler _event, AddressOf procedurename

where _event is the name of the event to handle and procedurename is the name of a procedure that handles the event.

we can use AddHandler statement in the above code like this:

Private Sub btnCreateTextbox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateTextbox.Click
        Dim textbox1 As New TextBox
        textbox1.Name = "Textbox1"
        textbox1.Size = New Size(170, 20)
        textbox1.Location = New Point(167, 32)
        GroupBox1.Controls.Add(textbox1)
        AddHandler textbox1.TextChanged, AddressOf Text_Changed
  End Sub
 
    Private Sub Text_Changed(ByVal eventSender As System.Object, _
                             ByVal eventArgs As System.EventArgs)
        Dim txt As String = eventSender.Text
        If IsNumeric(txt) Then
 
        Else
            MessageBox.Show("Please enter numeric value")
        End If
    End Sub

each time when user input some text in the textbox, Text_Changed procedure will be executed.

5 thoughts on “Create dynamic Textbox and Label in vb.net”

  1. if form does not have many dynamic textbox then you declare object of textbox as global and then you can use text property:

     Dim textbox1 As New TextBox

    Private Sub btnCreateTextbox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateTextbox.Click
         
            textbox1.Name = “Textbox1”
            textbox1.Size = New Size(170, 20)
            textbox1.Location = New Point(167, 32)
            GroupBox1.Controls.Add(textbox1)

      End Sub

    Private sub GetText()

      messagebox.show(textbox1.text)

    End Sub

  2. You can also declare the Text Box control object with withevents like as:

    Public WithEvents textbox1 As New TextBox

Comments are closed.