mdi子窗体只保留一个实例
今天做mdi程序,一开始用 new 方法声明子窗口 来显示 发现同一个子窗体可以开n遍。所以找了些方法 设置子窗体只保留一个实例
今天做mdi程序,一开始用 new 方法声明子窗口 来显示 发现同一个子窗体可以开n遍。所以找了些方法 设置子窗体只保留一个实例 。这用到了反射的原理。反射我也是刚接触 不大明白,只是比葫芦画瓢。
方法可能不是很好,但还是贴出来分享一下:
上面提供了的代码直接可以应用(给出了设置 子窗体的标题 和给子窗体传值的方法,随便写的 用tag是不是不好我没有仔细想,但是这也是个思路,希望高手不吝赐教!!谢了先。)
另外我还看到一些朋友想实现 如果子窗体存在 只刷新子窗体,这里调用这个方法也可以 你只需要在子窗体的Activated方法中写你要刷的方法就可以了。
方法可能不是很好,但还是贴出来分享一下:
winform mdi#region winform mdi
/**//*
* 作者 :wts
* 日期 :2007-03-15
* 功能 :解决winform中mdi 子窗体只保留一个的问题
* */
/**//// <summary>
/// 打开mdi子窗体
/// </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= null;
try
{
obj = typForm.InvokeMember(null, BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.CreateInstance, null, null,null );
}
catch (MissingMethodException e)
{
MessageBox.Show(e.ToString());
}
// Show child form
if( obj != null )
{
myChild = obj as Form;
myChild.MdiParent = this;
myChild.Show();
myChild.Focus();
}
}
}
/**//// <summary>
/// 打开子窗口
/// </summary>
/// <param name="ChildTypeString">子窗口名</param>
/// <param name="name">子窗口标题</param>
/// <param name="ChildFormText">子窗体标题</param>
/// <param name="ChildFormArg">要传到子窗体的参数</param>
private void OpenWindow( string ChildTypeString ,string ChildFormText,object [] ChildFormArg )
{
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= null;
try
{
obj = typForm.InvokeMember(null, BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.CreateInstance, null, null,null );
}
catch (MissingMethodException e)
{
MessageBox.Show(e.ToString());
}
// Show child form
if( obj != null )
{
myChild = obj as Form;
myChild.MdiParent = this;
myChild.Text = ChildFormText;
myChild.Tag = ChildFormArg;
myChild.Show();
myChild.Focus();
}
}
}
/**//// <summary>
/// 打开子窗口
/// </summary>
/// <param name="ChildTypeString">子窗口名</param>
/// <param name="name">子窗口标题</param>
/// <param name="ChildFormText">子窗体标题</param>
/// <param name="ChildFormArg">要传到子窗体的参数</param>
private void OpenWindow( string ChildTypeString ,string ChildFormText,object ChildFormArg )
{
Form myChild = null;
if( !ContainMDIChild( ChildTypeString ) )
{
// 得到当前线程的集合
Assembly assembly = Assembly.GetExecutingAssembly();
// 根据传进来的参数 创建类型
Type typForm = assembly.GetType( ChildTypeString );
// 根据类型(type)的 "InvokeMember" 方法创建object对象
Object obj= null;
try
{
obj = typForm.InvokeMember(null, BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.CreateInstance, null, null,null );
}
catch (MissingMethodException e)
{
MessageBox.Show(e.ToString());
}
//显示子窗体
if( obj != null )
{
myChild = obj as Form;
myChild.MdiParent = this;
myChild.Text = ChildFormText;
myChild.Tag = ChildFormArg;
myChild.Show();
myChild.Focus();
}
}
}
/**//// <summary>
/// 判断子窗体是否存在
/// </summary>
/// <param name="ChildTypeString">子窗口名</param>
/// <returns></returns>
private bool ContainMDIChild( string ChildTypeString )
{
Form myMDIChild = null;
foreach(Form form in this.MdiChildren)
{
if( form.GetType().ToString() == ChildTypeString )
{
// 查找子窗体
myMDIChild = form;
break;
}
}
// 显示已经存在的窗体
if( myMDIChild != null)
{
myMDIChild.TopMost = true;
myMDIChild.Show();
myMDIChild.Focus();
return true;
}
else
return false;
}
#endregion winform mdi
示例代码/**//*
* 作者 :wts
* 日期 :2007-03-15
* 功能 :解决winform中mdi 子窗体只保留一个的问题
* */
/**//// <summary>
/// 打开mdi子窗体
/// </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= null;
try
{
obj = typForm.InvokeMember(null, BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.CreateInstance, null, null,null );
}
catch (MissingMethodException e)
{
MessageBox.Show(e.ToString());
}
// Show child form
if( obj != null )
{
myChild = obj as Form;
myChild.MdiParent = this;
myChild.Show();
myChild.Focus();
}
}
}
/**//// <summary>
/// 打开子窗口
/// </summary>
/// <param name="ChildTypeString">子窗口名</param>
/// <param name="name">子窗口标题</param>
/// <param name="ChildFormText">子窗体标题</param>
/// <param name="ChildFormArg">要传到子窗体的参数</param>
private void OpenWindow( string ChildTypeString ,string ChildFormText,object [] ChildFormArg )
{
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= null;
try
{
obj = typForm.InvokeMember(null, BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.CreateInstance, null, null,null );
}
catch (MissingMethodException e)
{
MessageBox.Show(e.ToString());
}
// Show child form
if( obj != null )
{
myChild = obj as Form;
myChild.MdiParent = this;
myChild.Text = ChildFormText;
myChild.Tag = ChildFormArg;
myChild.Show();
myChild.Focus();
}
}
}
/**//// <summary>
/// 打开子窗口
/// </summary>
/// <param name="ChildTypeString">子窗口名</param>
/// <param name="name">子窗口标题</param>
/// <param name="ChildFormText">子窗体标题</param>
/// <param name="ChildFormArg">要传到子窗体的参数</param>
private void OpenWindow( string ChildTypeString ,string ChildFormText,object ChildFormArg )
{
Form myChild = null;
if( !ContainMDIChild( ChildTypeString ) )
{
// 得到当前线程的集合
Assembly assembly = Assembly.GetExecutingAssembly();
// 根据传进来的参数 创建类型
Type typForm = assembly.GetType( ChildTypeString );
// 根据类型(type)的 "InvokeMember" 方法创建object对象
Object obj= null;
try
{
obj = typForm.InvokeMember(null, BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.CreateInstance, null, null,null );
}
catch (MissingMethodException e)
{
MessageBox.Show(e.ToString());
}
//显示子窗体
if( obj != null )
{
myChild = obj as Form;
myChild.MdiParent = this;
myChild.Text = ChildFormText;
myChild.Tag = ChildFormArg;
myChild.Show();
myChild.Focus();
}
}
}
/**//// <summary>
/// 判断子窗体是否存在
/// </summary>
/// <param name="ChildTypeString">子窗口名</param>
/// <returns></returns>
private bool ContainMDIChild( string ChildTypeString )
{
Form myMDIChild = null;
foreach(Form form in this.MdiChildren)
{
if( form.GetType().ToString() == ChildTypeString )
{
// 查找子窗体
myMDIChild = form;
break;
}
}
// 显示已经存在的窗体
if( myMDIChild != null)
{
myMDIChild.TopMost = true;
myMDIChild.Show();
myMDIChild.Focus();
return true;
}
else
return false;
}
#endregion winform mdi
上面提供了的代码直接可以应用(给出了设置 子窗体的标题 和给子窗体传值的方法,随便写的 用tag是不是不好我没有仔细想,但是这也是个思路,希望高手不吝赐教!!谢了先。)
另外我还看到一些朋友想实现 如果子窗体存在 只刷新子窗体,这里调用这个方法也可以 你只需要在子窗体的Activated方法中写你要刷的方法就可以了。