user3197450
user3197450

Reputation: 3

VB.NET Getting Value On Dynamic Control

I am new to VB and run into some problems.

I have created sub routine that will automatically add a control to a panelcontrol each time i click on the button, so it can create as many as i want.

Here is the code for the subroutine.

Private Sub CreateControl()

    'CREATE TEXTBOX ITEMNO
    Dim i_Itemno As Integer = TextEditItemno.Length
    ReDim Preserve TextEditItemno(i_Itemno)
    TextEditItemno(i_Itemno) = New TextEdit
    With TextEditItemno(i_Itemno)
        .Name = "Txtitemno" & i_Itemno.ToString()
        If TextEditItemno.Length < 2 Then
            .SetBounds(0, 0, 32, 20)
        Else
            .Left = TextEditItemno(i_Itemno - 1).Left
            .Top = TextEditItemno(i_Itemno - 1).Top + TextEditItemno(i_Itemno - 1).Height + 4
            .Size = TextEditItemno(i_Itemno - 1).Size
        End If
        .Tag = i_Itemno
    End With
    AddHandler TextEditItemno(i_Itemno).TextChanged, AddressOf TextEditItemno_TextChanged
    PanelControl5.Controls.Add(TextEditItemno(i_Itemno))


    'CREATE TEXTBOX PRICE
    Dim i_Price As Integer = TextEditPrice.Length
    ReDim Preserve TextEditPrice((i_Price))
    Dim PriceX As Int16 = LblHarga.Location.X
    TextEditPrice(i_Price) = New TextEdit
    With TextEditPrice(i_Price)
        .Name = "Txtprice" & i_Price.ToString()
        If TextEditSatuan.Length < 2 Then
            .SetBounds(PriceX, 0, 70, 20)
        Else
            .Left = TextEditPrice(i_Price - 1).Left
            .Top = TextEditPrice(i_Price - 1).Top + TextEditPrice(i_Price - 1).Height + 4
            .Size = TextEditPrice(i_Price - 1).Size
        End If
        .Tag = i_Price
    End With
    AddHandler TextEditPrice(i_Price).TextChanged, AddressOf TextEditPrice_TextChanged
    PanelControl5.Controls.Add(TextEditPrice(i_Price))


End Sub

And i call it in a button click.

Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
    CreateControl()
End Sub

Now what i am looking for is how to loop and get the value of those textboxes no matter how many textboxex i have create.

 For i As Integer = 0 To TextEditItemno.Length - 1
        ' code to get the value of each textbox
    Next

Thank you

Upvotes: 0

Views: 958

Answers (1)

Bradley Uffner
Bradley Uffner

Reputation: 16991

This code goes in to your loop and gets the value of each textbox based on i.

Dim Text as String = TextEditItemno(i).Text

You may also be better served by using a List(of Textbox) rather than an array of textboxes. You don't need to worry about redimming the array, you can just do MyListOfTextboxes.Add(TheNewTextBox). You can still retrieve the value of each textbox the same way as the array.

Upvotes: 1

Related Questions