我正在使用WinForms。我有2种形式,Form1 (主表格)和Form2 (子表格)。当用户点击form2顶部的“X”按钮时,我想关闭form1。在我的代码中,我试图通过说this.Owner.Close();
来关闭form1,但它会引发错误。为什么抛出这个错误,当用户单击窗体顶部的“X”按钮时,如何关闭子窗体的主窗体。如果用户点击“X”按钮,从子窗体关闭父窗体
错误
类型 'System.StackOverflowException' 的未处理的异常发生在System.Windows.Forms.dll中
表1
private void btn_Open_Form2_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Owner = this;
frm2.Show();
this.Hide();
}
窗体2
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
this.Owner.Close();
}
'Application.Exit();' – Xaqron
你关闭主人。这将关闭其拥有的窗户。这将引发FormClosing事件。这将关闭所有者。这将关闭其拥有的窗户。这将引发FormClosing事件。这将关闭所有者。这将关闭其拥有的窗户。这将引发FormClosing事件。哪个... Kaboom。使用* bool *变量来中断递归。或FormClosed事件。 –
你为什么要这么做呢?这不是好用户体验。 – CodingYoshi