简体   繁体   English

打开第二个表单,同时自动关闭第一个表单

[英]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.

相关问题 如何打开一个新表单然后从第二个表单 go 回到第一个表单而不关闭并重新打开第一个表单 - How to open a new form then from the second form go back to the first form without closing and reopening the first form 第一种形式不关闭,第二种不打开 - First form not closing, second not opening 打开第二个表单,右下一个表单 - Open second form, right next first form 如何在第一个表单的相同线程上打开第二个表单? - How to open a second form on the same thread of the first form? 当第一个表格已经加载时,如何打开第二个表格作为对话框? - How to open a second Form as dialog when the first Form is already loaded? 在第一个表单处于活动状态时保持第二个表单可见 - Keep second form visible while first form is active 如何在使用第二个表单时更改第一个表单的属性? - how to change the property of the first form while using a second form? 我想在打开主窗体后打开第二个窗体,但第二个窗体首先打开 - I want to open a second form AFTER opening the main form, but the second form opens first 关闭我的第二个表单时“激活”表单 - “Activate” form when closing my second form 如何在不关闭第一个表单的情况下隐藏第二个表单对话框? - how can i hide my second form dialog whilst not closing my first form?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM