黑马程序员技术交流社区
标题:
mdi窗体
[打印本页]
作者:
lyz1024
时间:
2012-1-24 14:32
标题:
mdi窗体
不重复打开同一个窗体?如果重复打开已打开的子窗体,把该子窗体设为活动窗体
作者:
岳喜礼
时间:
2012-2-1 23:45
本帖最后由 岳喜礼 于 2012-2-1 23:46 编辑
/// <summary>
/// Open child window
/// </summary>
/// <param name="ChildTypeString"></param>
private void OpenWindow(string ChildTypeString)
{
Form myChild = null;
if (!ContainMDIChild(ChildTypeString))
{
// Get current process assembly
Assembly assembly = Assembly.GetExecutingAssembly();
// Create data type using type string
Type typForm = assembly.GetType(ChildTypeString);
// Create object using type's "InvokeMember" method
Object obj = typForm.InvokeMember(
null,
BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.CreateInstance,
null,
null,
null);
// Show child form
if (obj != null)
{
myChild = obj as Form;
myChild.MdiParent = this;
myChild.WindowState = FormWindowState.Maximized;
myChild.Show();
myChild.Focus();
}
}
}
/// <summary>
/// Search mdi child form by specific type string
/// </summary>
/// <param name="ChildTypeString"></param>
/// <returns></returns>
private bool ContainMDIChild(string ChildTypeString)
{
Form myMDIChild = null;
foreach (Form f in this.MdiChildren)
{
if (f.GetType().ToString() == ChildTypeString)
{
myMDIChild = f;
break;
}
}
// Show the exist form
if (myMDIChild != null)
{
myMDIChild.TopMost = true;
myMDIChild.WindowState = FormWindowState.Maximized;
myMDIChild.Show();
myMDIChild.Focus();
return true;
}
else
return false;
}
打开窗体的按钮下
OpenWindow(typeof(MainChild).ToString());//MainChild为子窗体名
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2