473,327 Members | 1,896 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,327 software developers and data experts.

Disposing of Windows Forms

I have a project that has 5 or 6 forms.
VB.NET VS 2005
In the FormClosing Event of each form I have the following code:

If Not IsNothing(frmA) Or Not frmA.IsDisposed Then frmA.Close()

If Not IsNothing(frmB) Or Not frmB.IsDisposed Then frmB.Close()

If Not IsNothing(frmC) Or Not frmC.IsDisposed Then frmC.Close()

If Not IsNothing(frmD) Or Not frmD.IsDisposed Then frmD.Close()

Me.Dispose()

In Form B I have to remove the: If Not IsNothing(frmB) Or Not
frmB.IsDisposed Then frmB.Close()

In Form C I have to remove the: If Not IsNothing(frmC) Or Not
frmC.IsDisposed Then frmC.Close()

etc.... Is there a way to iterate through all the forms in the FormClosing
event to make sure everything is closed before ending the application?

Thanks
Nov 2 '06 #1
3 1650
What are you trying to accomplish?

Are you calling these in the respective forms, i.e. you're doing "If Not
IsNothing(frmA)..." in frmA, and "If Not IsNothing(frmB)..." in frmB and so
on, just to close the forms? Because you don't need to.

Presumably if you are *in* the FormClosing event, someone has chosen to
close the form, and it is being closed. At that point, asking to close it
would be repetitive.

Unlike older versions of VB, you no longer have to set the form to Nothing
in order to let the system know it is no longer needed. VB2005 marks it as
"unused" when there are no longer any references to it, and the
GarbageCollector gets rid of it on its next sweep through. This happens to
most objects, be it a form or a variable or a class object, whenever it goes
out of scope and there are no references to it.

Hope that helps.
Robin

"Henry Jones" <he***@yada.comwrote in message
news:eg****************@TK2MSFTNGP02.phx.gbl...
>I have a project that has 5 or 6 forms.
VB.NET VS 2005
In the FormClosing Event of each form I have the following code:

If Not IsNothing(frmA) Or Not frmA.IsDisposed Then frmA.Close()

If Not IsNothing(frmB) Or Not frmB.IsDisposed Then frmB.Close()

If Not IsNothing(frmC) Or Not frmC.IsDisposed Then frmC.Close()

If Not IsNothing(frmD) Or Not frmD.IsDisposed Then frmD.Close()

Me.Dispose()

In Form B I have to remove the: If Not IsNothing(frmB) Or Not
frmB.IsDisposed Then frmB.Close()

In Form C I have to remove the: If Not IsNothing(frmC) Or Not
frmC.IsDisposed Then frmC.Close()

etc.... Is there a way to iterate through all the forms in the
FormClosing event to make sure everything is closed before ending the
application?

Thanks


Nov 2 '06 #2
Yes, I am calling these respective forms in each form. In FormA I am
calling the FormB, FormC, FormD statements
In FormB I am Calling A,C,D, etc... So what you are saying is that I don't
need to call these when I am closing down the Program?

All that work for nothing :-)
"RobinS" <Ro****@NoSpam.yah.nonewrote in message
news:2a******************************@comcast.com. ..
What are you trying to accomplish?

Are you calling these in the respective forms, i.e. you're doing "If Not
IsNothing(frmA)..." in frmA, and "If Not IsNothing(frmB)..." in frmB and
so on, just to close the forms? Because you don't need to.

Presumably if you are *in* the FormClosing event, someone has chosen to
close the form, and it is being closed. At that point, asking to close it
would be repetitive.

Unlike older versions of VB, you no longer have to set the form to Nothing
in order to let the system know it is no longer needed. VB2005 marks it as
"unused" when there are no longer any references to it, and the
GarbageCollector gets rid of it on its next sweep through. This happens to
most objects, be it a form or a variable or a class object, whenever it
goes out of scope and there are no references to it.

Hope that helps.
Robin

"Henry Jones" <he***@yada.comwrote in message
news:eg****************@TK2MSFTNGP02.phx.gbl...
>>I have a project that has 5 or 6 forms.
VB.NET VS 2005
In the FormClosing Event of each form I have the following code:

If Not IsNothing(frmA) Or Not frmA.IsDisposed Then frmA.Close()

If Not IsNothing(frmB) Or Not frmB.IsDisposed Then frmB.Close()

If Not IsNothing(frmC) Or Not frmC.IsDisposed Then frmC.Close()

If Not IsNothing(frmD) Or Not frmD.IsDisposed Then frmD.Close()

Me.Dispose()

In Form B I have to remove the: If Not IsNothing(frmB) Or Not
frmB.IsDisposed Then frmB.Close()

In Form C I have to remove the: If Not IsNothing(frmC) Or Not
frmC.IsDisposed Then frmC.Close()

etc.... Is there a way to iterate through all the forms in the
FormClosing event to make sure everything is closed before ending the
application?

Thanks



Nov 3 '06 #3
No effort is useless if you learn something from it. (Now lift your feet,
it's getting deep in here.)

Generally, you might have a main form, and have it close everything before
it exits, just to make sure you didn't leave anything open. But you
shouldn't close each form in its own close event.

I have a main form (MainForm - how imaginative), and I might check and close
all other forms like the following. This is assuming you don't want to
finish closing the main form until all the other ones are closed.

For Each myForm as Form in My.Application.OpenForms
'MainForm is closing, so don't fire the close event again
if myForm.Name <"MainForm" Then
myForm.Close
End If
Next
'do other stuff before finally letting MainForm close

If your app starts up with a Sub Main(), you would put the following code
there. If you wanted to put it in every form, or you didn't care what order
they were closed, you could also do the following, but put it in the
FormClosed event rather than FormClosing, though, because FormClosed fires
AFTER the form is closed.

For Each myForm as Form in My.Application.OpenForms
myForm.Close
Next

By the way, if those are child forms of an MDI parent, the parent closes
them when the parent is closed.

Hope that gives you some closure. (Sorry; couldn't resist.)
Robin

"Henry Jones" <he********@yada.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
Yes, I am calling these respective forms in each form. In FormA I am
calling the FormB, FormC, FormD statements
In FormB I am Calling A,C,D, etc... So what you are saying is that I
don't need to call these when I am closing down the Program?

All that work for nothing :-)
"RobinS" <Ro****@NoSpam.yah.nonewrote in message
news:2a******************************@comcast.com. ..
>What are you trying to accomplish?

Are you calling these in the respective forms, i.e. you're doing "If Not
IsNothing(frmA)..." in frmA, and "If Not IsNothing(frmB)..." in frmB and
so on, just to close the forms? Because you don't need to.

Presumably if you are *in* the FormClosing event, someone has chosen to
close the form, and it is being closed. At that point, asking to close it
would be repetitive.

Unlike older versions of VB, you no longer have to set the form to
Nothing in order to let the system know it is no longer needed. VB2005
marks it as "unused" when there are no longer any references to it, and
the GarbageCollector gets rid of it on its next sweep through. This
happens to most objects, be it a form or a variable or a class object,
whenever it goes out of scope and there are no references to it.

Hope that helps.
Robin

"Henry Jones" <he***@yada.comwrote in message
news:eg****************@TK2MSFTNGP02.phx.gbl...
>>>I have a project that has 5 or 6 forms.
VB.NET VS 2005
In the FormClosing Event of each form I have the following code:

If Not IsNothing(frmA) Or Not frmA.IsDisposed Then frmA.Close()

If Not IsNothing(frmB) Or Not frmB.IsDisposed Then frmB.Close()

If Not IsNothing(frmC) Or Not frmC.IsDisposed Then frmC.Close()

If Not IsNothing(frmD) Or Not frmD.IsDisposed Then frmD.Close()

Me.Dispose()

In Form B I have to remove the: If Not IsNothing(frmB) Or Not
frmB.IsDisposed Then frmB.Close()

In Form C I have to remove the: If Not IsNothing(frmC) Or Not
frmC.IsDisposed Then frmC.Close()

etc.... Is there a way to iterate through all the forms in the
FormClosing event to make sure everything is closed before ending the
application?

Thanks




Nov 3 '06 #4

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

Similar topics

0
by: TP-Software | last post by:
Hi, I have a ChatFormCollection class which holds a collection of the class ChatForm which inherits from Form. MainForm has an instance of ChatFormCollection. When a ChatForm instance is...
0
by: Carl Mercier | last post by:
Hi, I'm trying to close and dispose a form that is running in another process, but I get the error message "A first chance exception of type 'System.ObjectDisposedException' occurred in...
10
by: Patrick De Ridder | last post by:
I have been looking at an example, and there is something I don't inderstand. Given: form1 calls form2 --------- Question: What is the use of having these lines in form2 --------------...
5
by: Mathias L. | last post by:
I have two questions for which I couldnt find answer: If I programaticaly close DialogForm (calling Close()), is it enough or do I have to dispose it as MS.NET help says? Also, in overriden...
3
by: Paul Aspinall | last post by:
Hi I have an MDI app.... All child MDI windows are non-updateable... however, the users may leave the windows open... and there can be several of them. I want to implement a system to dispose...
4
by: Dakkar | last post by:
I have a program with windows forms and after execution of my program im making it invisible for working background progress and i have a dispose function like this protected override void...
2
by: Dave | last post by:
I'm having trouble understanding dispose. I set up a class that, among other things, displays the time in a status bar panel. It does this by starting a thread. I create an instance of this...
5
by: Chris | last post by:
I have a form that requires drawing custom lines on it. The color of the lines is suppose to be the same as the forcolor of the form. Am I doing this the most efficent and correct way? ...
29
by: Jerry Spence1 | last post by:
I'm rather confused as to whether something should be disposed of, or not. What is the general rule? How can you be sure of doing the right thing? I've heard about disposing unmanaged resources but...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.