WinForm Closes when startup Form closes RRS feed

  • Question

  • I think this is super trival question. I know how to fix this in VB but c# not giving me the option to close on last form closed. currently I open a new form but then I close the previous. It wants to exit the application. I don't want to hide the previous. I want to close it. 


    var snd = (Button)sender;
                _btnName = snd.Name.ToString();
                _btnClick.Click(_Vision_Form._Current_Form, _btnName);

    My click function will open the nest form. The close will close my current form. When I close the startup form aka current form it exists the app. There is a setting I need to set to stop it from exiting when startup form is closed. I dont remember in c# where that setting lives. 

    • Edited by old_School Friday, February 14, 2020 5:09 PM
    • Moved by CoolDadTx Monday, February 17, 2020 5:28 PM Winforms related
    Friday, February 14, 2020 5:01 PM

All replies

  • One way is to Closing event of the start up form.

    So if Form1 is the startup form e.g from Program.cs

    static class Program
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
            Application.Run(new Form1());

    In Form1 subscribe to FormClosing event,

    public Form1()
        FormClosing += Form1_FormClosing;

    Then in the event

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        var window = MessageBox.Show(
            "Close the window?","Are you sure?",MessageBoxButtons.YesNo);
        e.Cancel = (window == DialogResult.No);

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    profile for Karen Payne on Stack Exchange

    Friday, February 14, 2020 5:46 PM
  • If I understand what you are saying then there are a couple of possibilities.

    It is not clear what the requirements are. Do you want to always show a startup form then close that then show a main form or do you sometimes want to do any of that but not always?

    Oh, wait; you posted this in the C# forum. We do not know if this is WPF or Windows Forms. Can you please tell us which? Assuming this is Windows Forms, please post Windows Forms questions in the Windows Forms forum.

    Please clarify.

    Sam Hobbs

    Saturday, February 15, 2020 8:14 PM
  • Hi old_School,

    Thank you for posting here.

    According to your description, your startup form is the master form.

    In this case, trying to achieve what you want may be troublesome, so I have an idea, why don't we create a new form as the startup form, and then initialize it before initialize the master form, just like this:

           public Form1()
                new Form2().ShowDialog();


    Best Regads,


    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

    Monday, February 17, 2020 3:49 AM
  • In your case I'll just pass nothing to Application.Run(), and let the application exit when there is no longer a thread to run. (i.e.: All forms closed and all threads exited) or when Application.Exit() is called.
    • Edited by cheong00 Monday, February 17, 2020 5:43 AM
    Monday, February 17, 2020 5:41 AM
  • That's the way it works by default. When the first form of your app closes the entire app terminates. There are workarounds. Karen's solution works well if you have a "main" window but it isn't the first window (aka a splash screen). Alternatively you can look at the Application.OpenForms collection and when it has no more top level windows then terminate the app by calling Application.Exit.

    Alternatively though, as you already know VB supports this, you can just use the VB library instead. Nothing prevents the VB code from working in C#, this is .NET after all. If you look at how VB implements this logic it has a type called WindowsFormsApplicationBase that ultimately implements the core logic that is exposed through the UI including multi-instance support. So to get the behavior you want derive your `Program` class from this type. Set the ShutDownStyle to `AfterAllFormsClose` and you get the behavior you had in VB.

    Michael Taylor

    Monday, February 17, 2020 5:27 PM
  • If you look at how VB implements this logic it has a type called WindowsFormsApplicationBase that ultimately implements the core logic that is exposed through the UI including multi-instance support.

    There is a C# equivalent but I forget what it is. I could look for some sample code I wrote but I was waiting for clarification of what the requirements are. Everyone is guessing at what is needed but I prefer for the person asking for help to be specific. What typically happens is that the person asking for help accepts one of the guesses but it might not be the best solution.

    Sam Hobbs

    Monday, February 17, 2020 7:53 PM