Dynamic Tab Control Inheritance

spach

Member
Joined
Sep 25, 2006
Messages
5
Programming Experience
Beginner
I've got a tab control and want to dynamically add tabs depending on the output from a database.

I can easily add a new tab, however I want to inherit the controls and layout from the initial tab.

Can anyone help me???

Many thanks in advance
 
You should define a UserControl that contains all the controls you want to display on a tab. You should then define an inherited TabPage class that, when created, creates an instance of your UserControl and displays it, e.g.
VB.NET:
Public Class TabPageEx
    Inherits System.Windows.Forms.TabPage

    Private WithEvents uc As New UserControl1

    Public Sub New()
        Me.uc.Margin = New Padding(0)
        Me.uc.Dock = DockStyle.Fill
        Me.Controls.Add(Me.uc)
    End Sub

End Class
Now you can create instances of your TabPageEx class and add them to the tabControl instead of regular TabPages.
 
Hi,
I never did implement it as I needed differing controls within the control!

Sorry I can't be of more help.

Thanks
Clive
 
tab class with usercontrol

Hi Experts,
i'm a beginner to vb. can anyone plz help me in solving this issue.

i have user control defined as:
Public Class UserControl1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox1.Text = "asdf"
End Sub
End Class


and created a tab class as follows:
Public Class MyTabControl
Inherits System.Windows.Forms.TabControl
Private WithEvents uc As New UserControl1()
Public Sub New()
Me.uc.Margin = New Padding(0)
Me.uc.Dock = DockStyle.Fill
Me.Controls.Add(Me.uc)
End Sub
End Class



Now on my main form i have a button to add tab with name from textbox.

Public Class Form1
Dim f1 As New MyTabControl()

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
f1.Controls.Add(New TabPage(TextBox1.Text))
End Sub


The error thrown is: Cannot add 'UserControl1' to TabControl. Only TabPages can be directly added to TabControls.

can anyone plz let me know what mistake am i doing here.
 
Read post #2 again by jmc, you made a class that inherited from tabcontrol instead of tabpage.
 
Back
Top