visual studio - Correctly open and close multiple windows forms in C# -


I have a Windows application that is using multiple forms. The program first runs the form with a login screen which allows the user to select a specific profile with their settings, and a button that opens another form to create a new profile.

The code I am using looks like this

  form 2 secondform = new form 2 (); Private Zero aButton_Click (Object Sender, EventArgs e) {this.Hide (); Secondform.Show (); }  

When I try to use

  then close it ();  

instead of

  this.Hide ();  

Exits the entire application, even if I put it after showing a new look.

The problem is that once you enter the main program, the only way to close the program is to click on the exit button completely, which I have placed there, Which uses

  application.Exit ();  

If you try to right-click on the Taskbar icon and switch off from there, or use the red exit button, the process still runs, and it's from the Task Manager Is eliminated.

What can I do to close the app correctly?

You should modify the main entry of your WinForm application's status, that is, "program.cs" The stable main method displays login mode as a model dialog and then call the application. () The method here is a simple example with an example of your main form:

  Fixed class programs {/// & lt; Summary & gt; Main entry point for the application /// & lt; / Summary & gt; [STAThread] Fixed Zero Main () {Application.EnableVisualStyles (); Application.SetCompatibleTextRenderingDefault (wrong); // login form login form form login = new LoginForm (); LoginForm.ShowDialog (); // Show main form application. Rune (new MainForm ()); }} Category LoginForm: Form {Public LoginForm () {BackColor = Color.Blue; Text = "entry form"; }} Class MainForm: form {public MainForm () {BackColor = Color.Red; Text = "main form"; }}  

The above code will first show a dummy login form (in blue) and when closed, will be shown in the main form (red). The code is very straightforward, there is no need to hide and especially close and similar.


Comments