出处:http://dev.csdn.net/author/Knight94/acd529d8a91a42f6ba5996babf1395d4.html
作者:愚翁
首先是通过子窗体类型名来判断是否打开新的子窗体,还是把原有的子窗体进行显示。
1/// <summary>
2/// Open child window
3/// </summary>
4/// <param name="ChildTypeString"></param>
5private void OpenWindow(string ChildTypeString)
6{
7 Form myChild = null;
8 if (!ContainMDIChild(ChildTypeString))
9 {
10 // Get current process assembly
11 Assembly assembly = Assembly.GetExecutingAssembly();
12
13 // Create data type using type string
14 Type typForm = assembly.GetType(ChildTypeString);
15
16 // Create object using type's "InvokeMember" method
17 Object obj = typForm.InvokeMember(null, BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.CreateInstance, null, null, null);
18
19 // Show child form
20 if (obj != null)
21 {
22 myChild = obj as Form;
23 myChild.MdiParent = this;
24 myChild.Show();
25 myChild.Focus();
26 }
27 }
28}
29
30/// <summary>
31/// Search mdi child form by specific type string
32/// </summary>
33/// <param name="ChildTypeString"></param>
34/// <returns><eturns>
35private bool ContainMDIChild(string ChildTypeString)
36{
37 Form myMDIChild = null;
38 foreach (Form f in this.MdiChildren)
39 {
40 if (f.GetType().ToString() == ChildTypeString)
41 {
42 // found it
43 myMDIChild = f;
44 break;
45 }
46 }
47
48 // Show the exist form
49 if (myMDIChild != null)
50 {
51 myMDIChild.TopMost = true;
52 myMDIChild.Show();
53 myMDIChild.Focus();
54 return true;
55 }
56 else
57 return false;
58}
2/// Open child window
3/// </summary>
4/// <param name="ChildTypeString"></param>
5private void OpenWindow(string ChildTypeString)
6{
7 Form myChild = null;
8 if (!ContainMDIChild(ChildTypeString))
9 {
10 // Get current process assembly
11 Assembly assembly = Assembly.GetExecutingAssembly();
12
13 // Create data type using type string
14 Type typForm = assembly.GetType(ChildTypeString);
15
16 // Create object using type's "InvokeMember" method
17 Object obj = typForm.InvokeMember(null, BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.CreateInstance, null, null, null);
18
19 // Show child form
20 if (obj != null)
21 {
22 myChild = obj as Form;
23 myChild.MdiParent = this;
24 myChild.Show();
25 myChild.Focus();
26 }
27 }
28}
29
30/// <summary>
31/// Search mdi child form by specific type string
32/// </summary>
33/// <param name="ChildTypeString"></param>
34/// <returns><eturns>
35private bool ContainMDIChild(string ChildTypeString)
36{
37 Form myMDIChild = null;
38 foreach (Form f in this.MdiChildren)
39 {
40 if (f.GetType().ToString() == ChildTypeString)
41 {
42 // found it
43 myMDIChild = f;
44 break;
45 }
46 }
47
48 // Show the exist form
49 if (myMDIChild != null)
50 {
51 myMDIChild.TopMost = true;
52 myMDIChild.Show();
53 myMDIChild.Focus();
54 return true;
55 }
56 else
57 return false;
58}
以上两部分就可以对于每个子窗体只创建一个实例。那么调用以上代码就非常简单了。
如:
//Open a mdi child form which type named "MDIChild"
OpenWindow( typeof( MDIChild ).ToString() );
OpenWindow( typeof( MDIChild ).ToString() );
点评:个人认为这种方法要优于其它网上可找到的方法,通过使用反射技术使代码得到进一步重构,更加简洁。有一点小小的意见就是,其中的OpenWindow方法其名称改为ShowWindow似乎更恰当一些。 ——by青弦
却原来、姹紫嫣红开遍,似这般、都付与断井颓痕 ——《昆曲·游园惊梦》