简体   繁体   English

关闭form2时关闭form1(从红色十字按钮开始)

[英]Close form1 when closing form2 (from the red cross button)

I have two forms .. the form1 is a login form. 我有两种形式.. form1是一个登录表单。 so if the username and the password typed correctly .. the user will be directed to the form2 and hide form1.. The form2 contain a button "logout" for closing form2 and returning to form1 .. 因此,如果用户名和密码输入正确..用户将被定向到form2并隐藏form1 .. form2包含一个按钮“注销”,用于关闭form2并返回到form1 ..

this is the code for the button logout 这是按钮注销的代码

  private void logout_Click(object sender, EventArgs e)
    {
        this.Close();
        foreach (Form OpenedForm in Application.OpenForms)
        {
            if (OpenedForm is Form1)
            {
                OpenedForm.Show();
                break;
            }
        }
    }

everything is ok with that .. but the only problem appears when the user clicks the red cross to exit the application directly without logging-out .. in this case the form1 (login) still hidden .. Is there a solution to close form1 when closing form2 from the red cross ? 一切都没问题..但唯一的问题是当用户点击红叉以直接退出应用程序而不登出时出现..在这种情况下,form1(登录)仍然隐藏..是否有解决方案关闭form1时从红十字会关闭form2? thanks and sorry for my poor english 谢谢,抱歉我的英语不好

You can try to use FormClosing Event . 您可以尝试使用FormClosing事件

private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
    // close the first form...
}

This thread can help you to detect when the red cross was clicked... 此线程可以帮助您检测何时单击红叉...

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

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