0

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
user3109627
  • 15
  • 1
  • 3

1 Answers1

0

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 
Mukul Varshney
  • 3,131
  • 1
  • 12
  • 19