简体   繁体   English

VB.NET在动态控件上获得价值

[英]VB.NET Getting Value On Dynamic Control

I am new to VB and run into some problems. 我是VB新手,遇到了一些问题。

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. 我创建了一个子例程,该子例程将在每次单击按钮时自动将一个控件添加到panelcontrol中,因此它可以创建任意数量的控件。

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. 现在我正在寻找的是无论我创建了多少个textboxex,如何循环并获取这些文本框的值。

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

Thank you 谢谢

This code goes in to your loop and gets the value of each textbox based on i . 此代码进入循环,并基于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. 使用List(of Textbox)而不是文本框数组可能也可以为您提供更好的服务。 You don't need to worry about redimming the array, you can just do MyListOfTextboxes.Add(TheNewTextBox) . 您无需担心重新格式化数组,只需执行MyListOfTextboxes.Add(TheNewTextBox) You can still retrieve the value of each textbox the same way as the array. 您仍然可以以与数组相同的方式检索每个文本框的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM