By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,294 Members | 2,517 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,294 IT Pros & Developers. It's quick & easy.

How to know a form is closed

P: n/a
I declare a variable X of type myForm then instantiate myForm saving a
reference in the variable X, and then do X.Show.

If I need to know if the form has been shown yet, I check X against Nothing.

If the users closes MyForm I'd like X to be set to Nothing but don't know
how to sense (except in MyForm) that myForm is closing.

In the myForm's Closing event if I set Me=Nothing will that set X to
Nothing?

How about doing it in the Closed event?
Nov 20 '05 #1
Share this Question
Share on Google+
21 Replies


P: n/a
Cor
Hi Sam,

Are you using VB.net

There it is not normal to set your forms to nothing.
VB.net is managed code, setting to nothing is one of the things that is
normaly managed for you.

Cor
Nov 20 '05 #2

P: n/a
" SamSpade" <st**************@REMOVEaol.com> wrote in message news:<#t**************@TK2MSFTNGP11.phx.gbl>...
I declare a variable X of type myForm then instantiate myForm saving a
reference in the variable X, and then do X.Show.

If I need to know if the form has been shown yet, I check X against Nothing.
This doesn't tell you the form has been shown, it tells you the form
has been instantiated.

If the users closes MyForm I'd like X to be set to Nothing but don't know
how to sense (except in MyForm) that myForm is closing.

In the myForm's Closing event if I set Me=Nothing will that set X to
Nothing?


You dont have to do this if the form is a regular form. As regular
form this is done automatically. The Closing event occurs as the form
is being closed. When a form is closed, all resources created within
the object are released and the form is disposed.

If you create a form as a dialog form i.e .ShowDialog rather than
..Show then you should check out the DialogResult property and note
that the above rule does not apply. Resources are not automaticlly
released for dialog forms.

Either way read the docs about the OnClosing, OnClosed and
DialogResult events and properties.
Hth

Richard
Nov 20 '05 #3

P: n/a
* " SamSpade" <st**************@REMOVEaol.com> scripsit:
I declare a variable X of type myForm then instantiate myForm saving a
reference in the variable X, and then do X.Show.

If I need to know if the form has been shown yet, I check X against Nothing.

If the users closes MyForm I'd like X to be set to Nothing but don't know
how to sense (except in MyForm) that myForm is closing.

In the myForm's Closing event if I set Me=Nothing will that set X to
Nothing?

How about doing it in the Closed event?


<http://groups.google.de/groups?selm=eZIYbXOEDHA.2892%40TK2MSFTNGP11.phx.gb l>

--
Herfried K. Wagner [MVP]
<http://dotnet.mvps.org/>
Website Address Changed!
Nov 20 '05 #4

P: n/a
That's the question. Does it set the variable or if not, how can I.

Thanks

"Cor" <no*@non.com> wrote in message
news:OB**************@tk2msftngp13.phx.gbl...
Hi Sam,

Are you using VB.net

There it is not normal to set your forms to nothing.
VB.net is managed code, setting to nothing is one of the things that is
normaly managed for you.

Cor

Nov 20 '05 #5

P: n/a

"Richard" <go****@intermension.com> wrote in message
news:ee**************************@posting.google.c om...
" SamSpade" <st**************@REMOVEaol.com> wrote in message news:<#t**************@TK2MSFTNGP11.phx.gbl>...
I declare a variable X of type myForm then instantiate myForm saving a
reference in the variable X, and then do X.Show.

If I need to know if the form has been shown yet, I check X against Nothing.
This doesn't tell you the form has been shown, it tells you the form
has been instantiated.

If the users closes MyForm I'd like X to be set to Nothing but don't

know how to sense (except in MyForm) that myForm is closing.

In the myForm's Closing event if I set Me=Nothing will that set X to
Nothing?


When a form is closed, all resources created within
the object are released and the form is disposed.


Maybe I'm missing something simple, but the question relates to a variable
in the parent form.
Thanks
Nov 20 '05 #6

P: n/a
It's not a Mdi child. Have to try and see if it still applies.

Even if it doesn't apply here I need the approach elsewhere. Very pleased to
receive it.

Thanks a lot
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:e1**************@TK2MSFTNGP10.phx.gbl...
* " SamSpade" <st**************@REMOVEaol.com> scripsit:
I declare a variable X of type myForm then instantiate myForm saving a
reference in the variable X, and then do X.Show.

If I need to know if the form has been shown yet, I check X against Nothing.
If the users closes MyForm I'd like X to be set to Nothing but don't know how to sense (except in MyForm) that myForm is closing.

In the myForm's Closing event if I set Me=Nothing will that set X to
Nothing?

How about doing it in the Closed event?

<http://groups.google.de/groups?selm=...TNGP11.phx.gbl

--
Herfried K. Wagner [MVP]
<http://dotnet.mvps.org/>
Website Address Changed!

Nov 20 '05 #7

P: n/a
* " SamSpade" <st**************@REMOVEaol.com> scripsit:
That's the question. Does it set the variable or if not, how can I.


It's more important that the form instance is /disposed/. Setting the
variable to 'Nothing' doesn't make much sense if it's a local variable.
When going out of scope, the variable will be set to 'Nothing'
automatically and when assigning another reference to the instance
variable, the old reference will be removed.

--
Herfried K. Wagner [MVP]
<http://dotnet.mvps.org/>
Nov 20 '05 #8

P: n/a
Cor
Hi Sam.

The user is not closing a form he only pushes a button or something else.

You are closing the form.

If you want to know if a form is closing you can use the close event or the
closing event.

I hope this helps, and if not, ask again

Cor
Nov 20 '05 #9

P: n/a
* " SamSpade" <st**************@REMOVEaol.com> scripsit:
It's not a Mdi child. Have to try and see if it still applies.
It will still work.
Even if it doesn't apply here I need the approach elsewhere. Very pleased to
receive it.


If you have any further questions on the solution, feel free to post
them here.

--
Herfried K. Wagner [MVP]
<http://dotnet.mvps.org/>
Nov 20 '05 #10

P: n/a
"> If you have any further questions on the solution, feel free
to post them here. Herfried K. Wagner [MVP]

Thanks for making me comfortable pursuing this.

I evently didn't make my question clear.

In a form I do:

Public frmClipboard As ClipboardForm

...snip

frmClipboard = New ClipboardForm

frmClipboard.Show()

Later I need to know if the ClipboardForm form is still showing or if the
user has closed it.

If it's still showing I'll do something to it.

If not I'll repeat the above 2 steps and then do something to it.

To see if the ClipboardForm is still showing can I check gfrmClipboard to
see if it is Nothing ?

Yow see, I'm hoping that the form closing will somehow cause gfrmClipboard
to be set to Nothing even thought they are not even in the same class.

Maybe in the ClipboardForm Closing event I need to do Me.Dispose (or the
Closed event)?

Thanks

Nov 20 '05 #11

P: n/a

I hope this helps, and if not, ask again

Cor


Thanks
Please see my last post to Herfried K. Wagner
Nov 20 '05 #12

P: n/a
> >
When a form is closed, all resources created within
the object are released and the form is disposed.


Maybe I'm missing something simple, but the question relates to a variable
in the parent form.
Thanks


Yep i think you are. If the varaible in the parent form reference is a
form type. And if a form type is automatically disposed of when you
close it, if it was shown with .Show.

Then once the form is closed the variable in the parent form that
references the child form will return true if you test for

myformvar.IsDisposed.

Like i say just read the docs, if you have them, on those subject
areas and it will become clear. If you dont have the docs i think you
can get them at MSDN, sorry but i dont have a Url for them handy.

Hth

Richard
Nov 20 '05 #13

P: n/a
I need to tell something else
frmClipboard
is a global variable defined in a module
"> If you have any further questions on the solution, feel free
to post them here.
Herfried K. Wagner [MVP]

Thanks for making me comfortable pursuing this.

I evently didn't make my question clear.

In a form I do:

Public frmClipboard As ClipboardForm 'Global variable

..snip

frmClipboard = New ClipboardForm

frmClipboard.Show()

Later I need to know if the ClipboardForm form is still showing or if the
user has closed it.

If it's still showing I'll do something to it.

If not I'll repeat the above 2 steps and then do something to it.

To see if the ClipboardForm is still showing can I check gfrmClipboard to
see if it is Nothing ?

You see, I'm hoping that the form closing will somehow cause

gfrmClipboard to be set to Nothing even thought they are not even in the same class.

Maybe in the ClipboardForm Closing event I need to do Me.Dispose (or the
Closed event)?

Thanks


I'm now experimenting but am leery of generalizing from one test case, but
the best I can tell is the gfrmClipboard is not set to Nothing and contains
an invalid reference after the form is closed.

I could use a method to sense the closing form posted by Herfried K. Wagner
in reply to something else but hope that I could do something in the
gfrmClipboard form when it closes.
Nov 20 '05 #14

P: n/a

myformvar.IsDisposed.

I think this is what I'm looking for. I was thinking I might be able to
check for Nothing but looks like IsDisposed is what I needed to know about.

Thanks
Nov 20 '05 #15

P: n/a
* " SamSpade" <st**************@REMOVEaol.com> scripsit:
Thanks for making me comfortable pursuing this.

I evently didn't make my question clear.

In a form I do:

Public frmClipboard As ClipboardForm

..snip

frmClipboard = New ClipboardForm

frmClipboard.Show()

Later I need to know if the ClipboardForm form is still showing or if the
user has closed it.

If it's still showing I'll do something to it.

If not I'll repeat the above 2 steps and then do something to it.

To see if the ClipboardForm is still showing can I check gfrmClipboard to
see if it is Nothing ?

Yow see, I'm hoping that the form closing will somehow cause gfrmClipboard
to be set to Nothing even thought they are not even in the same class.

Maybe in the ClipboardForm Closing event I need to do Me.Dispose (or the
Closed event)?


You will have to set the variable to 'Nothing' yourself. To do that,
you can add a handler to the form's 'Closed' event handler using
'AddHandler' (sample provided previously). In the event handler, place
the code to set the variable to 'Nothing'.

--
Herfried K. Wagner [MVP]
<http://dotnet.mvps.org/>
Nov 20 '05 #16

P: n/a
This is what I came up with
Nice and simple and clean

Thanks
If f Is Nothing Then

f = New Form2

ElseIf f.IsDisposed Then

f = New Form2

End If
Nov 20 '05 #17

P: n/a

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:uo**************@TK2MSFTNGP09.phx.gbl...
* " SamSpade" <st**************@REMOVEaol.com> scripsit:
Thanks for making me comfortable pursuing this.

I evently didn't make my question clear.

In a form I do:

Public frmClipboard As ClipboardForm

..snip

frmClipboard = New ClipboardForm

frmClipboard.Show()

Later I need to know if the ClipboardForm form is still showing or if the user has closed it.

If it's still showing I'll do something to it.

If not I'll repeat the above 2 steps and then do something to it.

To see if the ClipboardForm is still showing can I check gfrmClipboard to see if it is Nothing ?

Yow see, I'm hoping that the form closing will somehow cause gfrmClipboard to be set to Nothing even thought they are not even in the same class.

Maybe in the ClipboardForm Closing event I need to do Me.Dispose (or the Closed event)?


You will have to set the variable to 'Nothing' yourself. To do that,
you can add a handler to the form's 'Closed' event handler using
'AddHandler' (sample provided previously). In the event handler, place
the code to set the variable to 'Nothing'.

Thanks I'm playing with that. Also looking at using IsDisposed

Thanks
Nov 20 '05 #18

P: n/a
" SamSpade" <st**************@REMOVEaol.com> wrote in message news:<ua**************@TK2MSFTNGP12.phx.gbl>...
This is what I came up with
Nice and simple and clean

Thanks
If f Is Nothing Then

f = New Form2

ElseIf f.IsDisposed Then

f = New Form2

End If


Awesome! Although you could just just 1 or 3 lines.

If (f Is Nothing) OrElse (f.IsDisposed) Then f = New Form2

hth
Richard
Nov 20 '05 #19

P: n/a
I was afraid that it would evaluate (f.IsDisposed) when f was Nothing.

Thanks

"Richard" <go****@intermension.com> wrote in message
news:ee**************************@posting.google.c om...
" SamSpade" <st**************@REMOVEaol.com> wrote in message

news:<ua**************@TK2MSFTNGP12.phx.gbl>...
This is what I came up with
Nice and simple and clean

Thanks
If f Is Nothing Then

f = New Form2

ElseIf f.IsDisposed Then

f = New Form2

End If


Awesome! Although you could just just 1 or 3 lines.

If (f Is Nothing) OrElse (f.IsDisposed) Then f = New Form2

hth
Richard

Nov 20 '05 #20

P: n/a
" SamSpade" <st**************@REMOVEaol.com> wrote in message news:<O9*************@TK2MSFTNGP12.phx.gbl>...
I was afraid that it would evaluate (f.IsDisposed) when f was Nothing.

Thanks


No "OrElse" as well as "AndAlso" are whats called short circuiting
conditionals.
If the first evaluates to true the second is not evaluated - They can
be very handy of each of the conditions requires a bit of grunt to run
it - say if you were calling functions that returned Bools.

I use them almost every time i use i datareader:

If dr is Nothing Orlelse not (Dr.Read) then return nothing

Man i cant tell you how often ive written that line of code....
hundreds...
"Or" as well as "And" would evaluate the way you described.

Hth
Richard
Nov 20 '05 #21

P: n/a
thanks

"Richard" <go****@intermension.com> wrote in message
news:ee**************************@posting.google.c om...
" SamSpade" <st**************@REMOVEaol.com> wrote in message

news:<O9*************@TK2MSFTNGP12.phx.gbl>...
I was afraid that it would evaluate (f.IsDisposed) when f was Nothing.

Thanks


No "OrElse" as well as "AndAlso" are whats called short circuiting
conditionals.
If the first evaluates to true the second is not evaluated - They can
be very handy of each of the conditions requires a bit of grunt to run
it - say if you were calling functions that returned Bools.

I use them almost every time i use i datareader:

If dr is Nothing Orlelse not (Dr.Read) then return nothing

Man i cant tell you how often ive written that line of code....
hundreds...
"Or" as well as "And" would evaluate the way you described.

Hth
Richard

Nov 20 '05 #22

This discussion thread is closed

Replies have been disabled for this discussion.