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