(一)标签页

先看看效果:

20141112 WinForm子窗口标签页-LMLPHP

代码:

     public partial class 标签页 : Form
{
string s = "";
public 标签页()
{
InitializeComponent();
}
/// <summary>
/// 判断选项卡中是否有本选项卡
/// </summary>
/// <param name="s">选项卡名</param>
/// <returns>布尔型</returns> private bool HasTablePages(string s)
{
for (int i = ; i < tabControl1.TabPages.Count; i++)
{
if (tabControl1.TabPages[i].Text == s)
{
tabControl1.SelectedIndex = i;//如果选项卡中有本选项卡,则把本选项卡设为焦点
return false;
}
}
return true;
}
/// <summary>
/// 点击菜单时,创建本选项卡,同时将自定义的选项卡控件添加进去
/// </summary>
/// <param name="s">选项卡名</param>
/// <param name="uc">自定义的控件</param>
private void MenuAdd(string s,UserControl uc)
{
if (HasTablePages(s))
{
TabPage page = new TabPage();
page.Text = s; page.Controls.Clear();
page.Controls.Add(uc);
uc.Dock = DockStyle.Fill; tabControl1.TabPages.Add(page);
tabControl1.SelectedTab = page;
}
}
private void 会员管理ToolStripMenuItem_Click(object sender, EventArgs e)
{
s = "会员管理";
UC_HuiYuan uc = new UC_HuiYuan();
MenuAdd(s,uc);
} private void 数据管理ToolStripMenuItem_Click(object sender, EventArgs e)
{
s = "数据管理";
UC_ShuJu sj = new UC_ShuJu();
MenuAdd(s,sj);
} private void 客房管理ToolStripMenuItem_Click(object sender, EventArgs e)
{
s = "客房管理";
UC_KeFang kf = new UC_KeFang();
MenuAdd(s,kf);
}
/// <summary>
/// 双击选项卡名时,关闭本选项卡
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tabControl1_DoubleClick(object sender, EventArgs e)
{
tabControl1.TabPages.RemoveAt(tabControl1.SelectedIndex);
}
}

20141112 WinForm子窗口标签页-LMLPHP

(二)子窗体

先看看效果:

20141112 WinForm子窗口标签页-LMLPHP

代码:

     public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 关闭所有窗口
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void 关闭所有窗口ToolStripMenuItem_Click(object sender, EventArgs e)
{
while (this.MdiChildren.Length > )
{
MdiChildren[].Close();
}
}
/// <summary>
/// 新建Form2
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
bool b = true;//用来判断子窗口中是否存在Form2
for (int i = ; i < this.MdiChildren.Length; i++)
{
if (this.MdiChildren[i].Name == "Form2")
{
b = false;
}
}
//没有子窗口 或者 子窗口中没有Form2 时
if (this.MdiChildren.Length == || b)
{
Form2 fm = new Form2();
fm.MdiParent = this;
fm.Show();
}
}
/// <summary>
/// 新建Form3
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void toolStripMenuItem2_Click(object sender, EventArgs e)
{
bool b = true;
for (int i = ; i < this.MdiChildren.Length; i++)
{
if (this.MdiChildren[i].Name == "Form3")
{
b = false;
}
}
if (this.MdiChildren.Length == ||b)
{
Form3 fm = new Form3();
fm.MdiParent = this;
fm.Show();
}
}
}

PS:

1.父窗体:IsMdiContainer=true
2.子窗体:建一个窗体,设计好。在父窗体“新建子窗体”界面中,把子窗体new出来。把新new出来的子窗体的MdiParent属性设置为父窗体对象。

05-11 11:29