Winform Forms Closing and Opening a New Form

1. frmHome frm = new frmHome(); frm.Show(); this.Close(); 

I open HomeForm from LoginForm . In the LoginForm form_closed event, I call Application.Exit() . This allows you to open LoginForm and exit the application by pressing the X button.

The problem is that I switch from LoginForm to HomeForm and call this.Close() , the form_closed event of the form_closed triggers and the application closes.

I am allowed to show only one form at a time.

+4
source share
8 answers

you can use a boolean (global variable) as an exit flag in LoginForm

initialize it:

 exit = true;//in constructor 

set to false before closing:

 frmHome frm = new frmHome(); frm.Show(); exit = false; this.Close(); 

and in form_closed :

 if(exit) Application.Exit(); 

if the user closes the form using the 'X' button, exit will be true and Application.Exit() will be called.

EDIT:

the above does not work because LoginForm is your main form used by Application.Run(loginForm) .

2 offers:

With exit flag:

replace

 Application.Run(new LoginForm()) 

by

 LoginForm loginFrm = new LoginForm(); loginFrm.Show(); Application.Run(); 

Without exit flag:

replace in current code:

 frmHome frm = new frmHome(); frm.Show(); this.Close(); 

by

 frmHome frm = new frmHome(); this.Visible = false; frm.ShowDialog(); this.Close(); 
+12
source

In program.cs:

 void Main() { frmLogin fl = new frmLogin(); if (fl.ShowModal() == DialogResult.Ok) { frmHome fh = new frmHome(); fh.Show(); } } 
+5
source

you can make the form hide, not close. instead of catching the form_closed event, catch the form_closing event. it will look something like this.

 private void LoginFrm_Closing(object sender, FormClosingEventArgs e) { e.Cancel = true; this.Hide(); frmHome frm = new frmHome(); frm.Show(); } 

it will remain open, but only one visible. somewhere in frmHome there might be a public variable for storing LoginFrm, so you can switch between them using Hide (); and Show (); (and any other forms you can add)

Edit: grammar.

+1
source

All you have to do is close the two forms without problems. Form1 fin = new Form1 (); fin.Close (); this.Visible = false; Form2 win = new Form2 (); win.Visible = true;

+1
source

One way to achieve this:

  • Open the main form at startup.
  • Hide it. (optional, but not for you, if you really cannot show more than one form.)
  • Open the login form with ShowDialog ();
  • If the login is successful, show your main form. If not, then close your main form / application.
0
source

Why don't you just call application.exit on the form_closed event?

I'm not sure if you really need it, and if you do, you can remove the x icon from the screen and give them a close button.

In addition, there is a CloseReason in the event arguments, which will tell you if the user is closing a form or code or something else.

0
source

I'm not quite sure that I understand this, maybe you could do something like this if you want to iterate over a specific order of forms: (pseudocode)

 Dim formsList as New List(Of form) formsList.add(Form1) formsList.add(Form2) formsList.add(Form3) ' etc etc etc ' For Each f as Form in formsList f.ShowDialog() ' you could have a condition here which breaks the for loop perhaps ' Next Me.close ' close the app ' 

You can add a condition to the For loop, which interrupts the For loop to end the earlier ...

note: sorry for the vb.net code ... but this should be easy to understand though

0
source

try to help you

 frmHome frm = new frmHome(); this.hide() frm.ShowDialog(); this.Close(); 
0
source

All Articles