如何使得 一个Form的弹出不影响另一个Form
有两个Form,点击button1出现Form1,Button2出现Form2,点击button3出现Form3…….问题是:
1.在Form1显示的时候,不用关闭Form1,点击其他按钮,关闭Form1并且,弹出另外的Form
2.如何使得点击按钮1出现form1,再点一下按钮1,关闭Form1.
[解决办法]
最笨的方法:
public partial class Form1 : Form
{
Form f2 = null;
Form f3 = null;
Form f4 = null;
private void button1_Click(object sender, EventArgs e)
{
f2 = new Form2();
f2.Show();
if (f3 is Form)
f3.Close();
if (f4 is Form)
f4.Close();
}
private void button2_Click(object sender, EventArgs e)
{
f3 = new Form3();
f3.Show();
if (f2 is Form)
f2.Close();
if (f4 is Form)
f4.Close();
}
private void button3_Click(object sender, EventArgs e)
{
f4 = new Form4();
f4.Show();
if (f2 is Form)
f2.Close();
if (f3 is Form)
f3.Close();
}
}
void button1_Click(object sender, EventArgs e)
{
if(this.OwnedForms.Count>1)
{
Form oldform = this.OwnedForms[0];
oldform.Close();
if(oldform is Form1)
return;//如果关闭的窗口就是当前要打开的窗口,直接返回。
}
Form1 f = new Form1();
f.Owner = this;
f.Show();
}
{
frmTwo = FormTwo.GetForm();
frmTwo.Owner = this;
frmTwo.Show();
}
}
private void btn_form3_Click(object sender, EventArgs e)
{
if (frmTwo != null)
{
frmTwo.Close();
frmTwo = null;
}
else if (frmOne != null)
{
frmOne.Close();
frmOne = null;
}
else if (frmThree == null)
{
frmThree = FormThree.GetForm();
frmThree.Owner = this;
frmThree.Show();
}
}
测试过的