Add tabs to TabControl from another form

旧巷老猫 提交于 2021-02-19 03:50:27

问题


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.

  • I have a tab control on a form. This control does not have any tabs in it yet.

  • When the form loads, it loads a "Start" page. It adds the tab "tbpStart" and loads a form onto the tab page "frmStart".

  • On this start page, I have many Radio Buttons. When I click on one radio button, it should load other tabs on the main form.

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?


回答1:


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(...);


来源:https://stackoverflow.com/questions/19726416/add-tabs-to-tabcontrol-from-another-form

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!