`
king_tt
  • 浏览: 2107181 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

C#之Winform中防止重复打开同一子窗体

 
阅读更多

我封装为了一个函数,放在MDI主窗体代码中即可调用

/// <summary>
        /// 在MDI父窗体中打开子窗体,判断是否已经重复打开
        /// 如果已经打开,则激活这个子窗体,否则返回false值。
        /// </summary>
        /// <param name="p_ChildrenFormText"></param>
        /// <returns></returns>
        private bool showChildrenForm(string p_ChildrenFormName)
        {
            int i;
            //依次检测当前窗体的子窗体
            for (i = 0; i < this.MdiChildren.Length; i++)
            {
                //判断当前子窗体的name属性值是否与传入的字符串值相同
                if (this.MdiChildren[i].Name == p_ChildrenFormName)
                {
                    //此子窗体是目标子窗体,激活之
                    this.MdiChildren[i].Activate();
                    return true;
                }
            }

调用示例

private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if (!showChildrenForm("AccountForm"))
            {
                AccountForm eq_account = new AccountForm();//AccountForm为想检测的子窗体
                eq_account.MdiParent = this;
                eq_account.Show();
            }
        }


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics