Hi all,
I have a TabControl into which, at runtime, add a tabpage and a control to the tabpage created. I know how to create a tabpage. But I cant figure out a way how to create both at runtime.
Thanks in advance,

I find the easiest way to figure this out is to place the controls in the IDE then look at the <form>.Designer.vb file that is created when you save. If you do that the sequence becomes (with a little rearranging for personal taste)

  1. Create the tab control and add it to the form
  2. Create the tab pages and add them to the tab control
  3. Create controls for the individual tab pages and add them to that page

Here is some sample code to use on a blank form

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

    'Create the tab control and add it to the main form

    Dim tbc As New System.Windows.Forms.TabControl()
    tbc.Location = New Point(10, 50)
    tbc.Name = "tbcMyTab"

    'Create three tab pages and add them to the tab control

    For i As Integer = 0 To 2
        Dim tbp As New System.Windows.Forms.TabPage()
        tbp.Text = "Page " & i

    'Create a text control and add it to the last tab page

    Dim txt As New TextBox
    txt.Text = "default text"

End Sub

Keep in mind that unless you declare tbc at the class level you will have to refer to the control in other subs/functions as

Dim tbc As TabControl = Me.Controls("tbcMyTab")
tbc.TabPages ... etc

The same thing applies to any other controls you add to the tab pages.

You can also see from the *.Designer.vb file that there are other properties that you may want to set. You will also have to add event handlers at runtime. You can see how to do that here.

Edited 2 Years Ago by Reverend Jim

This question has already been answered. Start a new discussion instead.