473,657 Members | 2,574 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 Initialcomponen t 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( ))

{InitializeComp onent();}

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 1606
"CSharpNewB ie" <CS**********@V S2003.NET> wrote in message
news:uE******** *****@tk2msftng p13.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 Initialcomponen t 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( ))

{InitializeComp onent();}

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
"CSharpNewB ie" 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 Initialcomponen t 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( ))

{InitializeComp onent();}

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
misunderstandin g of control flow in your application. I would change
the application as follows, which might make things clearer:

public FrmMain()
{
InitializeCompo nent();
...
}

[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()
{
InitializeCompo nent();
...
}

public override void OnLoad(EventArg s 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*******@cana da.com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.com.. .
You seem to have things in the wrong place... perhaps due to a
misunderstandin g of control flow in your application. I would change
the application as follows, which might make things clearer:

public FrmMain()
{
InitializeCompo nent();
...
}

[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()
{
InitializeCompo nent();
...
}

public override void OnLoad(EventArg s 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\FrmMa in.cs(238): An object reference is required for the nonstatic
field, method, or property 'caUtil.FrmMain .LoadLoginForm( )'

"Brendan Grant" <gr****@NOSPAMd ahat.com> wrote in message
news:9E******** *************** ***********@mic rosoft.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
"CSharpNewB ie" 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 Initialcomponen t 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( ))

{InitializeComp onent();}

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.ShowD ialog() == 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( ))

{

InitializeCompo nent();

}

else

{

Application.Exi t();

}

}

Still I see a form.

"CSharpNewB ie" <CS**********@V S2003.NET> wrote in message
news:uE******** *****@tk2msftng p13.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 Initialcomponen t 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( ))

{InitializeComp onent();}

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****@NOSPAMd ahat.com> wrote in message
news:9E******** *************** ***********@mic rosoft.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
"CSharpNewB ie" 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 Initialcomponen t 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( ))

{InitializeComp onent();}

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
6886
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 datasheets (for subforms). I would like to do this on startup of the application. To be clear:
1
11297
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 Allow Toolbar/Menu Changes Use Access Special Keys
2
1500
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 VS makes for you. When I make my applications I sometimes feel uneasy about leaving the startup logic in the Form - it sort of seems the wrong place for it. I'm by no means an expert on such things (which is why I'm writing this post), but it...
4
1901
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 display a window that will allow him to change settings. When he hits "OK" or "Cancel", the window should hide again. At first, I tried putting a "Me.Visible = False" in the form's Load event, but apparently that code runs before the command can...
4
7844
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 problem is that the login form stays open. I put a me.close (in the login form) after opening the main form but that seems to close everything and the main form is not displayed. I thought I could call a Sub Main() that calls the login form, closes...
10
2994
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. Dim myForm as HardwareStore myForm = New HardwareStore myForm.Show() I understand that my form2 can be referenced by
3
1578
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 batch. Such programs are started from a scheduler and run on a server, cycling through a mass of data, without human intervention. No form should show on the server monitor, although an invisible one may be needed to support the Winsock or...
8
3558
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 nothing is selected as the startup object. It appears to function but should I set it to Sub Main?
10
22363
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 progress of these tasks on a label on the SplashScreen form. Once this is completed ok, the splash screen should close and the login form should be displayed. A successful login closes that form and shows the main form.
8
1123
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
8392
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8305
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8823
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8730
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4151
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1950
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.