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(); //使子窗體得到焦點
                }
            }
        }
相關文章
相關標籤/搜索