-1

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.

Nikolay Kostov
  • 16,433
  • 23
  • 85
  • 123
Solem
  • 17
  • 2
  • 7
  • i actually was be able to reopen it by adding this code "TempGraph tempG = new TempGraph();" in my private void temperatureToolStripMenuItem_Click. But the problem is i cant send anydata to my graphforms. – Solem Jan 06 '15 at 18:31
  • Calling `Close` on a `Form` will dispose the underlying resources of the `Form` so it cannot be shown again, calling `Hide` will make the `Form` no longer visible but not dispose of the underlying resources. – Anthony Jan 06 '15 at 19:11

1 Answers1

3

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.

Steve
  • 213,761
  • 22
  • 232
  • 286
  • We should really understand in which point this exception is raised. Do you have code in the tempGraph form executed at Constructor/Load/Activate time? Can you use the debugger to follow step by step what happen when you call Show or when you call SetText? – Steve Jan 06 '15 at 18:47
  • im calling setText to parse data into different textboxes after my function read(). This Read function is the one fetches data from my port. – Solem Jan 06 '15 at 18:52
  • In my tempGraph form. im actually creating a graph in there using zed and everything is okey and its running in a timer function also im utilizing a TempGraph_Load function to set the graph settings – Solem Jan 06 '15 at 18:53
  • Are you sure the message is _a disposed FILE_? Usually is _a disposed object_ – Steve Jan 06 '15 at 19:01
  • im getting the point.... we can avoid disposed object by simple creating a new instance of tempG by initializing it again. but the problem is when we initialized it again the data will not the send since there is a new tempG and the tempG im my function setText is still the old tempG. – Solem Jan 06 '15 at 19:09
  • @Solem It sounds like you need to not be closing the form in the first place, perhaps you really should just be hiding it. [See my comment](http://stackoverflow.com/questions/27804851/how-to-reopen-a-previously-closed-windows-form-cannot-access-a-disposed-object#comment44019935_27804851) to the original question. – Anthony Jan 06 '15 at 19:12
  • hmmm ill be trying your advices guys. Maybe i should disable the "temperatureToolStripMenuItem_Click" if the port is still close to avoid the user from opening the tempGraph form and so i could use the hide function when the port is already open. right? – Solem Jan 06 '15 at 19:24
  • Looking at your SetText. humidG and pressG are other forms with the same usage pattern of your tempG? If yes do you apply the same pattern used for tempG? – Steve Jan 06 '15 at 19:49
  • Plus one million for the concise, elegant, and correct, answer to the OP's (and my) problem! – buzzard51 Jun 07 '16 at 17:39