Startup problem with Main()

C

CSharpNewBie

Hi

I am creating a Winform application and I need help. Let me explain my
problem
This is what i do on startup, I create a form (a Login screen) at runtime
and that will ask user to enter a user/password on entering the password the
form will check the local file to authenticate, (encrypted file) then it
will return true in (loadLoginForm) else false if the used clicked cancel
based on the that input I have to load the main form. everything works but
the problem is even though it ignores the Initialcomponent on cancel but the
Main() gets executed. How can I close the application if the user clicks
cancel exit out without going in Main()
public FrmMain()

{

if (LoadLoginForm())

{InitializeComponent();}

else

{this.Close();} // this means user clicked cancel close the application.

}

[STAThread]

static void Main()

{

Application.Run(new FrmMain()); // I dont want this to execute if user
clicks cancel on the login screen

}

Thanks

Arun
 
G

Guest

By calling Application.Run(new FrmMain()) you are causing your Main form to
be displayed... a simple option would be to pull your Login Form logic back a
bit and try something like:

static void Main()
{
if (LoadLoginForm())
{
Application.Run(new FrmMain());
}
}

You would probably need to change the visibility LoadLoginForm or some
namespaces and/or add some using declarations in order to make
LoadLoginForm() accessible from Main().

Brendan
 
B

Bruce Wood

You seem to have things in the wrong place... perhaps due to a
misunderstanding of control flow in your application. I would change
the application as follows, which might make things clearer:

public FrmMain()
{
InitializeComponent();
...
}

[STAThread]
static void Main()
{
if (LoadLoginForm())
{
Application.Run(new FrmMain());
}
}

This gets rid of the ugly this.Close() in the constructor for FrmMain,
which is never going to do what you want: you can't "not construct"
something and return a result other than a constructed object to the
caller. The only way to not construct something in a constructor is to
throw an exception, which would take your application down.

This way, your Main method (which always has to run, no matter what)
calls the login screen directly, and doesn't start the application if
the user doesn't log in. Another possibility might look like this:

public FrmMain()
{
InitializeComponent();
...
}

public override void OnLoad(EventArgs e)
{
if (!LoadLoginForm())
{
this.Close();
return;
}
}

[STAThread]
static void Main()
{
Application.Run(new FrmMain());
}

This moves the login dialog pop-up and the this.Close() into the
OnLoad() method, which is a more appropriate place for that sort of
activity, leaving the constructor to simply build the form, which is
all it's supposed to do.
 
R

Richard Blewett [DevelopMentor]

CSharpNewBie said:
Hi

I am creating a Winform application and I need help. Let me explain my
problem
This is what i do on startup, I create a form (a Login screen) at runtime
and that will ask user to enter a user/password on entering the password
the form will check the local file to authenticate, (encrypted file) then
it will return true in (loadLoginForm) else false if the used clicked
cancel
based on the that input I have to load the main form. everything works but
the problem is even though it ignores the Initialcomponent on cancel but
the Main() gets executed. How can I close the application if the user
clicks cancel exit out without going in Main()
public FrmMain()

{

if (LoadLoginForm())

{InitializeComponent();}

else

{this.Close();} // this means user clicked cancel close the application.

}

[STAThread]

static void Main()

{

Application.Run(new FrmMain()); // I dont want this to execute if user
clicks cancel on the login screen

}

Thanks

Arun

Why not load the login form in Application.Run and get that to spawn the
main app if they login correctly?

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk
 
C

CSharpNewBie

This doesn't work because the LoadLoginForm() method is part of FrmMain and
when I do that I get a Compiler error
C:\caUtil\FrmMain.cs(238): An object reference is required for the nonstatic
field, method, or property 'caUtil.FrmMain.LoadLoginForm()'





Brendan Grant said:
By calling Application.Run(new FrmMain()) you are causing your Main form
to
be displayed... a simple option would be to pull your Login Form logic
back a
bit and try something like:

static void Main()
{
if (LoadLoginForm())
{
Application.Run(new FrmMain());
}
}

You would probably need to change the visibility LoadLoginForm or some
namespaces and/or add some using declarations in order to make
LoadLoginForm() accessible from Main().

Brendan


CSharpNewBie said:
Hi

I am creating a Winform application and I need help. Let me explain my
problem
This is what i do on startup, I create a form (a Login screen) at runtime
and that will ask user to enter a user/password on entering the password
the
form will check the local file to authenticate, (encrypted file) then it
will return true in (loadLoginForm) else false if the used clicked cancel
based on the that input I have to load the main form. everything works
but
the problem is even though it ignores the Initialcomponent on cancel but
the
Main() gets executed. How can I close the application if the user clicks
cancel exit out without going in Main()
public FrmMain()

{

if (LoadLoginForm())

{InitializeComponent();}

else

{this.Close();} // this means user clicked cancel close the application.

}

[STAThread]

static void Main()

{

Application.Run(new FrmMain()); // I dont want this to execute if user
clicks cancel on the login screen

}

Thanks

Arun
 
C

CSharpNewBie

Can you please explain me how?
LoadLoginForm is a Method in that I create the Login form manually
Here's the code
private bool LoadLoginForm()

{


Form frmLogin = new Form();

frmLogin.Width = 400;

frmLogin.Height = 200;

Button btnShutdown = new System.Windows.Forms.Button();

Button btnLogin = new System.Windows.Forms.Button();

TextBox txtPassword = new System.Windows.Forms.TextBox();

TextBox txtUserName = new System.Windows.Forms.TextBox();

if (frmLogin.ShowDialog() == DialogResult.OK)

{

bShutdown = false;

return true;

}

else

{

bShutdown = true;

return false;

}

}

There are other code i only pasted few lines.

The return value will determine what to do next and bShutdown is a private
variable.
 
C

CSharpNewBie

I even tried this

public FrmMain()

{

if (LoadLoginForm())

{

InitializeComponent();

}

else

{

Application.Exit();

}

}

Still I see a form.
 
C

Chris Dunaway

That's because Application.Run(new FrmMain()) is what is causing your
form to load which then calls the LoadLoginForm. By that time, you
cannot stop the form from loading. The correct approach is the one
Brendan suggested. You will have to make LoadLoginForm static in order
to avoid the error you were getting.

static void Main()
{
if (LoadLoginForm())
{
Application.Run(new FrmMain());
}

}
 
C

CSharpNewBie

This is my current structure of the application:

namespace mynamespace
public class FrmMain
{
private LoadLoginForm()
{
}
}
[STAThread]

static void Main()
{
Application.Run(new FrmMain());
}

Can you help me where I can add/change the code in order to move the
LoadLoginForm method in the Main

Thanks
Arun


Brendan Grant said:
By calling Application.Run(new FrmMain()) you are causing your Main form
to
be displayed... a simple option would be to pull your Login Form logic
back a
bit and try something like:

static void Main()
{
if (LoadLoginForm())
{
Application.Run(new FrmMain());
}
}

You would probably need to change the visibility LoadLoginForm or some
namespaces and/or add some using declarations in order to make
LoadLoginForm() accessible from Main().

Brendan


CSharpNewBie said:
Hi

I am creating a Winform application and I need help. Let me explain my
problem
This is what i do on startup, I create a form (a Login screen) at runtime
and that will ask user to enter a user/password on entering the password
the
form will check the local file to authenticate, (encrypted file) then it
will return true in (loadLoginForm) else false if the used clicked cancel
based on the that input I have to load the main form. everything works
but
the problem is even though it ignores the Initialcomponent on cancel but
the
Main() gets executed. How can I close the application if the user clicks
cancel exit out without going in Main()
public FrmMain()

{

if (LoadLoginForm())

{InitializeComponent();}

else

{this.Close();} // this means user clicked cancel close the application.

}

[STAThread]

static void Main()

{

Application.Run(new FrmMain()); // I dont want this to execute if user
clicks cancel on the login screen

}

Thanks

Arun
 

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