Hi. Im using vb 2010. I want to dynamically add textboxes to my form by clicking on a button. I've google searched and so far this code worked:

 Private Sub btn_addline_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_addline.Click
        Dim txtB1 As New TextBox
        Dim i

        For i = 0 To 5
            Me.Controls.Add(txtB1)
            With txtB1
                .Name = "chkDemo" & i
            End With
        Next i
    End Sub

but it only creates one(well as i can see) and i cant tell whether the name is actually set as chkDemo(how to check?). I tried adding .Location = 20,20 but got error something to do with integer. Once i got this down, my goal is to actually dynamically add multiple textboxes or even maybe a combobox at the same time.

thanks in advance

Recommended Answers

All 6 Replies

What you need to do is create the control in the loop:

Private Sub btn_addline_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_addline.Click
    Dim i
    For i = 0 To 5
        Dim txtB1 As New TextBox        
        Me.Controls.Add(txtB1)
        With txtB1
            .Name = "chkDemo" & i
        End With
    Next i
End Sub

I posted this code snippet a while back that I think will show you how to do what you want.

Hi. thank you @Reverend Jim that did help. how do i create a new row of textboxes everytime button is clicked? As for now when button is clicked 1 row of 3 textboxes is added. I want so every time button is clicked a new row is added.

I changed the Const ROWS and COLS

 Const ROWS = 1       
 Const COLS = 3 

And added this so textbox name is different

 Dim i As Integer
 i += 1
 newbox.Name = "txt_" & i

Later on users will fill in data into the textboxes to be inserted into database, which i will have to figure out how to do later.

Maybe this snippet can do the work. Also, you may add handlers if you wish to, as Reverend's code points out.

Public Class Form1

    Dim vLbl(-1) As Label, vTb(-1) As TextBox, iv As Int32
    Dim posY As Int32
    Private Sub btnAdd_Click(sender As System.Object, e As System.EventArgs) Handles btnAdd.Click
        Try
            Dim btn As Button = CType(sender, Button)
            posY = IIf(iv = 0, btn.Top, posY + btn.Height)
            ReDim Preserve vLbl(iv), vTb(iv)
            vLbl(iv) = New Label
            With vLbl(iv)
                .Top = posY + 4
                .Left = btn.Location.X + btn.Width + 10
                .AutoSize = True
                .Text = "Label " + (iv + 1).ToString
                .Visible = True
                Me.Controls.Add(vLbl(iv))
            End With
            vTb(iv) = New TextBox
            With vTb(iv)
                .Top = posY
                .Left = vLbl(iv).Location.X + 50
                .Visible = True
                Me.Controls.Add(vTb(iv))
            End With
            iv += 1
        Catch ex As Exception

        End Try
    End Sub
End Class

@Xavier_5 thanks! this helped.

You're welcome. Could you mark as solved? Thanks in advance!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.