出处:http://dev.csdn.net/author/Knight94/acd529d8a91a42f6ba5996babf1395d4.html
作者:愚翁
首先是通过子窗体类型名来判断是否打开新的子窗体,还是把原有的子窗体进行显示。
1
/// <summary>
2
/// Open child window
3
/// </summary>
4
/// <param name="ChildTypeString"></param>
5
private 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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
// Create data type using type string
14
Type typForm = assembly.GetType(ChildTypeString);
15![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
30
/// <summary>
31
/// Search mdi child form by specific type string
32
/// </summary>
33
/// <param name="ChildTypeString"></param>
34
/// <returns><eturns>
35
private 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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
35
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
36
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
37
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
39
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
40
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
42
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
43
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
44
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
45
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
46
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
47
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
48
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
49
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
50
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
51
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
52
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
53
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
54
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
55
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
56
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
57
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
58
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
以上两部分就可以对于每个子窗体只创建一个实例。那么调用以上代码就非常简单了。
如:
//Open a mdi child form which type named "MDIChild"
OpenWindow( typeof( MDIChild ).ToString() );
OpenWindow( typeof( MDIChild ).ToString() );
点评:个人认为这种方法要优于其它网上可找到的方法,通过使用反射技术使代码得到进一步重构,更加简洁。有一点小小的意见就是,其中的OpenWindow方法其名称改为ShowWindow似乎更恰当一些。 ——by青弦
却原来、姹紫嫣红开遍,似这般、都付与断井颓痕 ——《昆曲·游园惊梦》