简体   繁体   English

如何从另一个表单关闭一个表单

[英]How to close a form from another form

My 1st form is frmLeaveRequest. 我的第一种形式是frmLeaveRequest。

And My 2nd Form is frmLeaveRequestConfirmation. 我的第二个表单是frmLeaveRequestConfirmation。

There is a button named "Confirm" in 2nd form. 第二种形式的按钮名为“确认”。 I want to close those 2 forms when click on that button. 我要在单击该按钮时关闭这2个表单。

I tried to accomplish that task by following code. 我试图通过以下代码来完成该任务。

        frmLeaveRequest frm = new frmLeaveRequest() 
        frm.Close();
        this.Close();

but it's doesn't work because it creates just a new object and not the same which I want to close. 但这是行不通的,因为它只会创建一个新对象,而不是我要关闭的对象。

Please give me a solution to accomplies that task. 请给我一个解决方案以完成该任务。

//Form 1
private void button1_click(object sender, EventArgs e)
{
    frmLeaveRequestConfirmation frmForm2 = new frmLeaveRequestConfirmation();
    frmForm2.FormClosed += new FormClosedEventHandler(frmForm2_FormClosed);
    frmForm2.Show();

}

private void frmForm2_FormClosed(object sender, FormClosedEventArgs e)
{
   this.Close();
}


//Form 2
private void btnConfirm_Click(object sender, EventArgs e)
{
    this.Close();
}

您可以将这些表单作为参数发送到新的表单构造函数,因此您可以引用它们并关闭它们。

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

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