加载窗体到TabControl的方法:
private void AddNewTab(string frmName,string strTabName)
{
if(tcMain.TabPages.Count==5)
{
MessageBox.Show("最多只能打开5个子窗口!");
return;
}
else
{
tcMain.Visible = true;
Assembly asm = Assembly.GetExecutingAssembly();
Form fm = asm.CreateInstance("wisever."+frmName ) as Form ;
fm.TopLevel = false;
fm.WindowState = FormWindowState.Maximized;
fm.FormBorderStyle = FormBorderStyle.None;
var newPage = new TabPage((tcMain.TabPages.Count+1).ToString() + "-" + strTabName);
newPage.Controls.Add(fm);
tcMain.TabPages.Add(newPage);
tcMain.SelectedTab = tcMain.TabPages[tcMain.TabPages.Count - 1];
fm.Show();
}
}
点击菜单项时,执行以下语句:
private void tsmTest_Click(object sender, EventArgs e)
{
AddNewTab("frmTest", "Test" );
}
效果图:
关闭页签时,在页签上双击鼠标即可。
private void tcMain_MouseDoubleClick(object sender, MouseEventArgs e)
{
tcMain.TabPages.RemoveAt(tcMain.SelectedIndex);
if (tcMain.TabPages.Count == 0) tcMain.Visible = false;
}