Closing an Application in the Main Form Constructor

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
 
A

Alex Clark

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
 
T

Tom

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top