[英]Open a second form while automatically closing the first form
I created a login form in c#. 我在c#中创建了一个登录表单。
If a user signs in with the right password and username and clicks on 'Login' then the second form opens. 如果用户使用正确的密码和用户名登录并单击“登录”,则会打开第二个表单。 How do I close the login form after the last step?
最后一步之后如何关闭登录表单?
Change your Main() method in Program.cs to display the login dialog. 在Program.cs中更改Main()方法以显示登录对话框。 Don't start the message loop unless a valid login was entered.
除非输入有效的登录名,否则不要启动消息循环。 For example:
例如:
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
using (var login = new LoginForm()) {
if (login.ShowDialog() != DialogResult.OK) return;
}
Application.Run(new Form1());
}
Your LoginForm should set its DialogResult property to OK if it detected a proper login. 如果LoginForm检测到正确的登录名,则应将其DialogResult属性设置为OK。
You can type 您可以输入
FormName.ActiveForm.Close();
It closes the current active form. 它关闭当前的活动窗体。
您可以调用this.Close();
I would do a ShowDialog() of the Login Form from the main form. 我将从主窗体执行登录窗体的ShowDialog()。
After the login form closes you are back in the main form. 登录表单关闭后,您将返回主表单。
private void Form1_Load(object sender, EventArgs e)
{
var foo = new Form() { Text = "Login" };
if (foo.ShowDialog() == DialogResult.OK)
{
...
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.