我创建了一个新标签页,还向其中添加了一个富文本框:
Private Sub AddTab(ByVal ctrl As TabControl, _
ByVal text As String)
If Me.InvokeRequired Then
Me.Invoke(New AddTabDelegate(AddressOf AddTab), _
New Object() {ctrl, text})
Return
End If
Dim NewTab As New TabPage
NewTab.Name = "OutputTab" & outputs.Item(outputs.Count - 1)
NewTab.Text = "Domain"
Dim NewTextbox As New RichTextBox
NewTextbox.Name = "OutputTextbox" & outputs.Item(outputs.Count - 1)
ctrl.Controls.Add(NewTab)
NewTab.Controls.Add(NewTextbox)
End Sub
现在我尝试在代码的其他地方访问richtextbox:
Dim NewTextbox As RichTextBox
NewTextbox = Me.Controls.Item("OutputTextbox" & current_output)
debug.print(NewTextbox.name)
我收到以下错误:
A first chance exception of type 'System.NullReferenceException' occurred in program.exe
我知道这个名字是正确的,因为我已经在 create 方法中打印了这个名字,并且我已经在我尝试访问它的代码中打印了名字字符串。
因此,从外观上看,这似乎.Item()
不是访问控件的正确方法。
那么如何访问动态创建的控件呢?