从另一个表单向 TabControl 添加选项卡 [英] Add tabs to TabControl from another form
问题描述
这让我很害怕,如果可能的话,我很乐意提供帮助.
This is freaking me out, and if this is possible I would gladly appreciate the help.
我是 C# 开发人员,但必须在 VB.NET 中执行此操作.
I am a C# developer but have to do this in VB.NET.
所以 C# 答案也被接受.
So C# answers accepted as well.
我在表单上有一个选项卡控件.此控件中还没有任何选项卡.
I have a tab control on a form. This control does not have any tabs in it yet.
当表单加载时,它会加载一个开始"页面.它添加选项卡tbpStart"并将表单加载到选项卡页frmStart".
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?
代码:
当主表单加载时:
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
主窗体上的函数:
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
当我单击开始表单"上的一个单选按钮时,它会在 click_event 上执行此操作:
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?
推荐答案
修改 frmTabStart
的构造函数以接收 mainForm
的实例,如下所示:
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(...);
这篇关于从另一个表单向 TabControl 添加选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!