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]

  1. Private Sub btnCreateTextbox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateTextbox.Click
  2.         Dim textbox1 As New TextBox
  3.         textbox1.Name = "Textbox1"
  4.         textbox1.Size = New Size(170, 20)
  5.         textbox1.Location = New Point(167, 32)
  6.         GroupBox1.Controls.Add(textbox1)
  7.   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]

  1.     Private Sub lblCreateLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblCreateLabel.Click
  2.         Dim label1 As New Label
  3.         label1.Name = "label1"
  4.         label1.Text = "Enter Name"
  5.         label1.AutoSize = True
  6.         label1.Location = New Point(80, 33)
  7.         GroupBox1.Controls.Add(label1)
  8.     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:

  1. Private Sub btnCreateTextbox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateTextbox.Click
  2.         Dim textbox1 As New TextBox
  3.         textbox1.Name = "Textbox1"
  4.         textbox1.Size = New Size(170, 20)
  5.         textbox1.Location = New Point(167, 32)
  6.         GroupBox1.Controls.Add(textbox1)
  7.         AddHandler textbox1.TextChanged, AddressOf Text_Changed
  8.   End Sub
  9.  
  10.     Private Sub Text_Changed(ByVal eventSender As System.Object, _
  11.                              ByVal eventArgs As System.EventArgs)
  12.         Dim txt As String = eventSender.Text
  13.         If IsNumeric(txt) Then
  14.  
  15.         Else
  16.             MessageBox.Show("Please enter numeric value")
  17.         End If
  18.     End Sub

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

  • Nirmalsinh

    How to retrive value of textbox
    ???

  • http://www.authorcode.com Hirendra Sisodiya

    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

  • Hirendra Sisodiya

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

    Public WithEvents textbox1 As New TextBox

  • Deep Gandhi

    How to insert textbox if select the “Other” Item then textbox show in combobox?

  • ahmed

    Very nice example ….