Home > Enterprise >  c# How to swap between forms
c# How to swap between forms

Time:11-06

My application has a need to hide an initial form and open any one of four new forms. I can hide the initial and open the selected form with:

private void btn_Option1_Click(object sender, EventArgs e){
    Visible = false;

    Application x = new Application();
    x.show();
}

My question is how to close the second form and reopen the original form? Or I suppose it would be plausible to close each form on each form opening, but that seems wasteful.

CodePudding user response:

It sounds like you need the FormClosed event.

When you create instances of the new Form from your initial Form you can subscribe to the new Form's FormClosed event and show your initial Form from the handler.

This way, whenever one of your new Forms close, the event handler will fire and your initial Form will become visible again.

// This is a method that you would add to your initial Form.
private void SubForm_Closed(object sender, FormClosedEventArgs e)
{
    Visible = true;
}

private void btn_Option1_Click(object sender, EventArgs e)
{
    Visible = false;

    Application x = new Application();
    x.FormClosed  = SubForm_Closed;
    x.show();
}

CodePudding user response:

If you use Hide(), the form "essentially" disappears, you won't even see it in the taskbar. Then you can open by Showing it from one of the subsequent forms. I threw the below together and only tested it quickly so YMMV, you'll need to clean it up and make it work for you, but it should illustrate the point. Create a Windows Form Application. Call it "WindowsFormsApp2" since that's what I used. Paste in the below code:

    using System;
    using System.Windows.Forms;

    namespace WindowsFormsApp2
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            Form2 form2;
            Form3 form3;
            Form4 form4;
            Form5 form5;
            private void button1_Click_1(object sender, EventArgs e)
            {
                if (checkBox1.Checked)
                {
                    form2 = new Form2(this);
                    form2.Show();
                }
                if (checkBox2.Checked)
                {
                    form3 = new Form3(this);
                    form3.Show();
                }
                if (checkBox3.Checked)
                {
                    form4 = new Form4(this);
                    form4.Show();
                }
                if (checkBox4.Checked)
                {
                    form5 = new Form5(this);
                    form5.Show();
                }
                this.Hide();
            }
        }
    }

Paste the below code into the Form1 designer:

    namespace WindowsFormsApp2
    {
        partial class Form1
        {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;

            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }

            #region Windows Form Designer generated code

            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                this.groupBox1 = new System.Windows.Forms.GroupBox();
                this.checkBox1 = new System.Windows.Forms.CheckBox();
                this.checkBox2 = new System.Windows.Forms.CheckBox();
                this.checkBox3 = new System.Windows.Forms.CheckBox();
                this.checkBox4 = new System.Windows.Forms.CheckBox();
                this.button1 = new System.Windows.Forms.Button();
                this.groupBox1.SuspendLayout();
                this.SuspendLayout();
                // 
                // groupBox1
                // 
                this.groupBox1.Controls.Add(this.checkBox4);
                this.groupBox1.Controls.Add(this.checkBox3);
                this.groupBox1.Controls.Add(this.checkBox2);
                this.groupBox1.Controls.Add(this.checkBox1);
                this.groupBox1.Location = new System.Drawing.Point(12, 12);
                this.groupBox1.Name = "groupBox1";
                this.groupBox1.Size = new System.Drawing.Size(274, 73);
                this.groupBox1.TabIndex = 0;
                this.groupBox1.TabStop = false;
                this.groupBox1.Text = "Forms";
                // 
                // checkBox1
                // 
                this.checkBox1.AutoSize = true;
                this.checkBox1.Location = new System.Drawing.Point(17, 32);
                this.checkBox1.Name = "checkBox1";
                this.checkBox1.Size = new System.Drawing.Size(55, 17);
                this.checkBox1.TabIndex = 0;
                this.checkBox1.Text = "Form2";
                this.checkBox1.UseVisualStyleBackColor = true;
                // 
                // checkBox2
                // 
                this.checkBox2.AutoSize = true;
                this.checkBox2.Location = new System.Drawing.Point(78, 32);
                this.checkBox2.Name = "checkBox2";
                this.checkBox2.Size = new System.Drawing.Size(55, 17);
                this.checkBox2.TabIndex = 1;
                this.checkBox2.Text = "Form3";
                this.checkBox2.UseVisualStyleBackColor = true;
                // 
                // checkBox3
                // 
                this.checkBox3.AutoSize = true;
                this.checkBox3.Location = new System.Drawing.Point(139, 32);
                this.checkBox3.Name = "checkBox3";
                this.checkBox3.Size = new System.Drawing.Size(55, 17);
                this.checkBox3.TabIndex = 2;
                this.checkBox3.Text = "Form4";
                this.checkBox3.UseVisualStyleBackColor = true;
                // 
                // checkBox4
                // 
                this.checkBox4.AutoSize = true;
                this.checkBox4.Location = new System.Drawing.Point(200, 32);
                this.checkBox4.Name = "checkBox4";
                this.checkBox4.Size = new System.Drawing.Size(55, 17);
                this.checkBox4.TabIndex = 3;
                this.checkBox4.Text = "Form5";
                this.checkBox4.UseVisualStyleBackColor = true;
                // 
                // button1
                // 
                this.button1.Location = new System.Drawing.Point(12, 91);
                this.button1.Name = "button1";
                this.button1.Size = new System.Drawing.Size(199, 23);
                this.button1.TabIndex = 1;
                this.button1.Text = "Open Selected Forms";
                this.button1.UseVisualStyleBackColor = true;
                this.button1.Click  = new System.EventHandler(this.button1_Click_1);
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(800, 450);
                this.Controls.Add(this.button1);
                this.Controls.Add(this.groupBox1);
                this.Name = "Form1";
                this.Text = "Form1";
                this.groupBox1.ResumeLayout(false);
                this.groupBox1.PerformLayout();
                this.ResumeLayout(false);

            }

            #endregion

            private System.Windows.Forms.GroupBox groupBox1;
            private System.Windows.Forms.CheckBox checkBox4;
            private System.Windows.Forms.CheckBox checkBox3;
            private System.Windows.Forms.CheckBox checkBox2;
            private System.Windows.Forms.CheckBox checkBox1;
            private System.Windows.Forms.Button button1;
        }
    }

Add 4 Windows Forms: Form2, Form3, Form4 and Form5.

Paste in the below code to each and just change the form name on each to it's correct name (Form3, Form4, Form5):

    using System;
    using System.Windows.Forms;

    namespace WindowsFormsApp2
    {
        public partial class Form2 : Form
        {
            public Form2(Form1 Form1In)
            {
                InitializeComponent();
                form1 = Form1In;
            }
            Form1 form1;
            private void button1_Click(object sender, EventArgs e)
            {
                form1.Show();
            }
        }
    }

Finally paste in the below code to the designer of each form and change the name for the forms other than Form2 to their respective name:

    namespace WindowsFormsApp2
    {
        partial class Form2
        {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;

            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }

            #region Windows Form Designer generated code

            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                this.button1 = new System.Windows.Forms.Button();
                this.SuspendLayout();
                // 
                // button1
                // 
                this.button1.Location = new System.Drawing.Point(22, 21);
                this.button1.Name = "button1";
                this.button1.Size = new System.Drawing.Size(104, 23);
                this.button1.TabIndex = 0;
                this.button1.Text = "Show Form1";
                this.button1.UseVisualStyleBackColor = true;
                this.button1.Click  = new System.EventHandler(this.button1_Click);
                // 
                // Form2
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(800, 450);
                this.Controls.Add(this.button1);
                this.Name = "Form2";
                this.Text = "Form2";
                this.ResumeLayout(false);

            }

            #endregion

            private System.Windows.Forms.Button button1;
        }
    }

Then run it.

  • Related