在c#中导航表单时抛出ObjectDisposeException

本文关键字:ObjectDisposeException 表单 导航 | 更新日期: 2023-09-27 17:54:25

我想在表单中导航所以当我点击buttonclient时,我进入Form2然后当我点击control 'x'时,我回到Form1,我得到:

(fkForm2)

   private void button1_Click(object sender, EventArgs e) {
                if (fk == null)
                    fk = new OknoKlient();
                fk.Tag = this;
                fk.Show(this);//here is ObjectDisposedException
                Hide();
            }

然后在Form2

 protected override void OnFormClosing(FormClosingEventArgs e) {
            if (e.CloseReason == CloseReason.WindowsShutDown) return;
            var form1 = (Form1)Tag;
            form1.Show();
            Hide();
            // DO WHATEVER HERE
        }

当我单击button1时,Form2 fk打开,然后我通过x控件关闭它,然后再次单击button1,我得到例外ObjectDisposedException

在c#中导航表单时抛出ObjectDisposeException

protected override void OnFormClosing(FormClosingEventArgs e) {
    if (e.CloseReason == CloseReason.WindowsShutDown) return;
    e.Cancel = true; // <---------------------- this
    var form1 = (Form1)Tag;
    form1.Show();
    Hide();
    // DO WHATEVER HERE
}

你正在隐藏表单yes..但表格还在处理中。您需要添加e.Cancel = true;来取消表单关闭

相关文章:
  • 没有找到相关文章