Q: Closing a form

  • Thread starter Thread starter G .Net
  • Start date Start date
G

G .Net

Hi

How can I tell if a form has been closed using the red cross in the top
right of the form rather than, for example, pressing an OK button?

Geoff
 
How can I tell if a form has been closed using the red cross in the
top right of the form rather than, for example, pressing an OK button?

You could have a form level variable called OkClicked. In the btnOk.Clicked
event handler, set OkClicked = True. Now in your FormClosing event handler
check your OkClicked variable and handle it accordingly. Typcially, this
is used to disallow a user to close the form witht the X and require them
to use Ok.

Incidentally, the FormClosingEventArgs includes a property for ClosingReason.
There doesn't appear to be an option to determine between closed by X or
closed by form.close and reports both as UserClosing. Below is some sample
code:

Private OkClicked As Boolean
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
OkClicked = True
Me.Close()
End Sub

Private Sub Form2_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs)
Handles Me.FormClosing
If Not OkClicked Then e.Cancel = True
End Sub

Jim Woole
 

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

Similar Threads

Q: Closing a form 2
Q: Closing a form 2
Excel VBA - Problem with Closing Print Preview 0
Q: closing forms 9
Q: closing a form 9
formclosing 6
Access How Current page of a form Read Only 2
Opening and Closing Forms 4

Back
Top