I need to be able to programmatically create new tabs on a TabControl, add controls to them, and be able to update the controls in each tab from another function. I already have a function to add tabs to the control, and to add controls to those tabs when they are created, but I'm stuck as to update the controls after they have been created.
EDIT: This is what I have to make the tabs and add the controls:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim tabpage As New TabPage
tabpage.Text = "(empty)"
Dim textbox1 As New TextBox
Dim textbox2 As New TextBox
textbox1.Parent = tabpage
textbox2.Parent = tabpage
textbox1.Location = New Point(10, 10)
textbox2.Location = New Point(10, 30)
TabControl1.TabPages.Add(tabpage)
End Sub
Urgh. I can't seem to get back into the account I used to post this question, so I have to post my follow-up to Tim's question in the comments for the previous answer as a new answer.
Debug.WriteLine(TabControl1.TabPages.Item(2).Controls.Find("textbox1", True).Count)
returns 0. The tab and the controls have been created prior.
Best Answer
Ok - I'll give it a shot, but I'm real rusty with WinForms, slightly less rusty with VB.NET. You'll need to locate the control you want to update, and you should be able to do that through the
Controls
collection of the appropriate container - in this case, most likely aTabPage
:Syntax might be slightly off, but hopefully this will at least point you in the right direction.
See Control.ControlCollection.Find Method
UPDATED
Hans Passant suggested that this isn't working because you didn't set the
Name
property (I'm assuming he means the name of the controls, not the tab page). I did a little more reading on theControlsCollection.Find
method, and MSDN says "Searches for controls by their Name property and builds an array of all the controls that match." You (and I) were trying to find the control by the instance name (textbox1, textbox2) - which were the instance names for the two controls, not the control names.So try this instead:
Then you can find the control using:
Give that a try and see if it works for you. The key (and what I missed looking at your code last night as it was past my bedtime for me) was there was now way to identify the control in the
Find
method.