如何在MDI中相同的子窗体只保留一个实例
很明显,在一个MDI窗体中,一般来说每个菜单都会对应一个Form,当用户多次单击某个菜单时,而我们并不需要同时弹出多个与之对应的Form。
所以我们在菜单事件上加上子窗体是否存在的验证就可以解决这个问题了。
代码如下:
方法一:
Code
/// <summary>
/// 检查MDI子窗体是否存在
/// </summary>
/// <param name="childFrmName"></param>
/// <returns></returns>
private bool IsChildFormExist(string childFrmName)
{
foreach (Form childFrm in this.MdiChildren)
{
if (childFrm.Name == childFrmName) //用子窗体的Name进行判断,如果存在则将他激活
{
if (childFrm.WindowState == FormWindowState.Minimized)
{
childFrm.WindowState = FormWindowState.Normal;
}
childFrm.Activate();
return true;
}
}
return false;
}
//调用示例如下
/// <summary>
/// 用户管理
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ToolStripMenuItem_UserManage_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(currentLoginName))
{
return;
}
//通过窗体名称查询该窗体是否已经存在,如存在则显示,否则就新创建一个
if (!this.IsChildFormExist("UserForm"))
{
UserForm frmUser = new UserForm();
frmUser.MdiParent = this;
frmUser.WindowState = FormWindowState.Maximized;
frmUser.Show();
}
}
/// <summary>
/// 检查MDI子窗体是否存在
/// </summary>
/// <param name="childFrmName"></param>
/// <returns></returns>
private bool IsChildFormExist(string childFrmName)
{
foreach (Form childFrm in this.MdiChildren)
{
if (childFrm.Name == childFrmName) //用子窗体的Name进行判断,如果存在则将他激活
{
if (childFrm.WindowState == FormWindowState.Minimized)
{
childFrm.WindowState = FormWindowState.Normal;
}
childFrm.Activate();
return true;
}
}
return false;
}
//调用示例如下
/// <summary>
/// 用户管理
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ToolStripMenuItem_UserManage_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(currentLoginName))
{
return;
}
//通过窗体名称查询该窗体是否已经存在,如存在则显示,否则就新创建一个
if (!this.IsChildFormExist("UserForm"))
{
UserForm frmUser = new UserForm();
frmUser.MdiParent = this;
frmUser.WindowState = FormWindowState.Maximized;
frmUser.Show();
}
}
方法二:
Code
/// <summary>
/// 打开窗体
/// </summary>
/// <param name="mdiChildTypeString"></param>
/// <returns></returns> private void ShowWindow(string mdiChildTypeString)
{
Form currentMdiChild = null;
if (!IsMDIChildFormExist(mdiChildTypeString))
{
Assembly assembly = Assembly.GetExecutingAssembly();
Type typForm = assembly.GetType(mdiChildTypeString);
//关于InvokeMember用法,不明白的可以查看:http://msdn.microsoft.com/zh-cn/library/de3dhzwy(VS.80).aspx
Object mdiChild = typForm.InvokeMember(
null,
BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.CreateInstance,
null,
null,
null);
if (mdiChild != null)
{
currentMdiChild = mdiChild as Form;
currentMdiChild.MdiParent = this;
currentMdiChild.WindowState = FormWindowState.Maximized;
currentMdiChild.Show();
currentMdiChild.Focus();
}
}
}
/// <summary>
/// 检查MDI子窗体是否存在
/// </summary>
/// <param name="mdiChildTypeString"></param>
/// <returns></returns>
private bool IsMDIChildFormExist(string mdiChildTypeString)
{
Form currentMDIChild = null;
foreach (Form mdiChildFrm in this.MdiChildren)
{
if (mdiChildFrm.GetType().ToString() == mdiChildTypeString)
{
currentMDIChild = mdiChildFrm;
break;
}
}
if (currentMDIChild != null)
{
if (currentMDIChild.WindowState == FormWindowState.Minimized)
{
currentMDIChild.WindowState = FormWindowState.Normal;
}
currentMDIChild.TopMost = true;
currentMDIChild.Activate();
currentMDIChild.Focus();
return true;
}
else
{
return false;
}
}
//调用方法如下
/// <summary>
/// 用户管理
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ToolStripMenuItem_UserManage_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(currentLoginName))
{
return;
}
ShowWindow(typeof(UserForm).ToString());
}
/// <summary>
/// 打开窗体
/// </summary>
/// <param name="mdiChildTypeString"></param>
/// <returns></returns> private void ShowWindow(string mdiChildTypeString)
{
Form currentMdiChild = null;
if (!IsMDIChildFormExist(mdiChildTypeString))
{
Assembly assembly = Assembly.GetExecutingAssembly();
Type typForm = assembly.GetType(mdiChildTypeString);
//关于InvokeMember用法,不明白的可以查看:http://msdn.microsoft.com/zh-cn/library/de3dhzwy(VS.80).aspx
Object mdiChild = typForm.InvokeMember(
null,
BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.CreateInstance,
null,
null,
null);
if (mdiChild != null)
{
currentMdiChild = mdiChild as Form;
currentMdiChild.MdiParent = this;
currentMdiChild.WindowState = FormWindowState.Maximized;
currentMdiChild.Show();
currentMdiChild.Focus();
}
}
}
/// <summary>
/// 检查MDI子窗体是否存在
/// </summary>
/// <param name="mdiChildTypeString"></param>
/// <returns></returns>
private bool IsMDIChildFormExist(string mdiChildTypeString)
{
Form currentMDIChild = null;
foreach (Form mdiChildFrm in this.MdiChildren)
{
if (mdiChildFrm.GetType().ToString() == mdiChildTypeString)
{
currentMDIChild = mdiChildFrm;
break;
}
}
if (currentMDIChild != null)
{
if (currentMDIChild.WindowState == FormWindowState.Minimized)
{
currentMDIChild.WindowState = FormWindowState.Normal;
}
currentMDIChild.TopMost = true;
currentMDIChild.Activate();
currentMDIChild.Focus();
return true;
}
else
{
return false;
}
}
//调用方法如下
/// <summary>
/// 用户管理
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ToolStripMenuItem_UserManage_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(currentLoginName))
{
return;
}
ShowWindow(typeof(UserForm).ToString());
}