Trapping the Form Closing Event

Z

zacks

I have a .NET 2.0 MDI application where the child form has a Tab
Control. Each of the Tab in the Tab Control has a Validating event to
handle what it should do when the user changes tabs.

But these Validating Events are also fired when either the child form
or the main (parent) form Close icon is clicked. And I need for these
events to know if they are being invoked because the app (or child
window) is being closed. I have set a boolean in BOTH of the form's
FormClosing event, but it seems that the Validating event is firing
BEFORE the FormClosing event!!! ARGGGHHHH!!!

How can I tell in a Tab Validating event that it is being fired due to
the form or app being closed?
 
A

andrew.ames

Am i right in thinking that the Validate method is being called on
both the TabChange event and the FormClosing event?

If so, why not include a ByVal in the Validate method something like
isClosing as a boolean

so in form closing it would be

sub FormClosing
ValidateTabs(True)
end sub

and in the tab changed

sub TabChanged
ValidateTabs(false)
end sub


then in the validateTabs sub you just need an if statement to check
the value of isclosing to see if it was called when the form was
closing.

Sorry if I have the totally wrong end of the stick here
 
B

Bill McCarthy

Hi,

I don't believe you can. Instead you'll have to think about changing the
logic of what you are trying to do.
The problem is the closing is invoked after the validation occurs because
the validation occurs when the control, say a textbox, looses focus. That
lost focus occurs before the sys_cmd or any other way of closing the
application occurs other than your own code to force it to close. You might
be able to trap the lost focus, and see what gets focus and determine if the
user pressed the close button, but I don't think you'd have any luck if they
bring up the system menu by clicking on the icon. So all in all, I'd say
the answer is no, you'd be better off looking at a different program logic .

Regards,

Bill.
 
Z

zacks

Am i right in thinking that the Validate method is being called on
both the TabChange event and the FormClosing event?

If so, why not include a ByVal in the Validate method something like
isClosing as a boolean

so in form closing it would be

sub FormClosing
ValidateTabs(True)
end sub

and in the tab changed

sub TabChanged
ValidateTabs(false)
end sub

then in the validateTabs sub you just need an if statement to check
the value of isclosing to see if it was called when the form was
closing.

Sorry if I have the totally wrong end of the stick here

Thanks for your reply. I am not manually calling the tab validating
event in the form closing event, it is automatically being fired. I am
setting a boolean in the child form in the form closing event so the
validating event can know that is was fired because of the form
closing, but since the validating event code runs BEFORE the form
closing event, setting that boolean there is useless. That is my
problem.
 
J

Jay Balapa

Zack,

The way we do it is to create a boolean variable for tracking whether the
user pressed OK or Cancel.

If he pressed Cancel we set the bool_calncelled=true and we check for this
bool in form_closed event.
 
B

Bill McCarthy

Actually you might be able to check what the CloseReason property is.
Problem is it's private by default, so add this method and call it in your
validating. IF the valeu is None, then validate, otherwise close was called.


<Security.Permissions.ReflectionPermission(Security.Permissions.SecurityAction.Demand,Unrestricted:=True)> _ Function GetCloseReason() As CloseReason Dim t As Type = GetType(Form) Dim pi As Reflection.PropertyInfo = t.GetProperty("CloseReason",Reflection.BindingFlags.GetProperty Or Reflection.BindingFlags.Instance OrReflection.BindingFlags.NonPublic) Return CType(pi.GetValue(Me, Nothing), CloseReason) End Function"Bill McCarthy" <[email protected]> wrote in messageHi,>> I don't believe you can. Instead you'll have to think about changing thelogic of what you are trying to do.> The problem is the closing is invoked after the validation occurs becausethe validation occurs when the control, say a textbox, looses focus. Thatlost focus occurs before the sys_cmd or any other way of closing theapplication occurs other than your own code to force it to close. You mightbe able to trap the lost focus, and see what gets focus and determine if theuser pressed the close button, but I don't think you'd have any luck if theybring up the system menu by clicking on the icon. So all in all, I'd saythe answer is no, you'd be better off looking at a different program logic .>> Regards,>> Bill.>>>>> <[email protected]> wrote in messagehave a .NET 2.0 MDI application where the child form has a Tab>> Control. Each of the Tab in the Tab Control has a Validating event to>> handle what it should do when the user changes tabs.>>>> But these Validating Events are also fired when either the child form>> or the main (parent) form Close icon is clicked. And I need for these>> events to know if they are being invoked because the app (or child>> window) is being closed. I have set a boolean in BOTH of the form's>> FormClosing event, but it seems that the Validating event is firing>> BEFORE the FormClosing event!!! ARGGGHHHH!!!>>>> How can I tell in a Tab Validating event that it is being fired due to>> the form or app being closed?>>>
 
B

Bill McCarthy

I'll try that again ...

<Security.Permissions.ReflectionPermission(Security.Permissions.SecurityAction.Demand,Unrestricted:=True)> _ Function GetCloseReason() As CloseReason Dim t As Type = GetType(Form) Dim pi As Reflection.PropertyInfo = t.GetProperty("CloseReason",Reflection.BindingFlags.GetProperty Or Reflection.BindingFlags.Instance OrReflection.BindingFlags.NonPublic) Return CType(pi.GetValue(Me, Nothing), CloseReason) End FunctionRegards,Bill."Bill McCarthy" <[email protected]> wrote in messageActually you might be able to check what the CloseReason property is.Problem is it's private by default, so add this method and call it in yourvalidating. IF the valeu is None, then validate, otherwise close was called.>
 
G

Guest

zack,

Have you tried setting e.Cancel = False in the form's Closing event?

Kerry Moorman
 
B

Bill McCarthy

<Security.Permissions.ReflectionPermission(Security.Permissions.SecurityAction.Demand,
Unrestricted := True) > _
Function GetCloseReason() As CloseReason
Dim t As Type = GetType(Form)
Dim pi As Reflection.PropertyInfo = t.GetProperty("CloseReason",
Reflection.BindingFlags.GetProperty Or Reflection.BindingFlags.Instance Or
Reflection.BindingFlags.NonPublic)
Return CType(pi.GetValue(Me, Nothing), CloseReason)
End Function
 
M

Michel Posseth [MCP]

Hello


I see you got already a lot of response for your question , however nobody
mentions the solution that i am using , so i thought i share it with you



see below copy paste code wich also traps the X botton :


Private Const SC_CLOSE As Integer = 61536
Private Const WM_SYSCOMMAND As Integer = 274
Private ValidateFields As Boolean = True
''' <summary>
''' WNDs the proc.
''' </summary>
''' <param name="m">The m.</param>
Protected Overloads Overrides Sub WndProc(ByRef m As Message)
If m.Msg = WM_SYSCOMMAND AndAlso m.WParam.ToInt32 = SC_CLOSE Then
ValidateFields = False
End If
MyBase.WndProc(m) 'doorsturen naar de base class
End Sub
''' <summary>
''' Handles the Click event of the btnExit control.
''' </summary>
''' <param name="sender">The source of the event.</param>
''' <param name="e">The <see cref="T:System.EventArgs" /> instance
containing the event data.</param>
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnExit.Click
ValidateFields = False
Me.Close()
End Sub

in my validating routines i just check the ValidateFields boolean if it is
false i just jump out of the routine cause i know the form received a close
command
 
Z

zacks

Zack,

The way we do it is to create a boolean variable for tracking whether the
user pressed OK or Cancel.

If he pressed Cancel we set the bool_calncelled=true and we check for this
bool in form_closed event.

Thanks for your reply. Unfortunately, the user is not clicking on any
OK or Cancel command button. They are clicking on the Form Close Icon
button, the X button. Either the main form X button or the child form
X button.
 
Z

zacks

zack,

Have you tried setting e.Cancel = False in the form's Closing event?

Kerry Moorman

Thanks for your reply. I do not want to cancel the action of the X
button, just handle it when it happens.
 
Z

zacks

<Security.Permissions.ReflectionPermission(Security.Permissions.SecurityAct­ion.Demand,
Unrestricted := True) > _
Function GetCloseReason() As CloseReason
Dim t As Type = GetType(Form)
Dim pi As Reflection.PropertyInfo = t.GetProperty("CloseReason",
Reflection.BindingFlags.GetProperty Or Reflection.BindingFlags.Instance Or
Reflection.BindingFlags.NonPublic)
Return CType(pi.GetValue(Me, Nothing), CloseReason)
End Function

The CloseReason property is useful for something, but not this. It
only becomes available when the Form Closing event handler fires. And
that is the crux of the problem, that event handler does not fire
until AFTER the Validating event handler for the current control that
has focus.
 
Z

zacks

Hello

I see you got already a lot of response for your question , however nobody
mentions the solution that i am using , so i thought i share it with you

see below copy paste code wich also traps the X botton :

Thanks for your reply. This solution might work, but just where do I
find the code that traps the X button? It is not a named control on my
form so I do not know how to establish an OnClick event handler for
it.

If I knew where the OnClick event handler for the X button was, the
solution would be simple.
 
B

Bill McCarthy

HI Zacks,

Did you try the code ? It works here.

Regards,

Bill.


<Security.Permissions.ReflectionPermission(Security.Permissions.SecurityAct­ion.Demand,
Unrestricted := True) > _
Function GetCloseReason() As CloseReason
Dim t As Type = GetType(Form)
Dim pi As Reflection.PropertyInfo = t.GetProperty("CloseReason",
Reflection.BindingFlags.GetProperty Or Reflection.BindingFlags.Instance Or
Reflection.BindingFlags.NonPublic)
Return CType(pi.GetValue(Me, Nothing), CloseReason)
End Function

The CloseReason property is useful for something, but not this. It
only becomes available when the Form Closing event handler fires. And
that is the crux of the problem, that event handler does not fire
until AFTER the Validating event handler for the current control that
has focus.
 
M

Michel Posseth [MCP]

If I knew where the OnClick event handler for the X button was, the
solution would be simple.

it isn`t availlable , you can only trap it while subclassing your form


an the example of that is already there in the code i showed

Private Const SC_CLOSE As Integer = 61536
Private Const WM_SYSCOMMAND As Integer = 274
Private ValidateFields As Boolean = True

'this method will be receive all windows messages that are send to your
form
Protected Overloads Overrides Sub WndProc(ByRef m As Message)
If m.Msg = WM_SYSCOMMAND AndAlso m.WParam.ToInt32 = SC_CLOSE Then
'if the user presses the X then we set the boolean
ValidateFields = False
End If
MyBase.WndProc(m) 'let the window message pass to the base form
End Sub


i hope with above remarks you understand a bit more what the code does

Regards

Michel Posseth [MCP]
 
M

Michel Posseth [MCP]

Hi ,

Forgot to mention the code will receive the message of pressing the X
button before the validate events are fired

HTH

Michel


Michel Posseth said:
If I knew where the OnClick event handler for the X button was, the
solution would be simple.

it isn`t availlable , you can only trap it while subclassing your form


an the example of that is already there in the code i showed

Private Const SC_CLOSE As Integer = 61536
Private Const WM_SYSCOMMAND As Integer = 274
Private ValidateFields As Boolean = True

'this method will be receive all windows messages that are send to your
form
Protected Overloads Overrides Sub WndProc(ByRef m As Message)
If m.Msg = WM_SYSCOMMAND AndAlso m.WParam.ToInt32 = SC_CLOSE Then
'if the user presses the X then we set the boolean
ValidateFields = False
End If
MyBase.WndProc(m) 'let the window message pass to the base form
End Sub


i hope with above remarks you understand a bit more what the code does

Regards

Michel Posseth [MCP]



Thanks for your reply. This solution might work, but just where do I
find the code that traps the X button? It is not a named control on my
form so I do not know how to establish an OnClick event handler for
it.

If I knew where the OnClick event handler for the X button was, the
solution would be simple.
 
G

Guest

zacks,

Setting e.Cancel = False in the form's Closing event does not cancel the
action of the X button, it keeps the validating events from preventing the
form from closing.

Kerry Moorman
 
J

Jack Jackson

This is what I do, I couldn't find any other way. You don't need any
handler for the form close button.

In your validation event handler, check ValidateFields, and if False
don't do the validation, say that it is OK.

By default ValidateFields is True. You set it False for those
situations in which you don't want to do validation, such as when you
receive the closing message in WndProc.
 
Z

zacks

it isn`t availlable , you can only trap it while subclassing your form

an the example of that is already there in the code i showed

Sorry, I am a little dense sometimes. :)

I understand what you are saying now and this solution works fine.
Since I wanted to trap the closing event in both the child form and
the parent form, I had to trap windows messages in both classes. I
made the validating boolean a property in the child form and set it in
the message trap in the parent form.
Private Const SC_CLOSE As Integer = 61536
Private Const WM_SYSCOMMAND As Integer = 274
Private ValidateFields As Boolean = True

'this method will be receive all windows messages that are send to your
form
Protected Overloads Overrides Sub WndProc(ByRef m As Message)
If m.Msg = WM_SYSCOMMAND AndAlso m.WParam.ToInt32 = SC_CLOSE Then
'if the user presses the X then we set the boolean
ValidateFields = False
End If
MyBase.WndProc(m) 'let the window message pass to the base form
End Sub

i hope with above remarks you understand a bit more what the code does

Regards

Michel Posseth [MCP]

<[email protected]> schreef in bericht

Thanks for your reply. This solution might work, but just where do I
find the code that traps the X button? It is not a named control on my
form so I do not know how to establish an OnClick event handler for
it.
If I knew where the OnClick event handler for the X button was, the
solution would be simple.

- Show quoted text -
 

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

Top