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 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.
How to retrive value of textbox
???
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
End Sub
Private sub GetText()
messagebox.show(textbox1.text)
End Sub
You can also declare the Text Box control object with withevents like as:
Public WithEvents textbox1 As New TextBox
How to insert textbox if select the “Other” Item then textbox show in combobox?
Very nice example ….