0

我想浏览表单,所以当我客户端 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然后再次单击 button1 并出现异常。Form2 fkxObjectDisposedException

4

1 回答 1

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;以取消表单关闭。

于 2013-01-19T23:05:58.867 回答