0

I'm newbie and I have the following problem When running the program, the login form will appear for users to log in if successful and the system form will appear

formLogin.Hide();

But when I close the form the system is still running, how can I fix this. I tried on successful login but it will close the whole program

formLogin.Close()
Rand Random
  • 7,300
  • 10
  • 40
  • 88
Khả Nam
  • 33
  • 6
  • do you try `Application.Exit()`? – Lawraoke Jun 23 '20 at 09:49
  • 1
    If `formLogin` is your starting Form, you should change that.You can start your main Form (visible or not), then show your Login Form from your main. If the login proc fails, close your main and the app terminates. Otherwise, go on. If you search SO for *Login Form* or similar, you'll find a lot of similar questions. – Jimi Jun 23 '20 at 09:52
  • How do I catch an event that closes a form? – Khả Nam Jun 23 '20 at 09:57
  • You can show the Login Form as a modal dialog (`.ShowDialog()`): it will return a `DialogResult` value (you can set it yourself to any of the available values). Or, subscribe to the `FormClosed` event: this event is raised when the Form has been closed by any means, but public properties are still readable. – Jimi Jun 23 '20 at 10:51

1 Answers1

0

Somewhere in your program is the code Application.Run. If you pass a form to that method, then the application will exit automatically when that form closes.

If somehow you wish to explicitly stop your application at some point, use Application.Exit to exit the message pump, after which your Application.Run method will return and any cleanup can occur.

Calling Environment.Exit is the nuclear option that kills the process without prejudice. You should consider whether your application needs to clean anything up as it exits. If so, go for graceful exit.

Drew Noakes
  • 300,895
  • 165
  • 679
  • 742