C# winform判断窗体是否已打开

    Form1 form;
        /// <summary>
        /// 开始检测
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            if (form==null) //如果子窗体为空则创造实例 并显示
            {
                form = new Form1();
                form.StartPosition = FormStartPosition.CenterScreen;//子窗体居中显示
                form.Show();
            }
            else
            {
                if (form.IsDisposed) //若子窗体关闭 则打开新子窗体 并显示
                {
                    form = new Form1();
                    form.StartPosition = FormStartPosition.CenterScreen;//子窗体居中显示
                    form.Show();
                }
                else
                {
                    form.Activate(); //使子窗体获得焦点
                }
            }
        }
posted @ 2019-08-26 11:01  幽冥狂_七  阅读(15072)  评论(0编辑  收藏  举报