类似excel表格中各个表单sheet的切换效果,使用tabcontrol控件实现类似的功能。效果如下:
过程涉及父窗体MDIParent1、子窗体main、自定义基础功能类MdiChildBase。
基础功能类MdiChildBase继承自Form创建,定义了一个委托SetTabControlDelegate,委托的实例SetTabI用于指定FormClosing和Shown方法中调用的方法(函数),添加了窗口的FormClosing和Shown处理方法,窗口再打开和关闭的时候会被调用,即后面继承MdiChildBase创建的子窗体会在窗口关闭和显示的时候执行相应方法。可见,委托的实例SetTabI起到重要的连接作用。
子窗体main是自己创建的窗体,继承自类MdiChildBase,其他用于子窗体的Form也需要继承类MdiChildBase:“public partial class Main : MdiChildBase”,在窗口的关闭和显示过程中会执行MdiChildBase中注册的FormClosing和Shown方法,调用父窗体MDIParent1的SetTabControl方法添加和移除对应窗口的tab标签;
父窗体MDIParent1为MDI父窗体,包含一个用户显示子窗体列表的tabcontrol控件,定义了SetTabControl方法添加和移除对应窗口的tab标签;
主要代码
父窗体MDIParent1:
MDIParent父窗体设置为MDI容器窗体:this.IsMdiContainer = true;
MDIParent父窗体的Designer中tabControl控件的主要属性设置:
//
// tabControl1
//
this.tabControl1.Dock = System.Windows.Forms.DockStyle.Bottom;
this.tabControl1.Location = new System.Drawing.Point(0, 496);
this.tabControl1.Name = "tabControl1";
this.tabControl1.SelectedIndex = 0;
this.tabControl1.ShowToolTips = true;
this.tabControl1.Size = new System.Drawing.Size(992, 18);
this.tabControl1.TabIndex = 4;
this.tabControl1.Visible = false;
this.tabControl1.SelectedIndexChanged += new System.EventHandler(this.tabControl1_SelectedIndexChanged);
MDIParent:
public partial class MDIParent1 : Form
{
private void MDIParent1_Load(object sender, EventArgs e)
{
MdiChildBase.SetTabII = this.SetTabControl;
LoadMdiForm(typeof(Main));
}
public void LoadMdiForm(Type formType)
{
Form w = Application.OpenForms[formType.Name];
bool exists = false;
MdiChildBase mdiChild = null;
if (w != null)
{
exists = true;
mdiChild = w as MdiChildBase;
}
if (!exists)
{
mdiChild = (MdiChildBase)Activator.CreateInstance(formType, true);
mdiChild.MdiParent = this;
mdiChild.Show();
}
mdiChild.SetTabI = SetTabControl;
mdiChild.BringToFront();
mdiChild.Activate();
}
public void SetTabControl(string type,string name)
{
if(type=="add")
{
TabPage page= new TabPage();
page.Text = name;
page.Name = name;
tabControl1.TabPages.Add(page);
tabControl1.SelectedTab = page;
if (tabControl1.TabPages.Count > 0) tabControl1.Visible = true ;
}
else if (type=="remove")
{
TabPage tab = tabControl1.TabPages[name];
if(tab != null)
tabControl1.TabPages.Remove(tab);
if (tabControl1.TabPages.Count == 0) tabControl1.Visible = false;
}
else if (type=="select")
{
TabPage page = tabControl1.TabPages[name];
tabControl1.SelectedTab = page;
}
}
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
if (tabControl1.SelectedTab == null) return;
string WindowsName = tabControl1.SelectedTab.Text;
// MdiChildren
Form w = Application.OpenForms[WindowsName];
//判断对应窗口是否被打开
//if ((w == null) || (w.IsDisposed))
w.Activate();
//w.Show();
//w.WindowState = FormWindowState.Normal;
}
}
子窗体main:
public partial class Main : MdiChildBase
{
public Main()
{
CheckForIllegalCrossThreadCalls = false;
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
MdiChildBase:
public class MdiChildBase:Form
{
public MdiChildBase()
{
this.FormClosing += new FormClosingEventHandler(baseFormClosing);
this.Shown += new EventHandler(baseFormShown);
}
/// <summary>
/// 主要用于MDI父窗体中窗口列表的显示
/// </summary>
/// <param name="type"></param>
/// <param name="name"></param>
public delegate void SetTabControlDelegate(string type, string name);
//用于从mdiparent主窗口发起的窗口FormClosing、Shown过程中对窗口列表的添加删除;
public SetTabControlDelegate SetTabI;
//用于从其他非mdiparent主窗口发起的窗口生成,在mdiparent主窗口load中设置了方法名称;
//即:mdiparent主窗口实例存在的话,会在子窗口中的FormClosing和Shown过程中执行对应的方法;
// 若主窗口不是从 mdiparent主窗口开始运行,则不存在MDI父窗体,SetTabII为null,也不会执行对应方法
public static SetTabControlDelegate SetTabII;
//public Action<string,string> SetTabI;
//public static Action<string, string> SetTabII;
public virtual void baseFormClosing(object sender, EventArgs e)
{
//SetTabI?.Invoke("remove", this.Name);
if (SetTabI != null)
{
SetTabI.Invoke("remove", this.Name);
//MessageBox.Show("remove");
}
}
public virtual void baseFormShown(object sender, EventArgs e)
{
//SetTabI?.Invoke("add", this.Name);
if (SetTabI != null)
{
SetTabI.Invoke("add", this.Name);
//MessageBox.Show("add");
}
}
}
代码复用问题
上面所说的是针对父窗体包含子窗体的使用方式,在实际中会存在一个窗体可用于子窗体,也可用于单独窗体或单独项目中的启动窗体,如下面的窗体“数据对比”,它继承自MdiChildBase创建,当它用于一个不包含MDIParent的项目中(不包含对MDIParent的引用,因为MDIParent做为主窗体,可能所在项目包含了对所有窗体/类的引用),可借助MdiChildBase中的委托实例SetTabII来进行区分,具体信息见MdiChildBase类中SetTabII的文字说明。
//Form f = Application.OpenForms["数据对比"];
数据对比 f = new 数据对比(dt);
Form mDIParent = Application.OpenForms["MDIParent1"];
MDIParent1 mDIParent1 = Application.OpenForms["MDIParent1"] as MDIParent1;
MdiChildBase mdiChild = null;
if (f != null)
{
f.MdiParent = mDIParent;
mdiChild = f as MdiChildBase;
}
else
{
mdiChild = (MdiChildBase)Activator.CreateInstance(typeof(数据对比), true);
mdiChild.MdiParent = mDIParent;
}
mdiChild.SetTabI = MdiChildBase.SetTabII;
mdiChild.Show();
mdiChild.BringToFront();
mdiChild.Activate();