How to show child form with cannot access a dispose object error

S

scottiedog

Hi,
I have child form attached to a parent form. It is declare in parent
form.

private frmChild child;

I also have an event to show the form and activate it.

if (this.child == null)
{
this.child = new frmChild();
this.child.MdiParent = this;
}

this.child.Show();
this.child.Activate();

No problem when called for the very first time to show the child form.
Problem occurs when the child from is closed and the called again to
show. "Cannot access a dispose object" error occurs on .Show() line.

I do not want to just hide the child form. Can I completely dispose
the form so that it can be called again immediately? How do I do
that? Or is there a way to make the .Show() work again?

Thanks.
 
P

Peter Duniho

scottiedog said:
[...]
No problem when called for the very first time to show the child form.
Problem occurs when the child from is closed and the called again to
show. "Cannot access a dispose object" error occurs on .Show() line.

I do not want to just hide the child form. Can I completely dispose
the form so that it can be called again immediately? How do I do
that? Or is there a way to make the .Show() work again?

No. As the exception is telling you, once you've disposed the instance,
it can no longer be used.

You have two options: don't dispose the instance, or create a new one
when you want to show the form again. If you really are committed to
your statement that you "do not want to just hide the child form", then
only the second option will work.

Pete
 
S

scottiedog

Thanks for reply.

Yes, I would like to recreate them. But when I check for frmChild ==
null, it is not so the new frmChild doesn't get called. So with
simple this.Close() from child form doesn't get rid of the object and
yet leave the object disposed.

How can I truely get rid of it so that I can new frmChild() next time
around?

Thanks.
 
P

Peter Duniho

scottiedog said:
Thanks for reply.

Yes, I would like to recreate them. But when I check for frmChild ==
null, it is not so the new frmChild doesn't get called. So with
simple this.Close() from child form doesn't get rid of the object and
yet leave the object disposed. [...]

Why do you care if the value of the field is null?

You can, of course, set it to null when the instance is closed. And
it's probably a good idea to do so, just so you allow the memory
representing the instance to be garbage-collected. But there's no rule
that says you can't copy a new instance value into a variable that's
already non-null.

If you don't want to bother setting the field to null when the child
form is closed, then just use a different condition to decide when to
create a new one.

Pete
 
J

Jesse Houwing

* scottiedog wrote, On 20-1-2010 17:57:
Thanks for reply.

Yes, I would like to recreate them. But when I check for frmChild ==
null, it is not so the new frmChild doesn't get called. So with
simple this.Close() from child form doesn't get rid of the object and
yet leave the object disposed.

How can I truely get rid of it so that I can new frmChild() next time
around?

Disposing an instance does not set the instance to null. It releases all
resources used by the instance and usually leaves an object in an
unusable state (Disposed state).

Usually when the parent keeps a link to its child form, it is to bring
it to the front whenever someone tries to reopen an open form. That
doesn't seem to be the case here.

So you could just replace your code with this:

if (this.child != null)
{
this.child.MdiParent = null;
this.child.Dispose();
}

this.child = new Child();
this.child.MdiParent = this;
this.child.Show();

It makes sure that the original child form is disposed and cleaned up,
and that the reference back to the parent form is removed (sometimes not
removing these references can keep your parent form from being garbage
collected).

Then a new form is created and shown.

Alternatively, you can check the Form.Disposed property like so:

if (this.child == null || this.Child.Disposed)
{
this.child = new frmChild();
this.child.MdiParent = this;
}

this.child.Show();
this.child.Activate();

Which will create a new form when no child exists, or when the child has
been disposed.
 
S

scottiedog

This worked for me....

if (this.child != null)
{
this.child.MdiParent = null;
this.child.Dispose();
}

this.child = new Child();
this.child.MdiParent = this;
this.child.Show();

Thanks a bunch.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top