我在主窗口mainForm上加了个按钮,点击事件是new一个Form2对象,然后显示
private void toolStripButton11_Click(object sender, EventArgs e)
{
PlayDemoForm rf = PlayDemoForm.getPlayDemoForm();
rf.Show();
}我现在想让Form2只开一次,如果再点这个按钮就不能再打开了,同时如果rf被关闭了,又可以重新打开。请问我需要怎么做呢?
private void toolStripButton11_Click(object sender, EventArgs e)
{
PlayDemoForm rf = PlayDemoForm.getPlayDemoForm();
rf.Show();
}我现在想让Form2只开一次,如果再点这个按钮就不能再打开了,同时如果rf被关闭了,又可以重新打开。请问我需要怎么做呢?
foreach (Form form in Application.OpenForms)
{
if (form.Text == "你开的窗口名"
{
return;
}
}
或者用一个list把打开的窗口存起来,关闭之后取出,每次打开之前检索list,如果存在就不开了,方法很多的
1 全局变量来放你的 PlayDemoForm . 对Form进行判断.2 在Application.OpenForms[""]里找PlayDemoForm 的名字 3 ShowDialog
{
if (f1 is Form2)
f1.Close();
}
Form2 fm = new Form2();
fm.Show(this);
Form2 newform=null;
private void button_Click(object sender, EventArgs e)
{
if(newform==null)
{
newform=new Form2();
newform.ShowDialog();
}
}
//父窗体
public partial class Form1 : Form
{
private void button1_Click(object sender, EventArgs e)
{
Form2.ShowChildForm();
}
}
public partial class Form2 : Form
{
private static Form2 formInstance; public static Form2 Instance
{
get
{
if(formInstance == null||formInstance.IsDisposed)
{
formInstance = new Form2();
}
return formInstance;
}
} public static void ShowChildForm()
{
if(!Instance.Visible)
{
Instance.Show();
}
}
}
2.对ShowDialog()的结果进行判断。
Application.OpenForms[""]很好很强大private void toolStripButton11_Click(object sender, EventArgs e)
{
Form f = Application.OpenForms["PlayDemoForm"];
if(f==null)
{
PlayDemoForm rf = new PlayDemoForm();
rf.Show();
}
}上边是我的代码
private static frmLabel label; private frmLabel()
{
InitializeComponent();
} public static frmLabel CreateForm()
{
lock (typeof(frmLabel))
{
if (label == null)
{
label = new frmLabel();
}
return label;
}
}
必须重写ONCLOSEING事件
label = null你多文档的话,不能使用SHOWDIALOG的