winform判断一个窗口是否打开并显示出来

判断一个窗口是否打开,和显示当前窗口:

form1中声明:private Form2 f2;

在需要弹出窗口的处理上做如下修正:

if (f2 == null||f2.IsDisposed) { f2 = new Form2(); f2.Show(); } else { f2.Activate(); }  

完成代码如下:

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace chxboxs { public partial class Form1 : Form { private Form2 f2; public Form1() { InitializeComponent(); } private void Form1_KeyDown(object sender, KeyEventArgs e) { this.label1.Text = e.KeyCode.ToString(); } private void button1_Click(object sender, EventArgs e) { if (f2 == null||f2.IsDisposed) { f2 = new Form2(); f2.Show(); } else { f2.Activate(); } } } } 

你可能感兴趣的:(winform判断一个窗口是否打开并显示出来)