C# 打开一个新窗体并关闭另一个窗体 [英] C# Opening a new form and closing the other one
问题描述
在我的程序中,我在主表单之前显示一个登录表单,一旦程序检测到成功登录,我就使用这个:
In my program I show a login form before the main form, once the program detects a successful login I use this:
MainForm Main = new MainForm();
Main.Show();
this.Hide();
这工作正常,但有一个问题,登录表单仍然打开,即使它被隐藏,所以当程序关闭时,进程仍然挂起,我该如何阻止这种情况发生?
This works fine but there is a problem, the login form is still open even though it is hidden so when the program is closed the process still hangs, how can I stop this from happening?
对不起,忘了补充,使用this.Close();
不起作用,会完全关闭程序.
Sorry, forgot to add, using this.Close();
doesn't work and will completely close the program.
推荐答案
您需要在启动应用程序时以及在此表单的 Load
事件处理程序中指定您的 MainForm
要求登录.在这种情况下,您将在开始时运行应用程序并Login
:
You need to specify your MainForm
when you staring application and in the Load
event handler of this form ask for login. In this case you will have runned application and Login
for on the starting:
Program.cs
Application.Run(new MainForm());
MainForm.cs
private void Form1_Load(object sender, EventArgs e)
{
LoginForm loginForm = new LoginForm();
if (loginForm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
// check login here
}
}
附言Close
将完全关闭您的应用程序,如果它是您应用程序的主要形式.
P.S. Close
will close your application completelly if it's main form of your application.
这篇关于C# 打开一个新窗体并关闭另一个窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!