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
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 Wooley
Firstly, can I apologise if this has already been answered. Something odd
has happened because I sent this a couple of days ago (it is in my sent
folder) but I can't seem to see it on the newsgroup (which reminds me, I'm
using Outlook Express - is there a better newsgroup reader that others could
recommened?)
Anyway, here is my original question:
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
"VHD50" wrote:
> This is fairly simple... :)
> 1. Declare a global var and set it to some value, like
> Dim OKBtnClick As Boolean = False
> 2. In your OKButton_Click sub, set OKBtnClick = True
> 3. In the form_Closing event sub, check the value of OKBtnClick. If it's
> False then you know the user click the red x button in the corner of the form.
> There are other more complicated ways... It's up to you to decide what to
> use though :)
> Hope this helps,
> VHD50.