我有一个VB.Net Sub,它将列表中的值加载到表单上的文本框中(此表单是子所在的位置)。如果我直接引用文本框 ,请说txt01.Text = "Blah"
,它可以正常工作;但如果我以编程方式引用相同的文本框,例如Me.Controls("txt01").Text = "Blah"
,我会继续System.NullReferenceException
。
以下是代码:
Private Sub lboxCodebooks_SelectedIndexChanged(sender As Object, e As
EventArgs) Handles lboxCodebooks.SelectedIndexChanged
'On Error GoTo ErrorHandler
If lboxCodebooks.SelectedIndex < 0 Then GoTo CleanUpAndExit
grpCodebook.Enabled = True
Dim c As Codebook = CB.Item(lboxCodebooks.SelectedIndex)
Dim ctlName As String
Dim ctl As Control
txtCodebookName.Text = c.Name
For i As Integer = 1 To 93
ctlName = "txt" & Format(i, "00")
ctl = Me.Controls(ctlName) ' This sets ctl to Nothing - but why?
ctl.Text = Mid(c.AlphaCodex, i, 1) ' NullReference error here.
Next
CleanUpAndExit:
On Error Resume Next
c = Nothing
Exit Sub
ErrorHandler:
Call ErrorHandler(Err)
Resume CleanUpAndExit
End Sub
我到底做错了什么?