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(B yVal sender As System.Object, ByVal e As
System.EventArg s) 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.ObjectDi sposedException 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 6 4454
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.co m> wrote in message
news:7F******** *************** ***********@mic rosoft.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(B yVal sender As System.Object, ByVal e As System.EventArg s) 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.ObjectDi sposedException 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
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.ObjectDi sposedException in system.windows. forms.dll. Can’t access disposed object named Form2 “.
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
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.Componen tModel.CancelEv entArgs) _
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(B yVal sender As System.Object, ByVal e As System.EventArg s) 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.ObjectDi sposedException 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
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(B yVal sender As System.Object, ByVal e As
System.EventArg s) 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.Componen tModel.CancelEv entArgs) _ 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(B yVal sender As System.Object, ByVal e As System.EventArg s) 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.ObjectDi sposedException 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
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(B yVal sender As System.Object, ByVal e As System.EventArg s) 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.EventArg s) 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 indiscriminatel y.
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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 Boolean), the code call component.Dispose() method but the VS said that 'System.ComponentModel.Component.Protected Overrridable Sub Dispose(disposing As Boolean)' is not accessible in this context because it is 'Protected'. I don't know this code is from...
|
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 2003 Server or ADO or ODBC issue,
I am posting this on all of the three newsgroups.
That's the setup:
Windows 2003 Server with IIS and ASP.NET actiavted
Access 2002 mdb file (and yes, proper rights are set on TMP paths and path,
|
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?
|
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:
Me.Dispose()
Then run the project, double click on an item in the listbox to close the
|
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 just make a Sub Dispose() and
the CLR will know that this is the Dispose method to call when the object
falls out of scope?
Thanks.
| |
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 inside a method call.
dim myvar as new object1
object1.dosomethingmethod(new object2)
Note that object 2 has a dispose method but how do I dispose of it unless I
do the following:
|
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. When the
component is run on the exchange 2003 server a wizard gui is
automatically started which wants to install Outlook 2003. The
component is using mapi to access the default inbox.
What do i need to do to convert this component to run on...
|
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?
Geoff Callaghan
|
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 enviroment with
a performance very good; but production not.
-------------------------------------------------------------------------------------------
|
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 before buying
VB 2005 standard.
Should the forms' code, that was upgraded from VB6 to 2003, still work
properly in 2005?
The program function was expanded so much after upgrading to 2003 that it
would be rather a nightmare to do it again from 6 ->...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |