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.
- if (ShowLoginScreen(ProfileList, StartupClient.CompanyName, LastSessProf.UserName) == true)
- Application.Exit();
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?)
The Do while loop is there in case the wrong username or password is entered. (Is there a better way to do this?)
- public bool ShowLoginScreen(List<string> ProfileList, string CompanyName, string UserName)
- {
- frmLogin frm = new frmLogin(ProfileList, CompanyName, UserName);
- bool bCloseApplication = false;
- bool bLoginResult = false;
- do
- {
- var result = frm.ShowDialog();
- if (result == DialogResult.OK)
- {
- if (ValidateLogin(frm.Profile, frm.UserName, frm.Password) == true)
- {
- bLoginResult = true;
- frm.Close();
- }
- else
- {
- bLoginResult = false;
- MessageBox.Show("Incorrect Username or Password");
- }
- }
- if (result == DialogResult.Cancel)
- {
- bLoginResult = true;
- frm.Close();
- bCloseApplication = true;
- return bCloseApplication;
- }
- }
- while (bLoginResult == false);
- return bCloseApplication;
- }
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,
Any help would be appreciated,
Thanks.

ketan borsdiyaRank #2,258 · 624 reputationPosted Oct 4, 2016, 2:48 AM EDT
ketan borsdiyaRank #2,258 · 624 reputationPosted Oct 4, 2016, 12:33 AM EDT
Donald C152 reputationPosted Oct 4, 2016, 1:38 AM EDT
I don't have the option for Application.Current....
Delpin Susai RajRank #53 · 29.5K reputationPosted Oct 4, 2016, 5:58 AM EDT
frmLogin loginForm = new frmLogin(); //Set the dialog result on login form depending on ok and cancel button //close the application if user wants to cancel if(loginForm.DialogResult == DialogResult.Cancel) this.Close();Delpin Susai RajRank #53 · 29.5K reputationPosted Oct 4, 2016, 5:59 AM EDT
private void btnLogin_Click(object sender, EventArgs e) { // some codes here frmMainForm.ShowDialog(); frmLogin.Hide(); }Delpin Susai RajRank #53 · 29.5K reputationPosted Oct 4, 2016, 6:00 AM EDT
Donald C152 reputationPosted Oct 5, 2016, 11:21 AM EDT