Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
198 views
in Technique[技术] by (71.8m points)

c# - How to reopen a previously closed windows form. "Cannot access a disposed Object"

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

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.

How will I be able to open again my 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;
    }
}                

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

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

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();
}        

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;
}

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...