在.net中可不可以在一个工程中建立多个窗口,然后通过点击其中一个窗口中的按钮,切换到另外的一个窗口中?请大家指教,谢谢!

解决方案 »

  1.   

    this.Hide();
    (new Form2()).Show();
      

  2.   

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows.Forms;namespace Test
    {
        class RunMain:Form
        {
            Form1 fr1;
            Form2 fr2;
            static void Main()
            {
                Application.Run(new RunMain());
            }        RunMain()
            {
                fr1 = new Form1();
                fr2 = new Form2();
                fr1.ChangeForm += new EventHandler(fr1_ChangeForm);
                fr2.ChangeForm += new EventHandler(fr2_ChangeForm);
                fr1.Show();
                this.Visible = false;
            }
            void fr2_ChangeForm(object sender, EventArgs e)
            {
                fr2.Visible = false;
                fr1.Show();        }        void fr1_ChangeForm(object sender, EventArgs e)
            {
                fr1.Visible = false;
                fr2.Show();
            }
        }
        public partial class Form1 : Form
        {
            public event EventHandler ChangeForm;
            public Form1()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                if (ChangeForm != null)
                    this.ChangeForm(sender, e);
            }
        }
        partial class Form1
        {
            private System.ComponentModel.IContainer components = null;
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }        #region Windows 窗体设计器生成的代码
            private void InitializeComponent()
            {
                this.button1 = new System.Windows.Forms.Button();
                this.SuspendLayout();
                // 
                // button1
                // 
                this.button1.Location = new System.Drawing.Point(28, 42);
                this.button1.Name = "button1";
                this.button1.Size = new System.Drawing.Size(75, 23);
                this.button1.TabIndex = 0;
                this.button1.Text = "button1";
                this.button1.UseVisualStyleBackColor = true;
                this.button1.Click += new System.EventHandler(this.button1_Click);
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(295, 155);
                this.Controls.Add(this.button1);
                this.Name = "Form1";
                this.Text = "Form1";
                this.ResumeLayout(false);        }        #endregion        private System.Windows.Forms.Button button1;
        }
        public partial class Form2 : Form
        {
            public event EventHandler ChangeForm;
            public Form2()
            {
                InitializeComponent();
            }        private void button2_Click(object sender, EventArgs e)
            {
                if (this.ChangeForm != null)
                {
                    this.ChangeForm(sender, e);
                }
            }
        }
        partial class Form2
        {
            private System.ComponentModel.IContainer components = null;        /// <summary>
            /// 清理所有正在使用的资源。
            /// </summary>
            /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }        #region Windows 窗体设计器生成的代码        /// <summary>
            /// 设计器支持所需的方法 - 不要
            /// 使用代码编辑器修改此方法的内容。
            /// </summary>
            private void InitializeComponent()
            {
                this.button2 = new System.Windows.Forms.Button();
                this.SuspendLayout();
                // 
                // button2
                // 
                this.button2.Location = new System.Drawing.Point(35, 28);
                this.button2.Name = "button2";
                this.button2.Size = new System.Drawing.Size(75, 23);
                this.button2.TabIndex = 0;
                this.button2.Text = "button2";
                this.button2.UseVisualStyleBackColor = true;
                this.button2.Click += new System.EventHandler(this.button2_Click);
                // 
                // Form2
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(311, 180);
                this.Controls.Add(this.button2);
                this.Name = "Form2";
                this.Text = "Form2";
                this.ResumeLayout(false);        }        #endregion        private System.Windows.Forms.Button button2;
        }
    }