Search code examples
c#exitmultiple-forms

Visual Studio C# - X Button only closes one form rather than the whole program


So my issue is pretty straight-forward, I have a program with a login feature, and if all goes well it will open up another form and hide itself.

My issue is that in the new form that opens, hitting the X button to close the window will only close the form but leave the other one running, along with the process.

I have a quit button that shuts down the program, but is there any way to make the X button shut down the program entirely?

Thanks!


Solution

  • You need to handle the Closed or 'FormClosed' event. This event is raised when form is closed.

    use either of these when creating childform.

    childForm.Closed += (s, ev) => Application.Exit();
    childForm.FormClosed += (s, ev) => Application.Exit();
    

    Update :

    Sorry, I might have over looked the last part in question, since you have X button which has the Click event, you can simply call Application.Exit(); on that event.