频道导航.NET数据库Java网络编程语言WEB开发重点导航热门下载最新下载最新编程资料热门编程文章编程工具大集合下载精品电子书教程下载
  站长作品     虚拟主机   广告联系       计算机毕业论文下载
全站文字链广告30元每月 全站文字链广告30元每月 全站文字链广告30元每月 全站文字链广告30元每月 全站文字链广告30元每月 全站文字链广告30元每月
[在线教程]: [ 11天学会C编程] [ Linux 常用C函数(中文版)] [ C语言编程宝典] [ 高质量C++/C编程指南] [ Linux内核编程] [ Python基础教程] [ Python教程] [ Python编程经典] [ Perl经典用法] [ 经典JavaScript教程] [ ASP.NET中文教程]
首页>>开发资料>>.NET编程>> 如何在MDI中使子窗体只保留一个实例

如何在MDI中使子窗体只保留一个实例 增强型1G空间特价130元!

点击: 26257
发布时间:2008-4-6 0:00:00
作者: 编程联盟整理
来源:互联网


进入社区讨论    月付优惠主机   定做出售计算机毕业论文 

由于在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() );

文章来源:http://www.bcbbs.net/news/Content.aspx?id=8
文章来源:http://www.bcbbs.net/news/Content.aspx?id=8
收藏本文章 关闭本页
用户讨论区
昵称:   您还没有登录不能评论
评论:  
验证码:
用户登录 账号:  密码:   还没注册?
 
视频教程推荐
相关资料站点
虚拟主机
相关资料站点
精彩推荐