对于VB中的循环文本框

时间:2014-02-07 10:45:48

标签: vb.net nullreferenceexception

For a = 1 To row
     For b = 1 To clmn
           Form1.Controls("A" & CStr(a) & "T" & CStr(b)).Text = table.Rows(a)(b)
     Next
Next

我收到错误“System.NullReferenceException”

我的文本框名称是A1T1,A1T2,A1T3,....

如何在这些文本框中使用循环? 虽然当我从0,0开始表索引时,错误仍然相同

1 个答案:

答案 0 :(得分:0)

DataTable使用从零开始的索引。因此,您的第一行/列索引为0,而不是1

Dim key As String

For a As Integer = 0 To (table.Rows.Count - 1)
    For b As Integer = 0 To (table.Columns.Count - 1)
        key = String.Format("A{0}T{1}", (a + 1), (b + 1))
        If (Me.Controls.ContainsKey(key)) Then
            Me.Controls.Item(key).Text = Convert.ToString(table.Rows(a)(b))
        Else
            Throw New ApplicationException("You need to create a control named: '" & key & "'")
        End If
    Next
Next