一个程序有2个窗体,FORM1和FORM2,每个窗体上都有一个BUTTON,我想在运行后显示FORM1,隐藏FORM2,然后单击FORM1上的BUTTON,FORM1隐藏(也可以是关闭,但整个程序不退出),显示FORM2,单击FORM2上的BUTTON,隐藏FORM2(也可以是关闭),显示FORM1.
该怎么实现这样的功能..

解决方案 »

  1.   

    Timer控件控制要隐藏的窗体的位置
      

  2.   

    两个窗体互相持有对对方的引用
    1.点击按钮的时候,隐藏自身,显示对方。
    2.或者,点击按钮的时候,隐藏自身,处理对方的VisibleChanged事件(关闭的时候是Closing事件)。
      

  3.   

        1楼的,这个关timer什么事啊???
         namespace WindowsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                Form2 f = new Form2();
                this.Hide();
                f.Show();
            }
        }
    }     另一个窗体也用同样的代码既可``` 不过个人认为隐藏似乎不太好,因为当form1处于隐藏状态,关闭form2的时候,form1并未关闭````````不空易发现``````
      

  4.   

    public class Form1
    {
        public static Form1 Instance;
        public Form1(){Instance =this;}
    }
    用Form1.Instance.Show/Hide去控制就好了.
      

  5.   

    这个问题昨天有人提过了啊。
    主要看form1是不是主窗体(也就是主线程),如果是的话就不能关闭(close),只能隐藏(hide)。
      

  6.   

    一个Hide,一个Show,另一个Hide,另一个Show
      

  7.   

    修改主程序,实例化两个窗口(program.cs)Form1 fm1 = new Form1();
    Form2 fm2 = new Form2();然后把application.run(new Form1())这一句改成:
    application.run(fm1);这样程序启动就实现了显示Form1,隐藏Form2.剩下的在按钮的click事件里写,就是上面几楼说的Hide,Show.
      

  8.   

    4楼的代码可以做到,但每次隐藏/显示,都是新建了一个实例,而不是显示/隐藏原FORM,
    5楼的方法只能隐藏自身..但怎么现实另一个窗体呢..
    9楼的方法,按照你所写的修改了program.cs之后程序无法运行,一运行就提示遇到错误需要关闭.
    我想要实现的是2个FORM,单击每个FORM上的按钮,就隐藏自身,显示对方,而不是每次都要从新再实例化一次...
      

  9.   

    加个判断if(f1!=null)f1.show,就不会出现每次实例化一次的事了~
      

  10.   

    楼主用这个吧
    窗体一的代码:
                      private System.Windows.Forms.Button button1;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; public Form1(System.Windows.Forms.Form frm)
    {
    InitializeComponent();
    }
    public Form1()
    {
    InitializeComponent();
    }

    /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.button1 = new System.Windows.Forms.Button();
    this.SuspendLayout();
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(192, 96);
    this.button1.Name = "button1";
    this.button1.TabIndex = 0;
    this.button1.Text = "button1";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(292, 273);
    this.Controls.Add(this.button1);
    this.Name = "Form1";
    this.Text = "Form1";
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void button1_Click(object sender, System.EventArgs e)
    {
    Form2 ff=new Form2 (this);
    ff.Show();
    this.Hide();
    }
    窗体二的代码:private System.Windows.Forms.Button button1;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; public Form2(System.Windows.Forms.Form frm)
    {
    InitializeComponent();
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if(components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.button1 = new System.Windows.Forms.Button();
    this.SuspendLayout();
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(192, 96);
    this.button1.Name = "button1";
    this.button1.TabIndex = 0;
    this.button1.Text = "b";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // Form2
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(292, 273);
    this.Controls.Add(this.button1);
    this.Name = "Form2";
    this.Text = "Form2";
    this.ResumeLayout(false); }
    #endregion private void button1_Click(object sender, System.EventArgs e)
    {
    Form1 ff=new Form1 (this);
    ff.Show();
    this.Hide();
    }
      

  11.   

    主要代码也就这几行:
    窗体一:
    public Form1(System.Windows.Forms.Form frm) 

       InitializeComponent(); 

    public Form1() 

       InitializeComponent(); 
    }
    static void Main()  

      Application.Run(new Form1()); 
    } private void button1_Click(object sender, System.EventArgs e) 

       Form2 ff=new Form2 (this); 
       ff.Show(); 
       this.Hide(); 
    }窗体二:
    public Form2(System.Windows.Forms.Form frm) 

        InitializeComponent(); 
    }
    private void button1_Click(object sender, System.EventArgs e) 

        Form1 ff=new Form1 (this); 
        ff.Show(); 
        this.Hide(); 
    }
      

  12.   

    我是初学者..不是很懂..能稍微解释一下代码么.
    public Form1(System.Windows.Forms.Form frm)  
    {  
       InitializeComponent();  
    }  
      

  13.   

        public partial class Form1 : Form
        {
            Form2 form = new Form2();
            public Form1()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                form.Owner = this;
                this.Hide();
                form.Show();
            }
        }
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                this.Hide();
                Owner.Show();
            }
        }
      

  14.   

    引用 15 楼 FrostX 的回复:
    我是初学者..不是很懂..能稍微解释一下代码么. 
    public Form1(System.Windows.Forms.Form frm) 

    InitializeComponent(); 
    } 把窗体作为参数传递,就是重载了form1的一个构造方法 
      

  15.   

    Form2 frm = new Form2();
                frm.Show();
                this.Hide();
    将上面的代码贴到第一个窗体的按钮中单击事件中Form1 frm = new Form1();
                frm.Show();
                this.Hide();
    将上面代码贴到第二个窗体的按钮中单击事件中按照楼主的题意,大致可以将上面的代码复制进程序中。因为Form1是Application的启动窗体,所以,如果该窗体被关闭了,那么Main方法中的Application.Run()方法将会执行完毕。所以,如果要在关闭(这里直隐藏)第一个窗体时使用Hide方法。而可以在第二个窗体中使用Close方法。如果你使用上面的方法就会遇到程序无法关闭的问题。那是因为,你的第一个窗体是Application.Run方法的启动窗体,如果这个窗体没有被关闭的话,那么整个程序将无法结束。而从上面的程序来看,Form1被隐藏后,就没有任何方式可以调用到该窗体的Close方法。所以,要解决两个窗体之间的数据传递,最好是在被调用窗体的构造方法中,传递调用窗体的对象引用。具体方法:
    public partial class Form2 : Form
        {
            //定义窗体成员字段,用于存放调用窗体的引用
            private Form1 frm = null;        public Form2()
            {
                InitializeComponent();
            }        //传递窗体的引用的构造方法
            public Form2(Form1 frm)
                : this()    //调用当前窗体的无参构造方法
            {
                //将当前成员字段初始化
                this.frm = frm;
            }        private void button1_Click(object sender, EventArgs e)
            {
                frm.Show();
                this.Close();
            }
        }
    这是最简单的窗体之间的数据传递方式,也是最为方便的。这里还有一个特例,就是X掉被调用的窗体(点击窗体右上角的X按钮),就无法在显示第一个窗体了。不过解决也方便,就是在第二个窗体的FrmClosing时间中把button1_click方法中的代码复制过去就可以了
      

  16.   

    Form1的Button_Click的代码:            this.Hide();
                if (Application.OpenForms["fm2"] == null)
                {
                    Form2 fm2 = new Form2();
                    fm2.Show();
                }
                else
                {
                    Application.OpenForms["fm2"].Show();
                }
    Form2的Button_Click代码:            this.Hide();
                Application.OpenForms["Form1"].Show();测试过的,保证好用:)
      

  17.   

    看清楚我5楼的代码啊...
    Form1.Instance是全局可调用的.应用程序中任何一个地方都可以将其它Show/Hide!