Detecting whether a form has been closed or not

  • Thread starter Thread starter Guest
  • Start date Start date
Xero said:
How do you detect whether a form has been closed or not?

If the form is shown modally:

\\\
Dim f As New FooForm()
f.ShowDialog()
MsgBox("The form has been closed!")
///

Otherwise:

\\\
Dim f As New FooForm()
f.Show()
..
..
..
If f.IsDisposed Then
MsgBox("The form has been closed!")
Else
MsgBox("Form still open!")
End If
///

In addition to that, you can use 'AddHandler' to add a handler to a form's
'Closed' event in order to be notified when a form closes.
 
Herfried,

A trick sample?
\\\
Dim f As New FooForm()
f.ShowDialog()
MsgBox("The form has been closed!")
///

It is right before you understand me wrong.
However I had to look twice.

:-)

Cor
 
Hi Herifried,

Thanks for replying. I found the information in my previous post wasn't
enough to tell you the entire picture ... so I think I should tell you a
little bit more about what I'm trying to do.

There are two forms in my program - namely Form 1 and Form 2. A button in
Form 1 opens Form 2, where the user is prompted to enter some information.
After the information has been entered, Form 2 is closed and the data
collected in Form 2 is used for some process in Form 1.

I'd like to know how can Form 1 detect that Form 2 has closed and it can
carry on.

Thanks again.

Xero
 
Xero said:
There are two forms in my program - namely Form 1 and Form 2. A button in
Form 1 opens Form 2, where the user is prompted to enter some information.
After the information has been entered, Form 2 is closed and the data
collected in Form 2 is used for some process in Form 1.

I'd like to know how can Form 1 detect that Form 2 has closed and it can
carry on.

\\\
Dim f As New DataEntryForm()
If f.ShowDialog() = DialogResult.OK Then
UserName = f.UserNameTextBox.Text
...
End If
f.Dispose()
///
 
I tried but the code didn't work.
I inserted a breakpoint at the line where the 'Dim' statement occured.
In the debugging mode, I highlighted the 'DialogResult' statement to check
its value. 'None' appeared in a tooltip and the program skips the If block.
It seems that the program can't receive the input. How can I fix this?

Thanks.

Xero
 
Xero,

You would have to tell if you use the showdialog or the show.

Herfried showed two samples.

When a form is showed with showdialog, than the mainform stops and wait
until the showdialog form is closed. And therefore those forms are forever
closed when they come back (not disposed)

When a form is showed with show than the mainform proceeds direct when that
is done.

You know nothing about the showed forms from inside your form where you
started the show, when you don't test that. While there is a slight time cap
between the closed state and disposed state from those forms when the user
has pushed the close button and the actions are done for that.

That can be tested with the second snippet in Herfried first answer to you.

I hope this helps?

Cor
 
Xero said:
I inserted a breakpoint at the line where the 'Dim' statement occured.
In the debugging mode, I highlighted the 'DialogResult' statement to check
its value. 'None' appeared in a tooltip and the program skips the If
block.
It seems that the program can't receive the input. How can I fix this?

Add an "OK" button to the form and assign it to the form's 'AcceptButton'
property. Alternatively you can set the form's 'DialogResult' property to
'DialogResult.OK' in a button's 'Click' event handler prior to closing the
form.
 

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

Back
Top