C# Windows 窗体:登录后创建主应用程序,运行哪个窗体?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1629205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 19:31:48  来源:igfitidea点击:

Windows Forms: create the main application after login, which form to run?

c#winforms

提问by Petr

Using Windows FormsI would like to have a small login screen the user authorizes himself/herself through (say its Form1), so the main application (say its Form2) would be opened after login. But I suppose when I use Application.Run(Form1), after closing it the whole application closes.

使用Windows 窗体我想要一个小的登录屏幕,用户可以通过(比如它的 Form1)授权他/她自己,所以主应用程序(比如它的 Form2)将在登录后打开。但是我想当我使用 Application.Run(Form1) 时,关闭它后整个应用程序都会关闭。

Isn't there any other way except using invisible Form2? Something like run Form2 on demand and close originally ran Form1? Hope it makes sense to you :)

除了使用不可见的 Form2 之外没有其他方法吗?像按需运行Form2并关闭最初运行Form1之类的东西?希望它对你有意义:)

采纳答案by elder_george

Create an overload of System.Windows.Forms.ApplicationContext, create Form1first and then Form2in its constructor.

创建 的重载System.Windows.Forms.ApplicationContextForm1首先创建然后Form2在其构造函数中。

Use Application.Runoverload that accepts ApplicationContextobject.

使用Application.Run接受ApplicationContext对象的重载。

回答by VVS

The ApplicationContextclass is what you need. There's an Application.Run(ApplicationContext)overload you can call.

ApplicationContext班是你所需要的。这里有一个Application.Run(ApplicationContext)你可以叫超载。

See here for an example: http://msdn.microsoft.com/en-us/library/system.windows.forms.applicationcontext.aspx

请参见此处的示例:http: //msdn.microsoft.com/en-us/library/system.windows.forms.applicationcontext.aspx

回答by Lou

Try this approach. From your program mainline create your main form class, from within this class have a "go" function that calls out to a login form. If this function returns true you can proceed with a call to Application.Run(form).

试试这个方法。从你的程序主线创建你的主表单类,在这个类中有一个调用登录表单的“go”函数。如果此函数返回 true,您可以继续调用 Application.Run(form)。

MainForm form = new MainForm();
form.Show();
if (form.go())
{
  Application.Run(form);
}
else
{
  form.Close();
}

class MainForm 
{
  public bool go()
  {
    LoginFrom lf = new LoginForm()
    if (lf.ShowDialog() != DialogResult.OK)
    {
      return false;
    }
  }
}

A little simplistic perhaps but it should get you going in the right direction.

也许有点简单,但它应该让你朝着正确的方向前进。

回答by ManiacZX

You can call your authentication form before starting up your main application form inside of Program.cs (default name), such as:

您可以在 Program.cs(默认名称)内启动主应用程序表单之前调用您的身份验证表单,例如:

    static void Main()
    {
        Form1 f1 = new Form1();
        DialogResult dr = f1.ShowDialog();
        if (dr == DialogResult.OK)
        {
            Application.Run(new Form2());
        }
        else
        {
            Application.Exit();
        }
    }

Inside of Form1 if they properly authenticate then you just need to end with:

在 Form1 内部,如果它们正确地进行了身份验证,那么您只需要以以下内容结尾:

    this.DialogResult = DialogResult.OK;
    this.Close();

If the authentication fails, you can allow them to re-attempt authentication, give them a max number of attempts, etc. Then when you decide they have had too much just call

如果身份验证失败,您可以允许他们重新尝试身份验证,给他们最大尝试次数等。然后,当您确定他们已经太多时,请致电

    Application.Exit();

回答by Kumars

Try using settings in project properties

尝试使用项目属性中的设置

  1. Go to project properties
  2. Go to settings tab, and create setting 'lcheck' of bool type and value is 'false'
  3. Let form1 is main form and form2 is logon form
  4. Use while loop in form1- formload function like

    while(System.properties.default.lcheck!=true)
    {
        authentication process:
    }
    
  1. 转到项目属性
  2. 转到设置选项卡,并创建 bool 类型的设置“lcheck”,值为“false”
  3. 设form1为主窗体,form2为登录窗体
  4. 在 form1-formload 函数中使用 while 循环,例如

    while(System.properties.default.lcheck!=true)
    {
        authentication process:
    }
    

Note: I have a doubt about this process. Somebody may be able to change the settings from the appdata folder of the application.

注意:我对这个过程有疑问。有人可能能够从应用程序的 appdata 文件夹中更改设置。