How do you add controls to a tab page at run time? You use the Add method of the Controls collection of the tab page. You can see this at work when you click the "Add new button" button in the TabControls example on the CD-ROM. Here's the code—I'm adding a new button to a tab page, and adding an event handler to the new button so it actually does something when clicked:
Dim WithEvents MyButton As New Button() Private Sub Button2_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button2.Click AddHandler MyButton.Click, AddressOf MyButton_Click MyButton.Size = New Size(88, 23) MyButton.Location = New Point(8, 45) MyButton.Text = "New button" TabControl1.TabPages(0).Controls.Add(MyButton) End Sub Private Sub MyButton_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyButton.Click MsgBox("You clicked the button!") End Sub
You can see the results in Figure 10.26—when you click the "Add new button" button, a new button appears in the tab page.