473,387 Members | 1,749 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Startup problem with Main()

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
Nov 17 '05 #1
9 1595
"CSharpNewBie" <CS**********@VS2003.NET> wrote in message
news:uE*************@tk2msftngp13.phx.gbl...
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
Nov 17 '05 #2
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" wrote:
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

Nov 17 '05 #3
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.

Nov 17 '05 #4
I already have a FrmMain_OnLoad event and I do other things in that event.
"Bruce Wood" <br*******@canada.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
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.

Nov 17 '05 #5
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" <gr****@NOSPAMdahat.com> wrote in message
news:9E**********************************@microsof t.com...
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" wrote:
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

Nov 17 '05 #6
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.

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

Nov 17 '05 #7
I even tried this

public FrmMain()

{

if (LoadLoginForm())

{

InitializeComponent();

}

else

{

Application.Exit();

}

}

Still I see a form.

"CSharpNewBie" <CS**********@VS2003.NET> wrote in message
news:uE*************@tk2msftngp13.phx.gbl...
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

Nov 17 '05 #8
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());
}

}

Nov 17 '05 #9
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" <gr****@NOSPAMdahat.com> wrote in message
news:9E**********************************@microsof t.com...
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" wrote:
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

Nov 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: cefrancke | last post by:
I cant seem to find a straight answer on the following. I want to programmatically hide all menus except a basic custom report menu (during report preview) and right click pop-up A-Z sorting on...
1
by: cefrancke | last post by:
I have set the Startup properties to the following... All menus, toolbars, etc are turned off plus these are unchecked Allow Full Menus Allow Built-in Toolbars Allow Default Shortcut Menus...
2
by: Simon Harvey | last post by:
Hi all, I was wondering how most developers handle the initial startup of their Windows Forms applications? When you make an application in Visual Studio, the IDE places Main in the form that...
4
by: Tony Vitonis | last post by:
Hello. I've written an app that I want to "live" in the system tray. I want it to start up with just a tray icon showing, and if the user selects "Settings..." from the icon's context menu, to...
4
by: Johnnie Miami | last post by:
I'm using VB.Net 2005 beta 2 and have my login form (login.vb) specified as the startup form. If the user is successful logging in, I call my main form (main.vb). This all works fine but the...
10
by: Bernie Hunt | last post by:
This is probably a silly question, but I've gotten myself confused. My app has two forms, form1 and form2. form1 is the start up object in the propers. An event in form1 instantiates form2. ...
3
by: steveeisen | last post by:
I'm a long-time VB6 programmer in a shop that is mostly moving to VB ..NET 2005. And I'm confused about coding the start of solutions for unattended operations. Much of what I write is old-time...
8
by: cj | last post by:
In 2003 I sometimes changed the startup object of a project to Sub Main which was found in Module1.vb. I upgraded one such project to 2005 and I notice in the properties page for the project that...
10
by: =?Utf-8?B?UmljaGFyZCBCeXNvdXRo?= | last post by:
Hi In my app I have a SplashScreen, a login form and a main form. On launching the app, I'd like to show the SplashScreen while reading config files and attempting a database connection. I show...
8
by: Rob | last post by:
In VS2003 you had the option to select "Sub Main" as the Startup Object It appears this option is not present in VS2005... Is this the case ? Thanks !
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.