如何重新打开以前关闭的Windows窗体. “无法访问已处置的对象". [英] How to reopen a previously closed windows form. "Cannot access a disposed Object"

查看:130
本文介绍了如何重新打开以前关闭的Windows窗体. “无法访问已处置的对象".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里已经阅读了几篇类似的文章,但是在我的问题中找不到解决方法.

I've read several things like this here but found no solution in my problem.

我正在将数据从form1发送到我的tempGraph表单.一切正常,直到我关闭tempGraph表单并尝试重新打开它.当我尝试重新打开时,它说CANNOT ACCESS A DISPOSED OBJECT,这现在是我的问题.

I'm sending data from form1 to my tempGraph form. Everything is OK not until I close my tempGraph form and try to reopen it. as i try to reopen it says CANNOT ACCESS A DISPOSED OBJECT which is now my problem.

我如何能够再次打开我的tempGraph?

How will I be able to open again my tempGraph?

这是我的代码,用于将数据发送到诸如tempGraph之类的不同形式:

This is my code for sending data to different forms like my tempGraph:

 public void SetText(string text)//Set values to my textboxes
{
    if (this.receive_tb.InvokeRequired)
    {   
        SetTextCallback d = new SetTextCallback(SetText);
        this.Invoke(d, new object[] { text });
    }
    else
    {  var controls = new TextBox[]
       {    wdirection_tb,
            wspeed_tb,
            humidity_tb,
            temperature_tb,
            rainin_tb,
            drainin_tb,
            pressure_tb,
            light_tb
        };
        data = text.Split(':');
        for (int index = 0; index < controls.Length && index < data.Length; index++) // This code segment Copy the data to TextBoxes
        {   
            TextBox control = controls[index];
            control.Text = data[index];
            //planning to pud the code for placing data to DataGridView here.
        }
            //or Place a code here to Call a UDF function that will start copying files to DataGridView
        //index1++; //it will count what row will be currently added by datas
        if (data.Length != 0)
        { datagridreport(temperature_tb.Text.ToString(), humidity_tb.Text.ToString(),     pressure_tb.Text.ToString());  }  


        //sending of data to each graph. THIS CODE SENDS DATA TO OTHER FORMS
        tempG.temp.Text = temperature_tb.Text;
        humdidG.humid.Text = humidity_tb.Text;
        pressG.Text = pressure_tb.Text;


        //updating textbox message buffer
        this.receive_tb.Text += text;
        this.receive_tb.Text += Environment.NewLine;
    }
}                

这是我打开位于form1中的tempGraph的代码:

Here are my codes in opening the tempGraph located in my form1:

private void temperatureToolStripMenuItem_Click(object sender, EventArgs e) 
{            
    tempG.Show();
}

然后使用右上角的X按钮关闭tempG/tempGraph或使用以下命令使用按钮将其关闭:

and I close my tempG/tempGraph using the X button located in the upper right or closing it using a button with the following command:

private void button1_Click(object sender, EventArgs e)
{
    timer1.Stop();
    timer1.Enabled = false;
    TempGraph.ActiveForm.Close();
}        

注意:当我关闭它后重新打开tempGraph时,会发生错误.

Note: When I reopen my tempGraph after closing it the error occurs.

推荐答案

之所以会发生这种情况,是因为您已将对当前tempGraph表单的引用存储在全局变量中,并且当您关闭当前实例时,该变量仍保留对它的引用.现在已处置的物体.

This happens because you have stored the reference to the current tempGraph form in a global variable and when you close the current instance that variable still holds a reference to a now disposed object.

解决方案是在您的主窗体中获取已关闭事件,并将全局变量重置为空.

The solution is to get the closed event in you main form and reset to null the global variable.

因此,要更改菜单,请单击

So suppose to change you menu click to

private void temperatureToolStripMenuItem_Click(object sender, EventArgs e) 
{            
    if(tempG == null)
    {
        tempG = new tempGraph();
        tempG.FormClosed += MyGraphFormClosed;
    }
    tempG.Show();                
}

并在您的主表单中添加以下事件处理程序

and add the following event handler in your main form

private void MyGraphFormClosed(object sender, FormClosedEventArgs e)
{
    tempG = null;
}

现在,当关闭由tempG引用的tempGraph表单实例时,系统将通知您,您可以将全局变量tempG设置为null.当然,现在您需要在使用该变量之前检查所有地方,但是当您调用tempG.Show()时,一定要确保它指向正确的NON DISPOSED实例.

Now, when the tempGraph form instance referenced by tempG is closed you will be notified and you could set the global variable tempG to null. Of course now you need to check everywhere before using that variable but when you call tempG.Show() you are sure to have it point to a correctly NON DISPOSED instance.

这篇关于如何重新打开以前关闭的Windows窗体. “无法访问已处置的对象".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆