-1

I am trying to hide my main form on a certain event and then show it again later. The problem is that the form gets disposed when I hide it.

My code for hiding the form:

private void MessageRecived(object sender)
{
    //Do stuff

    if (status == NetConnectionStatus.Connected)
    {
        this.Hide();
    }
    else if (status == NetConnectionStatus.Disconnected)
    {
        this.Show();
    }

    //Do some more stuff
}

When the "this.Show()" method is called, the following exception is thrown:

System.ObjectDisposedException

Additional information: Cannot access a disposed object.

I have also tried to use "this.Visible = false" and "this.SetVisibleCore(false)" but I get the same result.

How can I hide the form without it getting disposed?

EDIT:

I found my mistake: There was an object in my code that referenced the form, and it closed it. Thanks to Justin Harvey who pointed out that something else is using the form.

Community
  • 1
  • 1
  • 1
    Unrelated, but you're not checking for equality in your if statements, you're assigning status. Change those to `status == NetConnectionStatus.Connected` and `status == NetConnectionStatus.Disconnected`. Also, what is happening in `//Do stuff` and `// Do some more stuff`, as `Control.Hide` does not dispose by design. – AntiTcb Apr 28 '16 at 11:55
  • 3
    It is not the hiding that is disposing it, can you show the code that uses the form? – Justin Harvey Apr 28 '16 at 11:55
  • @AlexGravely you are correct, I will fix it – user6262616 Apr 28 '16 at 11:55
  • yea when is `MessageRecived` called? – Daniel A. White Apr 28 '16 at 11:57
  • The only other code that uses the form is a button click that connects to a server – user6262616 Apr 28 '16 at 11:57
  • The MessageRecived is called when the application recives a message from a remote server – user6262616 Apr 28 '16 at 11:58
  • Does the form open as hidden? Chances are that the handle isn't created if that's the case. – Barry O'Kane Apr 28 '16 at 12:00
  • You can look at this link [http://stackoverflow.com/a/3742980/3206674](http://stackoverflow.com/a/3742980/3206674) and also [http://stackoverflow.com/questions/70272/single-form-hide-on-startup](http://stackoverflow.com/questions/70272/single-form-hide-on-startup) – dev.burak Apr 28 '16 at 12:00
  • @BarryO'Kane The form is opend shown – user6262616 Apr 28 '16 at 12:01
  • You should create a [mcve] that demostrates the issue. – Sayse Apr 28 '16 at 12:12

1 Answers1

0

It seems like your form is Disposed by Garbage Collector or by some other code. Setup breakpoint in

protected override void Dispose(bool disposing)

method (usualy inside NAME.Designer.cs file)

I've made the following experiment and it works well!

  1. Create new WindwsForms Application
  2. Place Time on main form in Form Designer
  3. Add FormClosingevent handler
  4. Add Time Tick event handler
  5. Write the following code:

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (MessageBox.Show("Press Yes to Hide only the Form?", "Exit", MessageBoxButtons.YesNo) ==
            DialogResult.Yes)
        {
            e.Cancel = true;
            timer1.Enabled = true;
            Hide();
        }
    }
    
    private void timer1_Tick(object sender, EventArgs e)
    {
        Show();
        timer1.Enabled = false;
    }
    
Dmitriy Zapevalov
  • 1,357
  • 8
  • 13