简体   繁体   English

从另一个表单向TabControl添加选项卡

[英]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. 我是C#开发人员,但必须在VB.NET中执行此操作。

So C# answers accepted as well. 所以C#答案也被接受了。

  • 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". 它添加了选项卡“tbpStart”并将表单加载到标签页“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: 当我点击“开始表单”上的一个单选按钮时,它会在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? 我究竟做错了什么?

Modify the constructor for frmTabStart to receive an instance of mainForm like this: 修改构造frmTabStart接收的实例mainForm是这样的:

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

and then when you need to add the tab: 然后当你需要添加标签时:

_mainForm.AddTabPage(...);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM