最近有个WINFORM项目要做,布局要像下面这样的。
但是有一个问题,就是要判断窗口是否已打开,看了网上的一些代码,这样的:
private
void
ShowNewForm(
object
sender, EventArgs e)
{
if (FindDocument( " BaseForm " ) == null )
{
BaseForm childForm = new BaseForm();
childForm.Show(dockPanel1);
}
else
{
Form f = FindDocument( " BaseForm " ) as Form;
f.Focus();
}
}
/// 在dockPanel中查找已经打开的窗口
/// </summary>
/// <param name="text"> 传入的窗口标题 </param>
/// <returns> 返回的窗口 </returns>
private IDockContent FindDocument( string text)
{
if (dockPanel1.DocumentStyle == DocumentStyle.SystemMdi)
{
foreach (Form form in MdiChildren)
if (form.Text == text)
return form as IDockContent;
return null ;
}
else
{
foreach (IDockContent content in dockPanel1.Documents)
if (content.DockHandler.TabText == text)
return content;
return null ;
}
}
{
if (FindDocument( " BaseForm " ) == null )
{
BaseForm childForm = new BaseForm();
childForm.Show(dockPanel1);
}
else
{
Form f = FindDocument( " BaseForm " ) as Form;
f.Focus();
}
}
/// 在dockPanel中查找已经打开的窗口
/// </summary>
/// <param name="text"> 传入的窗口标题 </param>
/// <returns> 返回的窗口 </returns>
private IDockContent FindDocument( string text)
{
if (dockPanel1.DocumentStyle == DocumentStyle.SystemMdi)
{
foreach (Form form in MdiChildren)
if (form.Text == text)
return form as IDockContent;
return null ;
}
else
{
foreach (IDockContent content in dockPanel1.Documents)
if (content.DockHandler.TabText == text)
return content;
return null ;
}
}
看了代码相信都能理解了吧!