Donald C

Donald C

  • 1.5k
  • 153
  • 3.2k

Close application when frmLogin Cancel button is pressed

Oct 4 2016 3:03 AM
Hi Guys,
 
I'm having trouble with my application where despite calling Application.Exit(); it doesn't close 
I've set up my frmLogin as a dialog screen with the cancel button set as a Dialog.CANCEL button. 
In the frmMain constructor, I call a Security class, which in turn calls ShowLoginScreen. 
 
  1. if (ShowLoginScreen(ProfileList, StartupClient.CompanyName, LastSessProf.UserName) == true)  
  2.   Application.Exit();  
  3.                 
 
In the code snippet I've posted you can see the ShowLoginScreen being called.
In the ShowLoginScreen method, the frmLogin Screen is instantiated in a do while loop.
The Do while loop is there in case the wrong username or password is entered. (Is there a better way to do this?)
 
  1. public bool ShowLoginScreen(List<string> ProfileList, string CompanyName, string UserName)  
  2.     {  
  3.       frmLogin frm = new frmLogin(ProfileList, CompanyName, UserName);  
  4.   
  5.       bool bCloseApplication = false;  
  6.       bool bLoginResult = false;  
  7.   
  8.       do  
  9.       {  
  10.         var result = frm.ShowDialog();  
  11.   
  12.         if (result == DialogResult.OK)  
  13.         {  
  14.           if (ValidateLogin(frm.Profile, frm.UserName, frm.Password) == true)  
  15.           {  
  16.             bLoginResult = true;  
  17.             frm.Close();  
  18.           }  
  19.           else  
  20.           {  
  21.             bLoginResult = false;  
  22.             MessageBox.Show("Incorrect Username or Password");  
  23.           }  
  24.   
  25.         }  
  26.   
  27.         if (result == DialogResult.Cancel)  
  28.         {  
  29.           bLoginResult = true;  
  30.           frm.Close();  
  31.   
  32.           bCloseApplication = true;  
  33.           return bCloseApplication;  
  34.   
  35.         }  
  36.       }  
  37.       while (bLoginResult == false);  
  38.   
  39.       return bCloseApplication;  
  40.     }  
 
If the user clicks Cancel, the DialogResult.Cancel response is generated and is dealt with by the if statement. I've tried Application.Exit() from there, but that didn't work.
The if statement checking for the cancel result works fine, and we leave the do... while loop, back to where the ShowLoginScreen was called. (1st Code snippet)
 
While debugging, the Application.Exit() code is stepped through, the method finishes and we head back to Program.cs where  Application.Run(new frmMain()); is called.
 
 
 How do I close the application when the frmLogin Cancel button has been pressed?

Any help would be appreciated, 
 
Thanks. 
 
 
 

Attachment: Class1.zip

Answers (7)