Search code examples
vb.nettabcontroltabpage

TabControl in TabPage


I got a following problem: I add a dynamically created TabControl to a tabpage (which is also dynamically created). When I try to add another tabpage with another tabcontrol, it acts like there's no new TabControl added to the new TabPage. In fact, it does not add anything. What should I do? Here's a code where I create a TabPage:

    Dim tbstate As New TabPage
    tbstate.ContextMenuStrip = QuestEd.cmsState
    tbstate.Text = String.Format("State: {0}", txtState.Text)
    QuestEd.TabControl1.TabPages.Add(tbstate)
    txtState.Text = Nothing
    tbstate.Select()
    QuestEd.RefreshData()
    Me.Dispose()

And here is the code where I create a new TabControl for this TabPage

Public newtab As New TabControl
     ...
            If QuestEd.TabControl1.SelectedTab.Controls.Count = 0 Then
                newtab.Parent = QuestEd.TabControl1.SelectedTab
                newtab.Dock = DockStyle.Fill
                QuestEd.TabControl1.SelectedTab.Controls.Add(newtab)
                QuestEd.TabControl1.SelectedTab.Refresh()
                newtab.TabPages.Add(String.Format("When: {0}", typ))
            Else
                newtab.TabPages.Add(String.Format("When: {0}", typ))
            End If

What do I do wrong? Can somebody suggest me how to fix it? Thanks in advance


Solution

  • Just ran a quick test of your code. I am guessing since you have Public visibility on your newtab that you are declaring it at the Class level. Your code does work, it does add a new TabControl on a TabPage. In reading Hans's comment he most likely is correct on what is going on, If you are using the class name of the Form instead of an Instance Name then you would not see anything. If you are trying to add these controls from another form you will need to pass in the instance of the form that you are adding it to. Without seeing how your are structuring your project it is just a wild guess.

    My Example:

    Public Class Form1
        Dim newtab As New TabControl
        Dim typ As String = "Hello"
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim tbstate As New TabPage
            Dim tbstate1 As New TabPage
            tbstate.Text = String.Format("State: {0}", "My Test")
            TabControl1.TabPages.Add(tbstate)
            TabControl1.TabPages.Add(tbstate1)
            tbstate.Select()
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            If TabControl1.SelectedTab.Controls.Count = 0 Then
                newtab = New TabControl
                newtab.Parent = TabControl1.SelectedTab
                newtab.Dock = DockStyle.Fill
                TabControl1.SelectedTab.Controls.Add(newtab)
                TabControl1.SelectedTab.Refresh()
                newtab.TabPages.Add(String.Format("When: {0}", typ))
            Else
                DirectCast(TabControl1.SelectedTab.Controls(0), TabControl).TabPages.Add(New TabPage(String.Format("When: {0}", typ)))            End If
        End Sub
    End Class