473,416 Members | 1,566 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

Dispose Question (VB.NET 2003)

RG

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.

--
RG
Nov 23 '05 #1
6 4438
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.

"RG" <rg@medworks.com> wrote in message
news:7F**********************************@microsof t.com...

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.

--
RG

Nov 23 '05 #2

use form2.hide instead of form2.close.
"RG" wrote:
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 “.

Nov 23 '05 #3
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/de...posedtopic.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

Nov 23 '05 #4
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

RG wrote:
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.

--
RG


Nov 23 '05 #5
RG

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.

--
RG
"Brian Gideon" wrote:
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

RG wrote:
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.

--
RG


Nov 23 '05 #6

RG wrote:
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.
Thanks.

--
RG


Nov 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Teeravee Sirinapasawasdee | last post by:
Here is code from MSDN-Library April 2003 which I tried to implement to my class but there was some error that I don't know how to solve it. In Privete Overloads Sub Dispose(ByVal disposing...
11
by: Wolfgang Kaml | last post by:
Hello All, I have been working on this for almost a week now and I haven't anything up my sleeves anymore that I could test in addition or change.... Since I am not sure, if this is a Windows...
16
by: Daniel Mori | last post by:
If an object implements the IDisposable interface (regardless if its a framework object or a user object), should I always dispose of that object out of principle?
14
by: Andy | last post by:
Hi to All, I found the following issue, is this a bug? Create a new project and, in the main form, add a listbox with a few sample items. In the DoubleClick event of the listbox, add: ...
7
by: Scott M. | last post by:
In a typical class, do I need to indicate that it implements the IDisposable interface and then create a Dispose method that implements the Dispose required by the IDisposable interface or can I...
156
by: Dennis | last post by:
Ok, I'm trying to dispose of every object that I create that has a dispose method based on advice from this newsgroup. However, I'm not sure how to dispose of the following object that was created...
0
by: fish | last post by:
Hi, I have a VB.net application that will save attachments to a directory on my local pc. I need to run this component on our exchange 2003 server and also save the attachments to a local DIR. ...
9
by: Geoff Callaghan | last post by:
I have several functions that require doing a New on a local object. If the object is local to a sub or function, do I need to dispose of it, or will it just go away like any other local variable?...
5
by: Mr | last post by:
Hi We have a application with Framework 1.1 and Vs .NET 2003. the application has a service with pooling; like a File system Watcher, but works. the system works fine in the development...
1
by: Dave Harry | last post by:
I've had trouble with VB 2003 not doing things quite right, help won't compile, etc, things are being a general pain the the bum. I've downloaded the VB Express 2005 and trying my code there...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.