I’ve created a new tabpage and also added a richtextbox to it:
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
Now I try to access the richtextbox somewhere else in the code:
Dim NewTextbox As RichTextBox
NewTextbox = Me.Controls.Item("OutputTextbox" & current_output)
debug.print(NewTextbox.name)
I get the following error:
A first chance exception of type 'System.NullReferenceException' occurred in program.exe
I know the name is rigth cause I have printed the name in the create method and I have printed the name string in the code where I try to access it.
So by the looks of it it seems .Item() is not the right way to access the control.
So how to access to dynamically created control?
You are adding the dynamic control to a container by name
ctrland later looking for it in the form container. You can search recursively usingMe.FindControl()but in your case, since you know the container that has theRichTextBox, it would be more efficient to do something as shown below.Try