Fred
Fred

Reputation: 2468

Add tabs to TabControl from another form

This is freaking me out, and if this is possible I would gladly appreciate the help.

I am a C# developer but have to do this in VB.NET.

So C# answers accepted as well.

The problem is how can I add tabs to one form's tab control from another form?

CODE:

When Main Form loads:

Try
    'Load the Start Tab
    Dim start As New frmTabStart
    AddTabPage("Start", start)
Catch ex As Exception
    PMComponentLibrary.PMMessageBox.ShowErrorMessage("Error occurred while trying to load the from.", ex)
End Try

Function on Main Form:

Public Sub AddTabPage(tabPageName As String, myForm As System.Windows.Forms.Form)
    Try
        myForm.TopLevel = False
        myForm.Dock = DockStyle.Fill
        myForm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None

        Dim NewTab As New System.Windows.Forms.TabPage
        NewTab.Name = "tab" + tabPageName
        NewTab.Text = tabPageName
        NewTab.Controls.Add(myForm)
        tbcMain.TabPages.Add(NewTab)
        myForm.Show()
    Catch ex As Exception
        Throw ex
    End Try
End Sub

When I click on one Radio Button on "Start Form" it executes this on a click_event:

If sender Is rdbWIPPostings Then

    entity = New frmTabEntity()
    mainForm.AddTabPage("Step 1", entity)
    Application.DoEvents()
    dte = New frmTabDate()
    mainForm.AddTabPage("Step 2", dte)

    wipSelect = New frmTabWIPSelect()
    mainForm.AddTabPage("Step 3", wipSelect)

    finish = New frmTabFinish()
    mainForm.AddTabPage("Finish", finish)

End If

But the tabs does not get added to the Main Form. What am I doing wrong?

Upvotes: 3

Views: 2365

Answers (1)

Mike Perrenoud
Mike Perrenoud

Reputation: 67898

Modify the constructor for frmTabStart to receive an instance of mainForm like this:

public frmTabStart(MainForm mainForm)
{
    // store that in a field
}

and then when you need to add the tab:

_mainForm.AddTabPage(...);

Upvotes: 1

Related Questions