[英]How to fix "System.ObjectDisposedException" when closing a form in winform application
[英]System.ObjectDisposedException when closing form
我有一个 Windows 应用程序,它有一个主窗体(主屏幕)和许多子窗体。
当子窗体关闭并从主窗体再次回调时,发生 System.ObjectDisposedException 异常。
以下是我列出的屏幕代码:
调用子窗体的主屏幕代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Application
{
public partial class Home : Form
{
private void Businesslogic_button_Click(object sender, EventArgs e)
{
BusinessRules.Show();
}
}
}
用于处理对象的子表单 Designer.CS 代码:
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
表单关闭事件的子表单 .CS 代码:
bool formClosing false;
private void BusinessRules_FormClosing(object sender, FormClosingEventArgs e)
{
if (formClosing) return;
e.Cancel = true;
Timer Tmr = new Timer();
Tmr.Tick += Tmr_Tick;
Tmr.Start();
formClosing = true;
}
void Tmr_Tick(object sender, EventArgs e)
{
((Timer)sender).Stop();
this.Close();
}
在 Subform.CS 中试试这个:
private void Subform_FormClosing( object sender, FormClosingEventArgs e )
{
e.Cancel = true;
this.Hide();
}
如果您需要存储表单的状态,只需调用 Hide() 方法并设置 e.Cancel = true。 然后再次在表单变量上调用 Show() 以重新打开它。
如果您不想保留状态,只需关闭表单即可。 并从主页打开一个新的表单实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.