How to know a form is closed

S

SamSpade

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?
 
C

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
 
R

Richard

SamSpade said:
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
 
H

Herfried K. Wagner [MVP]

* " SamSpade said:
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/[email protected]>
 
S

SamSpade

Richard said:
" SamSpade" <[email protected]> wrote in message Nothing.

This doesn't tell you the form has been shown, it tells you the form
has been instantiated.



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
 
S

SamSpade

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
 
C

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
 
H

Herfried K. Wagner [MVP]

* " SamSpade said:
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.
 
H

Herfried K. Wagner [MVP]

* " SamSpade said:
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.
 
S

SamSpade

"> 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
 
R

Richard

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
 
S

SamSpade

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




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.
 
S

SamSpade

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
 
H

Herfried K. Wagner [MVP]

* " SamSpade said:
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'.
 
S

SamSpade

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
 
S

SamSpade

Herfried K. Wagner said:
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
 
R

Richard

SamSpade said:
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
 

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