capturing how a form was closed

  • Thread starter Thread starter Derrick
  • Start date Start date
D

Derrick

I have a 2 form application - the first form opens the second form, which
allows a user to create a file. The first form then needs to do something
based on how the form was closed.

how the code looks
Dim tf As New frmNewLeague
tf.ShowDialog()

If < tf closed using save button > Then
do stuff
else
do other stuff
End If

Is using a global variable the best way, or can you cature how a form is
closed?

Derruck
 
Derrick said:
I have a 2 form application - the first form opens the second form, which
allows a user to create a file. The first form then needs to do something
based on how the form was closed.

how the code looks
Dim tf As New frmNewLeague
tf.ShowDialog()

If < tf closed using save button > Then
do stuff
else
do other stuff
End If

Is using a global variable the best way, or can you cature how a form is
closed?

You use DialogResult for that, buttons have a DialogResult property which
you can set to make them close and send a result to ShowDialog();

For example, if you set a button with an "OK" dialog result, and another one
with a "Cancel" one, you would:

(i'm a C# writer, i'll try my best at writing VB.Net code, but I've never
done this before)

Dim tf As New frmNewLeague
Dim mr As DialogResult

mr = tf.ShowDialog()
if mr = DialogResult.OK then
' do whatever for ok
elseif mr = DialogResult.Cancel then
' do whatever for cancel
else ' the form was closed with the 'x' button
' do whatever
end if

There are other dialog results, like "Yes", "No", "Abort", etc.

Take into account, that if the button has a DialogResult property associated
(not 'none'), it'll automatically close the form (or, technically called
'end the dialog'), and you don't have to manually call 'Close()' on the
form.

Hope this helps, and hope the above code is right (lemme repeat: i've never
coded any vb.net, nor any vb since version 2 for the matter ;D)

Javier Campos
 
* "Derrick said:
I have a 2 form application - the first form opens the second form, which
allows a user to create a file. The first form then needs to do something
based on how the form was closed.

how the code looks
Dim tf As New frmNewLeague
tf.ShowDialog()

If < tf closed using save button > Then
do stuff
else
do other stuff
End If

Is using a global variable the best way, or can you cature how a form is
closed?

Set the form's 'DialogResult' property and use something like this:

\\\
Select Case tf.DhowDialog()
Case DialogResult.OK
...
Case DialogResult.Cancel
...
...
End Select
///
 
Hi Derrick,

You may set the DialogResult property of the Button in the PropertyGrid in
the designer view and then use the suggestions from our community members.

If you still have problem on this issue, please feel free to reply in this
thread.


Best regards,

Ying-Shen Yu [MSFT]
Microsoft community Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, please remove the word "online"
before sending mail.
 
Thanks so much for all (Javier Campos, Herfried K. Wagner and Ying-Shen)
your help again - that worked perfectly.

Derrick
 
Back
Top