单击主窗体的一个按钮,在单击事件下这样写:
先检查Form2有没有打开,如果打开,则关闭;
如果没有,则打开,即:
Form2 frm=new Form2();
frm.show();
请问,怎样检查Form2有没有打开?
谢谢!
先检查Form2有没有打开,如果打开,则关闭;
如果没有,则打开,即:
Form2 frm=new Form2();
frm.show();
请问,怎样检查Form2有没有打开?
谢谢!
能说具体点吗?谢谢了!
application前面有什么吗?是主窗体类名吗?
foreach (Form f in forms)
{
if (f is Form2)
;// your code
}
foreach (Form2 frm in Application.OpenForms)
{
frm.Close();
}
这段代码不能写在主窗体下面啊!因为Application指的是是主窗体啊!
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
private Form2 winForm = null;
private bool state = false;
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
if (winForm == null)
{
winForm = new Form2();
state = true;
winForm.Show();
}
else
{
if (state)
winForm.Show();
else
winForm.Hide();
} state = !state; }
}
}
bool flag=false;
foreach(Form form in application.openforms)
{
if(form is Form2)
{
flag=true;
form.close();
}
}
if(!flag)
{
Form2 frm=new Form2();
frm.show();
}
试下
if (Form2==null)
{
Form2=new form2();
Form2.show();
}
else
{
Form2.Close();}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
private Form2 winForm = null;
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
if (winForm == null)
{
winForm = new Form2();
winForm.Show();
}
else
{
winForm.Dispose();
winForm = null;
}
}
}
}
public Form2 frm2 = null;
private void button1_Click(object sender, EventArgs e)
{
if (frm2==null)
{
frm2 = new Form2();
frm2.frm1 = this;
frm2.Show();
}
else
{
frm2.Close();
}
}
Form2中
public Form1 frm1 = null; //Form2关闭事件
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
frm1.frm2 = null;
}
foreach (Form f in forms)
{
if (f is Form2)
{
f.Close();
break;
}
}
foreach (Form childrenForm in this.MdiChildren)
{
//检测是不是当前子窗体名称
if (childrenForm.Name == "子窗体名称")
{
//是的话就是把他显示
childrenForm.Visible = true;
//并激活该窗体
childrenForm.Activate();
return;
}
}
//下面是打开子窗体
Form1 childrenForm = new Form1();
childrenForm.MdiParent = this;
childrenForm.Show();
childrenForm.WindowState = FormWindowState.Maximized;
本贴来自ZDNetChina中文社区 http://bbs.zdnet.com.cn ,本贴地址:http://bbs.zdnet.com.cn/viewthread.php?tid=878916