如何在不关闭应用程序的情况下关闭登录表单并显示主表单? [英] How can I close a login form and show the main form without my application closing?

查看:30
本文介绍了如何在不关闭应用程序的情况下关闭登录表单并显示主表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have two forms in my project (Login and Main).

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

What I'm trying to accoomplish is, if the login is successful, I must show the Main form and close the Login form.

我在登录表单中有这个方法,当登录成功时关闭登录表单.但主窗体不显示.

I have this method in Login form that closes the Login form when the login is successful. But the Main form doesn't show.

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.");
    }         
}

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

I tried hiding the Login form if the login process is successful. But it bothers me because I know while my program is running the login form is still there too, it should be closed right?

对此的正确方法应该是什么?谢谢...

What should be the right approach for this? Thanks...

推荐答案

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

The reason your main form isn't showing is because once you close the login form, your application's message pump is shut down, which causes the entire application to exit. The Windows message loop is tied to the login form because that's the one you have set as the startup form in your project properties. Look in your "Program.cs" file, and you'll see the responsible bit of code: Application.Run(new LoginForm()). Check out the documentation for that method here on MSDN, which explains this in greater detail.

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

The best solution is to move the code out of your login form into the "Program.cs" file. When your program first starts, you'll create and show the login form as a modal dialog (which runs on a separate message loop and blocks execution of the rest of your code until it closes). When the login dialog closes, you'll check its DialogResult property to see if the login was successful. If it was, you can start the main form using Application.Run (thus creating the main message loop); otherwise, you can exit the application without showing any form at all. Something like this:

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

这篇关于如何在不关闭应用程序的情况下关闭登录表单并显示主表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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