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

解决方案 »

  1.   

    增加3个bool的变量对应3个窗体,每次show出来都改变bool值,点另外按钮时,判断bool值,在关闭相应的窗体,打开需要打开的窗体??
      

  2.   


    如何使窗体show出来时改变bool值?
      

  3.   

    button事件,new 窗体对象后,在show之前改不行吗?
      

  4.   

    最笨的方法:
        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();
            }
        }
      

  5.   

    这样不能实现第二点呢
    2.如何使得点击按钮1出现form1,再点一下按钮1,关闭Form1.
      

  6.   

    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();
    }
    其它按钮事件类似。
      

  7.   


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