简体   繁体   English

Form1 不会关闭。 (C#)

[英]Form1 will not close. (C#)

I'm practicing some C# and I decided to make an extremely simple login screen.我正在练习一些 C#,我决定制作一个非常简单的登录屏幕。 I would like the login screen to close after the password is entered successfully, but I cannot seem to do it.我想在成功输入密码后关闭登录屏幕,但我似乎无法这样做。

        private void button1_Click(object sender, EventArgs e)
    {
        if (textBox1.Text == "pwhere")

        {

            Form2 form2 = new Form2();
            Form1 form1 = new Form1();

            {

                form2.ShowDialog();
                form1.Close();
            }

        }

    }

Thanks!谢谢! :) This button is in Form1 if that causes any trouble btw. :) 这个按钮在 Form1 中,如果这会导致任何麻烦顺便说一句。

Fixed it.修复。

        private void button1_Click(object sender, EventArgs e)
    {
        if (textBox1.Text == "rbxgod")

        {

            Form2 form2 = new Form2();
            form2.Show();
            Hide();
        }

    }

This button is in Form1 if that causes any trouble btw.这个按钮在 Form1 中,如果这会导致任何麻烦顺便说一句。

Assuming Form1 is your startup form, you can hide it, then call ShowDialog() with Form2, and finally close the original Form allowing the application to exit gracefully:假设Form1是你的启动窗体,你可以隐藏它,然后用Form2调用ShowDialog(),最后关闭原来的Form,让应用程序优雅退出:

this.Hide();
Form2 f2 = new Form2();  
f2.ShowDialog();
this.Close();

Alternatively, you could wire up the FormClosed() event of Form2 and close the current form from there.或者,您可以连接 Form2 的 FormClosed() 事件并从那里关闭当前表单。 This would allow you to use Show() instead of ShowDialog():这将允许您使用 Show() 而不是 ShowDialog():

this.Hide();
Form2 f2 = new Form2();
f2.FormClosed += (s2, e2) => { this.Close(); };
f2.Show();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM