如何在MDI中使子窗体只保留一个实例
2013-01-30 15:25
267 查看
http://blog.csdn.net/knight94/article/details/742324
由于在MDI中,子窗体会很多,那么在我以前一篇文章中所提到的方法就有些繁琐了。为了能适应多个子窗体,并对于每个子窗体只保留一个实例,那么我就用一个新的方法来替换,即用反射方法来作处理。
大致代码如下。
首先是通过子窗体类型名来判断是否打开新的子窗体,还是把原有的子窗体进行显示。
using System.Reflection;
///<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.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 )
{
// found it
myMDIChild = f;
break;
}
}
// Show the exist form
if( myMDIChild !=
null)
{
myMDIChild.TopMost = true;
myMDIChild.Show();
myMDIChild.Focus();
return
true;
}
else
return
false;
}
以上两部分就可以对于每个子窗体只创建一个实例。那么调用以上代码就非常简单了。
如:
//Open a mdi child form which type named "MDIChild"
OpenWindow( typeof( MDIChild ).ToString() );
由于在MDI中,子窗体会很多,那么在我以前一篇文章中所提到的方法就有些繁琐了。为了能适应多个子窗体,并对于每个子窗体只保留一个实例,那么我就用一个新的方法来替换,即用反射方法来作处理。
大致代码如下。
首先是通过子窗体类型名来判断是否打开新的子窗体,还是把原有的子窗体进行显示。
using System.Reflection;
///<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.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 )
{
// found it
myMDIChild = f;
break;
}
}
// Show the exist form
if( myMDIChild !=
null)
{
myMDIChild.TopMost = true;
myMDIChild.Show();
myMDIChild.Focus();
return
true;
}
else
return
false;
}
以上两部分就可以对于每个子窗体只创建一个实例。那么调用以上代码就非常简单了。
如:
//Open a mdi child form which type named "MDIChild"
OpenWindow( typeof( MDIChild ).ToString() );
相关文章推荐
- 如何在MDI中使子窗体只保留一个实例
- 如何在MDI中使子窗体只保留一个实例
- [转载]如何在MDI中使子窗体只保留一个实例(反射方法)
- [转载]如何在MDI中使子窗体只保留一个实例(反射方法)
- 如何在MDI中相同的子窗体只保留一个实例
- 如何在MDI中相同的子窗体只保留一个实例
- C#反射方法来处理每个子窗体只保留一个实例
- 实例:如何制作一个带启动屏幕的窗体
- 正在学设计模式呵,转载一篇如何保证一个窗体的实例运行
- 如何只保留一个应用程序实例
- 如何禁用窗口关闭按钮在一个 MFC 多文档界面 (MDI) 应用程序
- c#中如何在一个panel中放入窗体
- java 如何为一个窗体设置背景图片
- 实例化了 一个 新的窗体 form2,并且show()了出来 刷新
- 如何在一个窗体中调用另一个窗体的控件或方法
- [VB.NET][问]如何让父窗体响应子窗体中的某一个自定义事件
- 如何实现MDI窗体不重复打开同一个子窗体?
- 如何删除一个标签,但是保留里面的内容?
- 如何使应用程序只运行一个实例
- 分析RSSOwl如何做到只运行一个实例,并且可以激活