我想浏览表单,所以当我客户端buttonKlient时,我转到Form2然后当我点击控制'x'时,我回到Form1我得到了:
(fk
是Form2
)
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
。
答案 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;
才能取消表单结束。