C# WinForm中,如何判断窗口已打开?

如题所述

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Form2 f;
private void button1_Click(object sender, EventArgs e)
{

if (f == null) //如果子窗体为空则创造实例 并显示
{
f = new Form2();
f.StartPosition = FormStartPosition.CenterScreen;//子窗体居中显示
f.Show();

}
else
{
if (f.IsDisposed)//若子窗体关闭 则打开新子窗体 并显示
{
f = new Form2();
f.StartPosition = FormStartPosition.CenterScreen;//子窗体居中显示
f.Show();
}
else {
f.Activate(); //使子窗体获得焦点
}
}
} }
温馨提示:内容为网友见解,仅供参考
无其他回答