日期:2014-05-18 浏览次数:20893 次
Form f2 = new Form(); private void button1_Click(object sender, EventArgs e) { f2.Show(); }
------解决方案--------------------
Form f2; private void toolStripMenuItem1_Click(object sender, EventArgs e) { if(!f2.Created) { f2=new Form(); f2.Show(); } }
------解决方案--------------------
单件模式
public class Form2() { private Form2 f2; private Form2() { //构造函数声明为私有 } public static Form2 GetForm2() { if(this.f2==null) { this.f2=new Form2(); } return this.f2; } }
------解决方案--------------------
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
if(f2==null||f2.Disposed)
{Form f2 = new Form(); }
f2.Show();
}
------解决方案--------------------
参考如下:
private Form f2 = null; private void toolStripMenuItem1_Click(object sender, EventArgs e) { if (f2 == null || f2.IsDisposed) { f2 = new Form(); } f2.Show(); }
------解决方案--------------------
Form f2 = null;
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
if (f2 == null || f2.IsDisposed)
{
f2 = new Form();
}
f2.Show();
}
------解决方案--------------------
如果是MDI窗体可参考下
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
Form f2 = new Form();
f2.Show();
foreach (Form form in this.MdiChildren)
{ if (form is Form)
{
form.Focus();
return;
}
}
Form f2 = new Form();
f2.MdiParent = this;
f2.Show();
}