如何在不关闭应用程序的情况下关闭登录表单并显示主表单?

yon*_*236 41 .net c# winforms

我的项目中有两个表单(Login和Main).

我想要实现的是,如果登录成功,我必须显示主窗体并关闭登录表单.

我在Login表单中有这个方法,当登录成功时关闭Login表单.但主要形式没有显示.

public void ShowMain()
{
    if(auth()) // a method that returns true when the user exists.
    {             
        var main = new Main();
        main.Show();
        this.Close();
    }
    else
    {
        MessageBox.Show("Invalid login details.");
    }         
}
Run Code Online (Sandbox Code Playgroud)

如果登录过程成功,我尝试隐藏登录表单.但它困扰我,因为我知道当我的程序运行时,登录表单仍然存在,它应该关闭吗?

应该采用什么方法?谢谢...

Cod*_*ray 101

您的主窗体未显示的原因是,一旦您关闭登录表单,您的应用程序的消息泵将关闭,这将导致整个应用程序退出.在的Windows消息循环,因为这是你已经设置为你的项目属性中的启动形式一个是联系在一起的登录表单.查看"Program.cs"文件,您将看到负责任的代码:Application.Run(new LoginForm()).在MSDN上查看该方法的文档,这将更详细地解释这一点.

最好的解决方案是将代码从登录表单移到"Program.cs"文件中.当您的程序首次启动时,您将创建并将登录表单显示为模式对话框(在单独的消息循环上运行并阻止执行其余代码直到它关闭).登录对话框关闭后,您将检查其DialogResult属性以查看登录是否成功.如果是,您可以使用Application.Run(因此创建主消息循环)启动主窗体; 否则,您可以退出应用程序而不显示任何形式.像这样的东西:

static void Main()
{
    LoginForm fLogin = new LoginForm();
    if (fLogin.ShowDialog() == DialogResult.OK)
    {
        Application.Run(new MainForm());
    }
    else
    {
        Application.Exit();
    }
}
Run Code Online (Sandbox Code Playgroud)


MrE*_*yes 8

反过来我会这样做.

在主窗体的OnLoad事件中,将"登录"窗体显示为对话框.如果对话结果为OK,则允许Main继续加载,如果结果是身份验证失败,则中止加载并显示消息框.

编辑代码示例

private void MainForm_Load(object sender, EventArgs e)
{
    this.Hide();

    LogonForm logon = new LogonForm();

    if (logon.ShowDialog() != DialogResult.OK)
    {
        //Handle authentication failures as necessary, for example:
        Application.Exit();
    }
    else
    {
        this.Show();
    }
}
Run Code Online (Sandbox Code Playgroud)

另一个解决方案是在program.cs中显示Main方法的LogonForm,如下所示:

static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    LogonForm logon = new LogonForm();

    Application.Run(logon);

    if (logon.LogonSuccessful)
    {
        Application.Run(new MainForm());
    }
}
Run Code Online (Sandbox Code Playgroud)

在此示例中,LogonForm必须公开LogonSuccessful bool属性,该属性在用户输入有效凭据时设置为true

  • 为什么要在登录成功之前加载主表单? (5认同)

MoM*_*oMo 7

这是我的解决方案。创建 ApplicationContext 以设置应用程序的主窗体,并在要打开新窗体和关闭当前窗体时更改主窗体。

程序.cs

static class Program
{
    static ApplicationContext MainContext = new ApplicationContext();

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        MainContext.MainForm = new Authenticate();
        Application.Run(MainContext);
    }

    public static void SetMainForm(Form MainForm)
    {
        MainContext.MainForm = MainForm;
    }

    public static void ShowMainForm()
    {
        MainContext.MainForm.Show();
    }
}
Run Code Online (Sandbox Code Playgroud)

当登录过程完成时。

private void BtLogin_Click(object sender, EventArgs e)
    {
        //Login Process Here.

        Program.SetMainForm(new Portal());
        Program.ShowMainForm();

        this.Close();
    }
Run Code Online (Sandbox Code Playgroud)

我希望这能帮到您。


小智 5

这很简单.

这是代码.

 private void button1_Click(object sender, EventArgs e)
 {  
        //creating instance of main form
        MainForm mainForm = new MainForm();

        // creating event handler to catch the main form closed event
        // this will fire when mainForm closed
        mainForm.FormClosed += new FormClosedEventHandler(mainForm_FormClosed);
        //showing the main form
        mainForm.Show();
        //hiding the current form
        this.Hide();
  }

  // this is the method block executes when main form is closed
  void mainForm_FormClosed(object sender, FormClosedEventArgs e)
  {
       // here you can do anything

       // we will close the application
       Application.Exit();
  }
Run Code Online (Sandbox Code Playgroud)