显示数组或文本框列表中的循环值

时间:2016-12-20 11:16:05

标签: arrays vb.net list

我有一个非常简单的for循环语句,如下所示。如何使用数组或列表将每个值显示到相应的文本框?在我的循环中我有6个索引,我还有6个文本框,这意味着索引0将显示在txtbox1中,索引1将显示在txtbox2中,依此类推。

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

1 个答案:

答案 0 :(得分:0)

假设您想要从数组中指定值的所有文本框都是Form的子控件。如果文本框名称从txtBox0开始,请从以下代码中删除i+1

    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