Closing an Application in the Main Form Constructor

  • Thread starter Thread starter Peter D. Dunlap
  • Start date Start date
P

Peter D. Dunlap

I have an application in which the constructor for the main form pops
up a user name/password dialog and validates the user. If the user
hits the Cancel button in this dialog, I want to close the application
without ever showing the main form.


// this code is in the main form constructor
if (formLogin.ShowDialog() == DialogResult.Cancel)
{
// close the app here
}
// more code to initialize the app is here


For the code I've tried:

Application.Exit();
return;

and

Close();
return;

Neither one works, it still shows the main form and does not exit.

Any suggestions?

Pete Dunlap
 
Hi,
Make the app's entry point a "Main" routine in a static class (it would be a
"Module" in VB.NET terms). Then throw an exception in the form's
constructor if the password is invalid, and trap this in the "Main" routine.
This should prevent the form from loading.


Cheers,
Alex Clark
 
I'd do somtehing like:

public class StartShell
{
....

[STAThread]
static void Main(string[] commandLine)
{
StartShell oStartShell = new StartShell(commandLine);
}
public StartShell(string[] commandLine)
{
FLogin frmLogin = new FLogin();
if( frmLogin.ShowDialog() == DialogResult.OK )
{
FMainApp frmMainApp = new FMainApp();
Application.Run(frmMainApp);
}
}

Tom Clement
Apptero, Inc.
 
Back
Top