Main()的启动问题 [英] Startup problem with Main()

查看:101
本文介绍了Main()的启动问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在创建一个Winform应用程序,我需要帮助。让我解释一下我的

问题

这是我在启动时所做的,我在运行时创建一个表单(一个登录屏幕)

和那个将要求用户输入用户/密码输入密码

表格将检查本地文件进行身份验证,(加密文件)然后它

将返回true( loadLoginForm)否则为false,如果使用的clicked取消

基于我必须加载主窗体的输入。一切正常但是

问题是即使它在取消时忽略了Initialcomponent但是

Main()被执行了。如果用户点击

取消退出而不进入Main()

公共FrmMain()

$ b $如何关闭应用程序b {


if(LoadLoginForm())

{InitializeComponent();}


否则


{this.Close();} //这意味着用户点击取消关闭应用程序。


}


[STAThread]


静态无效主要()


{


Application.Run(new FrmMain()); //如果用户

点击取消登录屏幕,我不希望这个执行


}


谢谢


Arun

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

推荐答案

" CSharpNewBie" < CS ********** @ VS2003.NET>在消息中写道

news:uE ************* @ tk2msftngp13.phx.gbl ...
"CSharpNewBie" <CS**********@VS2003.NET> wrote in message
news:uE*************@tk2msftngp13.phx.gbl...

<我正在创建一个Winform应用程序,我需要帮助。让我解释一下我的
问题
这是我在启动时所做的,我在运行时创建一个表单(一个登录屏幕)
这将要求用户在输入时输入用户/密码密码
表单将检查本地文件进行身份验证,(加密文件)然后
它将返回true(loadLoginForm)否则为false如果使用的单击
取消
基于那个输入我必须加载主窗体。一切正常但问题是即使它在取消时忽略了Initialcomponent,但是Main()被执行了。如果用户点击取消退出而不进入Main()公共FrmMain()


如果(如果用户
点击取消退出应用程序如何关闭应用程序LoadLoginForm())

{InitializeComponent();}



{this.Close();} //这意味着用户点击取消关闭申请。

}

[STAThread]

静态无效主要()

{

Application.Run(new FrmMain()); //如果用户在登录屏幕上点击取消,我不希望这个执行

}
谢谢

Arun




为什么不在Application.Run中加载登录表单,如果他们正确登录就可以生成

主应用程序?


问候


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


通过调用Application.Run(new FrmMain() )你导致你的主表单显示

...一个简单的选择是将你的登录表单逻辑拉回

位并尝试类似: />

static void Main()

{

if(LoadLoginForm())

{

Application.Run(new FrmMain());

}

}


您可能需要更改可见性LoadLoginForm或一些

命名空间和/或添加一些使用声明以使

LoadLoginForm()可从Main()访问。


Brendan

" CSharpNewBie"写道:
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:


我正在创建一个Winform应用程序,我需要帮助。让我解释一下我的
问题
这是我在启动时所做的,我在运行时创建一个表单(一个登录屏幕)
这将要求用户在输入时输入用户/密码密码
表单将检查本地文件进行身份验证,(加密文件)然后它将返回true(loadLoginForm)否则为false如果使用点击取消
基于我输入的那个加载主窗体。一切正常,但问题是即使在取消时忽略了Initialcomponent但是
Main()被执行了。如果用户点击
取消退出而不进入Main()公共FrmMain()


如果(如果(如果) LoadLoginForm())

{InitializeComponent();}



{this.Close();} //这意味着用户点击取消关闭申请。

}

[STAThread]

静态无效主要()

{

Application.Run(new FrmMain()); //如果用户在登录屏幕上点击取消,我不希望这个执行

}
谢谢

Arun



你似乎把事情放在了错误的地方......也许是由于你的b / b
误导了你的控制流程应用。我会按照以下方式更改

应用程序,这可能会让事情更清晰:


公共FrmMain()

{

InitializeComponent();

...

}


[STAThread]

static void Main()

{

if(LoadLoginForm())

{

Application.Run (新的FrmMain());

}

}


这摆脱了丑陋的this.Close() FrmMain的构造函数,

永远不会做你想做的事情:你不能不构造

某事并返回除构造对象之外的结果到

来电。不在构造函数中构造某些东西的唯一方法是

抛出异常,这将使你的应用程序失效。


这样,你的Main方法(它总是必须运行,无论如何)

直接调用登录屏幕,如果用户没有登录,则无法启动应用程序

。另一种可能性可能如下:


公共FrmMain()

{

InitializeComponent();

...

}


公共覆盖void OnLoad(EventArgs e)

{

if(!LoadLoginForm())

{

this.Close();

return;

}

}


[STAThread]

static void Main()

{

Application.Run(new FrmMain());

}


这会移动登录对话框弹出窗口和this.Close()进入

OnLoad()方法,对于那种

活动来说,这是一个更合适的地方,让构造函数简单地构建表单,这是它应该做的全部。

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.


这篇关于Main()的启动问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆