获取MDI窗体的实例

View Code
  /// <summary>
        
/// 
        
/// </summary>
        
/// <param name="mdiParent"></param>
        
/// <param name="frmtype"></param>
        
/// <returns></returns>
        public static bool IsExistInstance(System.Windows.Forms.Form mdiParent, System.Type frmtype)
        {
            System.Windows.Forms.Form[] f = null;
            int i = 0;
            f = mdiParent.MdiChildren;
            for (i = 0; i <= f.Length - 1; i++)
            {
                if (f[i].GetType().Equals(frmtype))
                {
                    return true;
                }
            }
            return false;
        }


        /// <summary>
        
/// 
        
/// </summary>
        
/// <param name="mdiParent"></param>
        
/// <param name="midChild"></param>
        
/// <returns></returns>
        public static bool IsExistInstance(System.Windows.Forms.Form mdiParent, Form midChild)
        {
            System.Windows.Forms.Form[] f = null;
            int i = 0;
            f = mdiParent.MdiChildren;
            for (i = 0; i <= f.Length - 1; i++)
            {
                if (object.ReferenceEquals(f[i], midChild))
                {
                    return true;
                }
            }
            return false;
        }


        /// <summary>
        
/// 
        
/// </summary>
        
/// <param name="mdiParent"></param>
        
/// <param name="frmtype"></param>
        
/// <returns></returns>
        public static Form GetFormInstance(System.Windows.Forms.Form mdiParent, System.Type frmtype)
        {
            System.Windows.Forms.Form[] f = null;
            int i = 0;
            f = mdiParent.MdiChildren;
            for (i = 0; i <= f.Length - 1; i++)
            {
                if (f[i].GetType().Equals(frmtype))
                {
                    return f[i];
                }
            }
            return null;
        }

        /// <summary>
        
/// 
        
/// </summary>
        
/// <param name="mdiParent"></param>
        
/// <param name="midChild"></param>
        
/// <returns></returns>
        public static Form GetFormInstance(System.Windows.Forms.Form mdiParent, Form midChild)
        {
            System.Windows.Forms.Form[] f = null;
            int i = 0;
            f = mdiParent.MdiChildren;
            for (i = 0; i <= f.Length - 1; i++)
            {
                if (object.ReferenceEquals(f[i], midChild))
                {
                    return f[i];
                }
            }
            return null;
        }

调用:

           //检查指定MDI窗体的实例是否存在。
            IsExistInstance(this, typeof(WindowsFormsApplication2.Form1));
            //或者
            IsExistInstance(this, System.Type.GetType("WindowsFormsApplication2.Form1"));

 

 

确定两个 String 对象是否具有相同的值。

http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=ZH-CN&k=k(SYSTEM.STRING.EQUALS);k(EQUALS)&rd=true

ReferenceEquals 确定两个对象是否是相同的实例。

http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=ZH-CN&k=k(SYSTEM.OBJECT.REFERENCEEQUALS);k(REFERENCEEQUALS)&rd=true

is 检查对象是否与指定类型。

http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=ZH-CN&k=k(IS_CSHARPKEYWORD);k(IS)&rd=true

posted @ 2013-05-12 17:37  跟着阿笨一起玩.NET  阅读(345)  评论(0编辑  收藏  举报