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,
Yuvjeeth

Recommended Answers

All 3 Replies

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"
    Me.Controls.Add(tbc)

    '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
        tbc.Controls.Add(tbp)
    Next

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

    Dim txt As New TextBox
    txt.Text = "default text"
    tbc.TabPages(2).Controls.Add(txt)

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.

Thanks a lot Reverend Jim. It Works!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.