在我关闭表格后,当我尝试重新打开它时,我遇到了错误信息"Cannot access a disposed object. Object name: 'ApplicationProperties'."。我注意到这是因为退出表格时,程序正在"销毁"表格,所以我已经在所有的确认按钮和取消按钮(任何会关闭一个表格的按钮)中加入了以下代码。
this.Hide();
this.Parent = null;
这段代码只是隐藏了表单,并没有关闭表单
我的问题在于,当我点击表单上的“x”按钮后,再尝试重新打开表单时,仍然会收到错误消息。我尝试了几种修改表单现有函数的方法,例如:
private void ApplicationProperties_FormClosing(object sender, FormClosingEventArgs e)
{
//Hiding the window, because closing it makes the window unaccessible.
this.Hide();
this.Parent = null;
}
但是这没带给我任何好运。我想知道是否有人知道如何解决这个问题。以下是在取消和接受按钮内对我有效的代码。它对于所有关闭表单的按钮都是相同的。
private void OptionsCancelbtn_Click(object sender, EventArgs e)
{
//Hiding the window, because closing it makes the window unaccessible.
this.Hide();
this.Parent = null;
}
我已经在form1的顶部声明了一个实例,并且在form1内有一个按钮可以打开form2。
public partial class MainBox : Form
{
//Making a name for the ApplicationProperties form. It can be opened when called.
ApplicationProperties ApplicationPropertiesWindow = new ApplicationProperties();
private void ApplicationPropertiesbtn_Click(object sender, EventArgs e)
{
//Show the properties window.
ApplicationPropertiesWindow.Show();
}//End ApplicationProperties button.
}
当我用第二个窗体上的'x'按钮关闭程序后,由于错误消息在ApplicationPropertiesWindow.Show();
处触发,我无法再次访问form2。
在form2内部,我有以下代码:
public partial class ApplicationProperties : Form
{
//Creates and sets the instance MainBoxWindow.
public MainBox MainBoxWindow { get; set; }