首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

怎么使得 一个Form的弹出不影响另一个Form

2013-04-12 
如何使得 一个Form的弹出不影响另一个Form有两个Form,点击button1出现Form1,Button2出现Form2,点击button3

如何使得 一个Form的弹出不影响另一个Form
有两个Form,点击button1出现Form1,Button2出现Form2,点击button3出现Form3…….问题是:
1.在Form1显示的时候,不用关闭Form1,点击其他按钮,关闭Form1并且,弹出另外的Form
2.如何使得点击按钮1出现form1,再点一下按钮1,关闭Form1.
怎么使得 一个Form的弹出不影响另一个Form
怎么使得 一个Form的弹出不影响另一个Form怎么使得 一个Form的弹出不影响另一个Form怎么使得 一个Form的弹出不影响另一个Form

[解决办法]
最笨的方法:

    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();
}


其它按钮事件类似。
[解决办法]
引用:
有两个Form,点击button1出现Form1,Button2出现Form2,点击button3出现Form3…….问题是:
1.在Form1显示的时候,不用关闭Form1,点击其他按钮,关闭Form1并且,弹出另外的Form
2.如何使得点击按钮1出现form1,再点一下按钮1,关闭Form1.


 private void btn_form1_Click(object sender, EventArgs e)
        {
            if (frmThree != null)
            {
                frmThree.Close();
                frmThree = null;
            }
            else if (frmTwo != null)
            {
                frmTwo.Close();
                frmTwo = null;
            }
            else if (frmOne == null)
            {
                frmOne = FormOne.GetForm();
                frmOne.Owner = this;
                frmOne.Show();
            }
        }

        private void btn_form2_Click(object sender, EventArgs e)
        {
            if (frmThree != null)
            {
                frmThree.Close();
                frmThree = null;
            }
            else if (frmOne != null)
            {
                frmOne.Close();
                frmOne = null;
            }
            else if (frmTwo == null)


            {
                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();
            }
        }
测试过的

热点排行