Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Q: Closing a form

28 views
Skip to first unread message

G .Net

unread,
Mar 16, 2006, 10:53:35 AM3/16/06
to
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


Jim Wooley

unread,
Mar 16, 2006, 1:36:51 PM3/16/06
to
> 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 Wooley


G .Net

unread,
Mar 17, 2006, 1:08:12 PM3/17/06
to
Hi

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


Brian Shafer

unread,
Mar 17, 2006, 5:04:27 PM3/17/06
to
is there no .. what was in VB Classic .. QueryUnLoad?

"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.

0 new messages