Dispose Question (VB.NET 2003)

G

Guest

Hello – I’m relatively new to VB and I’m not getting how to do Dispose
correctly from my readings.

I have an application with 3 Forms (with a lot of logic going on within
each): Form1, Form2, and a 3rd Form which is a dialog.

- Form 1 at the top has:

Dim frmTwo As New Form2

(and upon a button click, shows Form2:)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

frmTwo.Show()

- Form 2, upon a button click, shows a Dialog with ShowDialog. I can show
and close and re-show that Dialog all I want. After the ShowDialog statement
I have a Dispose() statement for the Dialog form.

Here’s the problem: if I close Form2, and then later I try to Show Form2
again (by clicking the button on Form1) I get the error:

“unhandled exception System.ObjectDisposedException in
system.windows.forms.dll.
Can’t access disposed object named Form2 “.

I’ve tried putting Dispose & Finalize in various places, but I need some
advice here.

Thanks so much.
 
B

Bob Powell [MVP]

If you dispose of an object then you cannot use it again. If you want to
re-show your form after hiding it then you must not dispose of it.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
C

Cor Ligthert [MVP]

RG,

The dispose is probably one of the most confusing methods. A lot of people
want to see it as the finalize method. What it is not. In fact almost every
cleanup is done by the GC with sadly enough exceptinons. (as example, window
handles, streamreaders and open files).

Moreover is the disposing done by the implementation of Idisposable that is
in every designed form and designed component.

If you close a form, it will be disposed. There is however a short while
when that is not done. If you need to trap that short while there is the
instruction IsDisposed, which is not in intelisense.

http://msdn.microsoft.com/library/d...emwindowsformscontrolclassisdisposedtopic.asp

Be aware that one of the exceptions is a form showed with ShowDialog, where
the internal dialog is used. Those you need to close with myform.dispose.

I hope this helps,

Cor
 
B

Brian Gideon

RG,

Closing a form disposes it (unless it was shown as a dialog). You
either need to create a new instance of Form2 each time you show it or
hide it instead of closing it. You can hide it by handling the Closing
event.

Private Sub Form2_Closing( _
ByVal sender As Object, _
ByVal e As System.ComponentModel.CancelEventArgs) _
Handles MyBase.Closing

e.Cancel = True
Me.Hide()

End Sub

Brian
 
G

Guest

Dear Brian,
Thanks for your excellent suggestion to hide the form by handling the
Closing event as you suggested. It worked well.

If I may, I’d like to follow-up with another question:

You also mentioned the other possible solution of creating a new instance of
Form2 each time I show it. That actually may be a better solution for my
particular application, but I don’t understand how that would work:

As I said, I currently have this:

- Form 1 at the top has:
Dim frmTwo As New Form2

(and upon a button click, shows Form2:)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

frmTwo.Show()

How do I implement your idea? Create a whole bunch of Form2 instances like
this:
Dim frmTwo2 As New Form2
Dim frmTwo3 As New Form2
Dim frmTwo4 As New Form2 ... etc. and then somehow keep track of which have
been already Shown & Closed/Disposed and then use the next one up?

That seems hard to imagine for me. My users might be on my application all
day long, and click for Form2 100 times, or who knows how many times before
closing the application.

There’s a lot of logic I use to construct how Form2 looks, based on user
selections done in Form1 (combo-boxes, etc). Right now this is mostly done
in the Load Event for Form2. I noticed that on implementing your solution
you gave me (hide & re-show the one instance), upon re-showing the form2 no
Event seems to get invoked at all (at least not Load or GetFocus, I noticed).
So I would need to invoke all that logic in Form1 right before re-showing
Form2. I can live with that I guess, but perhaps your other idea is better
(creating a new instance of Form2 each time I show it.)

Thanks.
 
B

Brian Gideon

RG said:
Dear Brian,
Thanks for your excellent suggestion to hide the form by handling the
Closing event as you suggested. It worked well.

If I may, I'd like to follow-up with another question:

You also mentioned the other possible solution of creating a new instance of
Form2 each time I show it. That actually may be a better solution for my
particular application, but I don't understand how that would work:

As I said, I currently have this:

- Form 1 at the top has:
Dim frmTwo As New Form2

(and upon a button click, shows Form2:)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

frmTwo.Show()

How do I implement your idea? Create a whole bunch of Form2 instances like
this:
Dim frmTwo2 As New Form2
Dim frmTwo3 As New Form2
Dim frmTwo4 As New Form2 ... etc. and then somehow keep track of which have
been already Shown & Closed/Disposed and then use the next one up?

That seems hard to imagine for me. My users might be on my application all
day long, and click for Form2 100 times, or who knows how many times before
closing the application.

Do it like the following.

Private frmTwo as Form2

Private Sub Button1_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click

frmTwo = New Form2
frmTwo.Show()

End Sub

Of course, you'll need to put logic in there somewhere to prevent the
user from creating a whole bunch of Form2s indiscriminately.
There's a lot of logic I use to construct how Form2 looks, based on user
selections done in Form1 (combo-boxes, etc). Right now this is mostly done
in the Load Event for Form2. I noticed that on implementing your solution
you gave me (hide & re-show the one instance), upon re-showing the form2 no
Event seems to get invoked at all (at least not Load or GetFocus, I noticed).
So I would need to invoke all that logic in Form1 right before re-showing
Form2. I can live with that I guess, but perhaps your other idea is better
(creating a new instance of Form2 each time I show it.)

Yes, it sounds like creating a new form would work better in your
particular case.
 

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