Dpbo Sandi Rigo Nugraha

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

8/7/2017 vb.net - How to add textboxes, labels and buttons dynamically at runtime in VB?

- Stack Overflow

x Dismiss

Join the Stack Overflow Community

Stack Overflow is a community of 7.5 million


programmers, just like you, helping each other.
Join them; it only takes a minute:

Sign up

How to add textboxes, labels and buttons dynamically at runtime in VB?

How to create a form with a button add_subjects which adds one textbox and a corresponding label on each click, 3 buttons - Add, Edit and
Delete , for each textbox created during runtime in VB . Once each texbox's corresponding Add _button is clicked, it passes textbox's
value to the label.

vb.net winforms

edited Jan 8 '14 at 9:34 asked Jan 8 '14 at 9:25


Markus user3172488
11.6k 2 18 38 11 1 1 2

1 why dont you use datagrid and edit everthing there? DevelopmentIsMyPassion Jan 8 '14 at 9:30

3 Answers

A control like a textbox is just an object of the class Textbox. In order for the form to display
this object it needs to be added to the form's Controls property. To create a new textbox all you
need to do is

Dim newTB as New Textbox


newTB.Name = "tbNew"
'Set location, size and so on if you like
Me.Controls.Add(newTB)

If you want your control to be able to respond to events you need to add an event handler for
the event you want to the control. This handler refers the event to a method of your choice.

Public Class Form1

Sub CreateTB
Dim NewTB as New Textbox
newTB = New Textbox
newTB.Name = "tbNew"
AddHandler newTB.TextChanged, AddressOf HandleTextChanged
Me.Controls.Add(newTB)
End Sub

Private Sub HandleTextChanged(sender as Object, e as EventArgs)


'Handle the event
End Sub
End Class

You should make sure that the names are unique if you are creating the controls or you might
run into trouble.

You can also store your created controls in an array or list as a global variable. That way you
can easily access them afterwards.

https://stackoverflow.com/questions/20991539/how-to-add-textboxes-labels-and-buttons-dynamically-at-runtime-in-vb 1/3
8/7/2017 vb.net - How to add textboxes, labels and buttons dynamically at runtime in VB? - Stack Overflow

answered Jan 8 '14 at 10:04


Jens
4,703 2 11 35

Private Property number as Integer=1

Private Sub add_subject_Click(sender As Object, e As EventArgs) Handles add_subject.Click


Dim tb As New TextBox
tb.Name="TextBox"+number.ToString
tb.Position = New Point(number*40,10) ' change this if you want
Me.Controls.Add(tb)
Dim lb As New Label
lb.Name="Label"+number.ToString
lb.Position = New Point(number*40,50) ' change this if you want
Me.Controls.Add(lb)
Dim add As New Button
add.Name="AddButton"+number.ToString
add.Position = New Point(number*40,100) ' change this if you want
AddHandler(add.Click, AdressOf(add_Click))
Me.Controls.Add(add)
Dim edit As New Button
edit.Name="EditButton"+number.ToString
edit.Position = New Point(number*40,150) ' change this if you want
AddHandler(edit.Click, AdressOf(edit_Click))'you have to make edit_Click
YourForm.Controls.Add(edit)
Dim delete As New Button
delete.Name="DeleteButton"+number.ToString
delete.Position = New Point(number*40,200) ' change this if you want
AddHandler(delete.Click, AdressOf(delete_Click))'you have to make delete_Click
Me.Controls.Add(delete)
number+=1
End Sub

So we make all controls, dynamically make names, change positions, add handlers and add
controls to form.

Private Sub add_Click(sender As Object, e As EventArgs)


Ctype(Me.Controls.Find("Label"+sender.Name.Substring(9),True).First,Label).Text =
Ctype(Me.Controls.Find("TextBox"+sender.Name.Substring(9),True).First,TextBox).Text
End Sub

Here we find Label And TextBox using sender's number(sender.Name.Substring(9) will remove
AddButton and leave number) and change Label.Text to TextBox.Text.

Get all label values and insert them in database:

Private Sub save(sender As Object, e as EventArgs) Handles button_save_subjects.Click


For i = 1 to number
Dim value As String
value = CType(Me.Controls.Find("Label"+number.ToString).First,Label).Text
'insert into database
Next
End Sub

edited Jan 8 '14 at 20:33 answered Jan 8 '14 at 10:26


srka
690 5 20

Is it possible to track each of these dynamically created labels and their values, so that I can store those
labels values as a table fields in a database using SQL embedded in a button_save_subjects event?
user3172488 Jan 8 '14 at 14:31

I added code to get all values,but I don't know SQL so you'll have to insert values in database. srka Jan 8
'14 at 20:41

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

https://stackoverflow.com/questions/20991539/how-to-add-textboxes-labels-and-buttons-dynamically-at-runtime-in-vb 2/3
8/7/2017 vb.net - How to add textboxes, labels and buttons dynamically at runtime in VB? - Stack Overflow
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

Refer Here

Source

answered Jan 8 '14 at 9:53


Vignesh Kumar
16.6k 8 33 73

https://stackoverflow.com/questions/20991539/how-to-add-textboxes-labels-and-buttons-dynamically-at-runtime-in-vb 3/3

You might also like