简体   繁体   中英

Display for loop values in an array or list of textboxes

I have a very simple for loop statement as shown below. How to display each value to a corresponding textbox using an array or list? In my loop I have 6 indexes and I have also 6 textboxes, which means index 0 will be displayed in txtbox1, index 1 will be displayed in txtbox2 so on and so forth.

    Dim i As Integer
    For i = 0 To 5
       ' TextBox Here
    Next

Assuming all text boxes, you want to assign value from an array, are child control of Form . If text box name starts from txtBox0 then remove i+1 from the below code.

    Dim arr() As String = {"aa", "bb", "cc", "dd", "ee"}

    Dim txtBox As TextBox
    Dim ctrlName As String
    Dim i As Integer

    For i = 0 To 5
        ' TextBox Here
        ctrlName = "txtbox" + (i + 1).ToString
        Try
            txtBox = CType(Me.Controls(ctrlName), TextBox)
            If Not txtBox Is Nothing Then
                txtBox.Text = arr(i)
            End If
        Catch ex As Exception
            'ignore or raise error
        End Try
    Next 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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