none
How to switch between C# WinForm? RRS feed

  • Question

  • New to C#, trying to describe my question as clearly as I can.

    Form1 is shown automatically when the project starts, I have a line of code like this inside Program.cs

    Application.Run(new Form1 ());

    I have another form Form2, which is NOT inside Program.cs, that being said, Program.cs does not have any code related to Form2

    Inside Form1, there is a button SwitchFormButton, which has below code:

    Form2 f2 = new Form2();
    f2.Show();
    
    //this.Close();
    //If I use this.Close() here, it actually closes both form, end application, which is not what I want.
    
    //this.Visible = false;
    //The code does not work either, the code merely moves Form1 behind Form2, but both forms still show up in window taskbar.

    Here is what I want to accomplish: when I click the Form1 button SwitchFormButton, Form1 should be closed (not showing up in task bar either), Form2 shows up.

    How should I do it?

    Thanks.



    • Edited by VA_er Friday, March 1, 2019 4:29 AM
    • Moved by CoolDadTx Friday, March 1, 2019 2:57 PM Winforms related
    Friday, March 1, 2019 4:27 AM

All replies

  • Hi VAer8,

    Thank you for posting here.

    According to your description, you want to achieve this function that Form1 should be closed and Form2 shows up when you click the Form1 button SwitchFormButton.

    You could try the following code.

    private void SwitchFormButton_Click(object sender, EventArgs e)
            {
                child c = new child();
                this.Hide();
                c.ShowDialog();
                this.Close();
            }
    

    Result:

    Hope my solution could be helpful.

    Best regards,

    Jack



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 1, 2019 5:48 AM
  • Jack's reply is helpful (you should use .Hide() rather than set the .Visible property), but it will cause problems once the user Closes Form2 because Form1 is still running, but now you have no way to Close it! The solution is easy though:

    Add a new field and an additional constructor to Form2, like this:

    private Form CallingForm = null;
    public Form2(Form callingForm) : this()
    {
        this.CallingForm = callingForm;
    }
    // you still need the original constructor, so don't get rid of it:
    public Form2()
    {
        InitializeComponent();
    }

    Then add a FormClosing handler to Form2 (if you don't already have one) and put in this code to be able to Show Form1 again after Form2 Closes:

    private void Form2_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (this.CallingForm != null)
            this.CallingForm.Show();
    }

    And, lastly, in Form1's SwitchFormButton handler, instantiate Form2 slightly differently:

    Form2 f2 = new Form2(this);
    f2.Show();
    


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Saturday, March 2, 2019 6:02 PM
  • Hi

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer , so that it will help other members to find solution quickly if they faces similar issue.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 4, 2019 7:25 AM
  • Hi

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer , so that it will help other members to find solution quickly if they faces similar issue.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thanks, I will take a look at it later tonight.

    I am new to msdn, I was busy and forgot to follow up this thread, the big part of reason is: it does not set notification to my mailbox and notify me there is new reply. For other forums, there is setting to notification to email Inbox, that is typically how I follow up my threads.

    Edit: I just looked into setting, and add my email for alert. I remember when I signup msdn account, there is already an email, not sure why no email is associated with this account. Trying to get familiar with msdn....

    • Edited by VA_er Friday, March 8, 2019 12:55 PM
    Friday, March 8, 2019 12:43 PM
  • Jack's reply is helpful (you should use .Hide() rather than set the .Visible property), but it will cause problems once the user Closes Form2 because Form1 is still running, but now you have no way to Close it! The solution is easy though:

    Add a new field and an additional constructor to Form2, like this:

    private Form CallingForm = null;
    public Form2(Form callingForm) : this()
    {
        this.CallingForm = callingForm;
    }
    // you still need the original constructor, so don't get rid of it:
    public Form2()
    {
        InitializeComponent();
    }

    Then add a FormClosing handler to Form2 (if you don't already have one) and put in this code to be able to Show Form1 again after Form2 Closes:

    private void Form2_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (this.CallingForm != null)
            this.CallingForm.Show();
    }

    And, lastly, in Form1's SwitchFormButton handler, instantiate Form2 slightly differently:

    Form2 f2 = new Form2(this);
    f2.Show();


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Thank you very much for your answer, since I am very very newbie, I don't follow your post.

    I have slightly changed my question: there is a button in each form, I want to use that button to switch form, at the meanwhile, only one window shows up in taskbar (it just appears like running one application).

    Here are my complete code(there are errors, and I don't understand the errors) & screenshots:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace Switch_Form
    {
        static class Program
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
        }
    }

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace Switch_Form
    {
        public partial class Form1 : Form
        {
            private Form CallingForm = null;
            public Form1(Form callingForm) : this()
            {
                this.CallingForm = callingForm;
            }
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                if (this.CallingForm != null)
                    this.CallingForm.Show();
            }
    
            private void btnSwitch_Click(object sender, EventArgs e)
            {
                //child c = new child();
                this.Hide();
                //c.ShowDialog();
                //this.Close();
                Form2 f2 = new Form2(this);
                f2.Show();
            }
        }
    }

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace Switch_Form
    {
        public partial class Form2 : Form
        {
            private Form CallingForm = null;
            public Form2(Form callingForm) : this()
            {
                this.CallingForm = callingForm;
            }
    
            public Form2()
            {
                InitializeComponent();
            }
    
            private void Form2_FormClosing(object sender, FormClosingEventArgs e)
            {
                if (this.CallingForm != null)
                    this.CallingForm.Show();
            }
    
            private void btnSwitch2_Click(object sender, EventArgs e)
            {
                this.Hide();
                Form1 f1 = new Form1(this);
                f1.Show();
            }
        }
    }

    Friday, March 8, 2019 11:45 PM
  • I guess I don't understand your requirements ... you've changed your mind about what you want? Initially, you wanted Form1 to Show Form2 (and then wanted Form1 to "disappear"). When Form2 closed, you wanted Form1 to reappear. The code that I posted will do that. If it didn't work right, then perhaps you didn't add the Form2_FormClosing event handler correctly. You have to do that while in the Form2 Designer, and go to the events in the Properties window and double-click on the FormClosing event, just like you did for the btnSwitch Click event.

    You say you have errors, but don't state what they are.

    However, I think you can salvage what you have (with your new code), like this:

    First, you don't need to have the CallingForm and the additional constructor to set the CallingForm in Form1. That belongs only in Form2. Likewise, you don't need the CallingForm stuff in Form1_FormClosing ... again, that only goes in Form2. Just like I initially showed you.

    The other thing you added, which is what's causing you problems, is the code in the btnSwitch in Form2. It should look like this:

    private void btnSwitch2_Click(object sender, EventArgs e)
    {
        if (this.CallingForm != null)
            this.Close();
    }

    That will then call the Form2_FormClosing() handler, which close Form2 and go back to showing the original Form1. You were creating a new Form1 there and that is what caused you all kinds of problems.

    Will this work OK for you?


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Saturday, March 9, 2019 12:52 AM
  • Let me try to be as clearly as possible now: 

    Application.Run(new Form1());  //This is in Program.cs

    1. When I start the application, Form1 opens, and Form1 icon shows up in Taskbar too.
    2. When I click Form1 button "Switch to Form 2", then Form1 should be disappear, and Form2 opens, and Form2 icon appears on taskbar too.
    3. When I click Form2 button "Switch to Form 1", then Form2 should be disappear, and Form1 opens, and Form1 icon appears on taskbar too.

    Thanks.

    Saturday, March 9, 2019 1:09 AM
  • And that *will* happen if you make the changes I suggested. Clicking the "Switch to Form 2" button will open a new Form2 every time. Clicking the "Switch to Form 1" will close Form2 and show the original Form1. The icon on the Taskbar will be correct.

    Did you try making those changes? Those changes are basically to put it back to what I originally suggested, and with changes to what you put in the btnSwitch2 to make the "Switch to Form 1" work correctly.


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Saturday, March 9, 2019 1:20 AM
  • First, I want to thank you for your replies. But seriously, I am very newbie, I did not follow your post and don't understand the errors. Now this is the current version of codes, and error message is also pasted here now:

    namespace Switch_Form
    {
        public partial class Form1 : Form
        {
            
            public Form1()
            {
                InitializeComponent();
            }
    
    
            private void btnSwitch_Click(object sender, EventArgs e)
            {
                this.Hide();
                Form2 f2 = new Form2(this);
                f2.Show();
            }
        }
    }

    namespace Switch_Form
    {
        public partial class Form2 : Form
        {
            private Form CallingForm = null;
            public Form2(Form callingForm) : this()
            {
                this.CallingForm = callingForm;
            }
    
            public Form2()
            {
                InitializeComponent();
            }
    
            private void Form2_FormClosing(object sender, FormClosingEventArgs e)
            {
                if (this.CallingForm != null)
                    this.CallingForm.Show();
            }
    
            private void btnSwitch2_Click(object sender, EventArgs e)
            {
                this.Hide();
                Form1 f1 = new Form1(this);
                f1.Show();
            }
        }
    }


    Saturday, March 9, 2019 1:39 AM
  • The problem is this:

    private void btnSwitch2_Click(object sender, EventArgs e)
    {
        this.Hide();
        Form1 f1 = new Form1(this);
        f1.Show();
    }

    … which I said that you need to change it to this:

    private void btnSwitch2_Click(object sender, EventArgs e)
    {
        if (this.CallingForm != null)
            this.Close();
    }


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Saturday, March 9, 2019 2:36 AM
  • Thank you very much, it can compile now, but it still does not act like what I want.

    1. When I start the application, Form1 opens, and Form1 icon shows up in Taskbar too.
    2. When I click Form1 button "Switch to Form 2", then Form1 should be disappear, and Form2 opens, and Form2 icon appears on taskbar too.
    3. When I click Form2 button "Switch to Form 1", then Form2 should be disappear, and Form1 opens, and Form1 icon appears on taskbar too.

    With the change, it still does not act step 3, when I click Form2 button "Switch to Form 1", Form2 does disappear, but Form1 does not open, no window is open at the moment.

    Saturday, March 9, 2019 2:58 AM
  • And Form2's Closing looks like this?

    private void Form2_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (this.CallingForm != null)
            this.CallingForm.Show();
    }
    

    If so, that should work … but, since it doesn't could you post the code for Form1 and Form2 again?  (Since you made changes, I just want to be sure they are all in the right places.)


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Saturday, March 9, 2019 5:56 AM
  • Yes, Form2_FormClosing is like that, let me copy the most current code here (below), the code has more new problems, let us explain the new problems after the code.

    Here is the previous problem in the earlier post:

    Code:

    namespace Switch_Form
    {
        public partial class Form2 : Form
        {
            private Form CallingForm = null;
            public Form2(Form callingForm) : this()
            {
                this.CallingForm = callingForm;
            }
    
            public Form2()
            {
                InitializeComponent();
            }
    
            private void Form2_FormClosing(object sender, FormClosingEventArgs e)
            {
                if (this.CallingForm != null)
                    this.CallingForm.Show();
            }
    
            private void btnSwitch2_Click(object sender, EventArgs e)
            {
                if (this.CallingForm != null)
                this.Close();
            }
        }
    }


    namespace Switch_Form
    {
        public partial class Form1 : Form
        {
            
            public Form1()
            {
                InitializeComponent();
            }
    
    
            private void btnSwitch_Click(object sender, EventArgs e)
            {
                this.Hide();
                Form2 f2 = new Form2(this);
                f2.Show();
            }
        }
    }
    

    New problem:As mentioned in the old problem, Form2 button "Swift to Form 1" does not work properly. At this moment, it seems the problem is still running behind the scene, so at this moment, if I try to rebuild solution, it comes up new problem.

    Saturday, March 9, 2019 4:11 PM
  • Yeah, that's the problem … it *is* still running. Sorry, I should have noticed that from one of your previous replies. 

    Go to Task Manager (right click on the Taskbar and click on Task Manager), you should be able to find the running SwitchForm.exe, and then right-click it and End Task. Then try running it again … it should compile fine now.


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com


    • Edited by BonnieBMVP Saturday, March 9, 2019 5:35 PM added more
    Saturday, March 9, 2019 5:33 PM
  • Thanks, but the issue is not about ending task from Task Manager, the issue is the code should not act like that and the code needs to be fixed to avoid this kind of problem.

    Saturday, March 9, 2019 5:36 PM
  • The reason it happened was because of the incorrect code you had initially. With the correct code, that will never happen.

    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Saturday, March 9, 2019 5:51 PM
  • Hello VA_er!

    Try this, this may work:

    this.Hide();
    form Form2 = new Form2();
    m.Show();

    If it doesn't, let me know!


    • Edited by Wackyduck Sunday, November 10, 2019 5:05 AM whoops! i made some mistakes! :O
    Sunday, November 10, 2019 5:04 AM