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

时间:2013-01-19 22:48:34

标签: c# forms navigation

我想浏览表单,所以当我客户端buttonKlient时,我转到Form2然后当我点击控制'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控件关闭它,然后再次点击按钮1,我得到例外ObjectDisposedException

1 个答案:

答案 0 :(得分:0)

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
}

您正在隐藏表单,但表单仍在处理中。您需要添加e.Cancel = true;才能取消表单结束。