By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,827 Members | 2,155 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,827 IT Pros & Developers. It's quick & easy.

Trapping the Form Closing Event

P: n/a
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?

Aug 8 '07 #1
Share this Question
Share on Google+
19 Replies


P: n/a
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

Aug 8 '07 #2

P: n/a
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.


<za***@construction-imaging.comwrote in message
news:11*********************@q75g2000hsh.googlegro ups.com...
>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?
Aug 8 '07 #3

P: n/a
On Aug 8, 11:33 am, andrew.a...@thenbs.com wrote:
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.

Aug 8 '07 #4

P: n/a
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.

--
Jay Balapa
http://jbalapa.blogspot.com

<za***@construction-imaging.comwrote in message
news:11*********************@q75g2000hsh.googlegro ups.com...
>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?
Aug 8 '07 #5

P: n/a
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(Securit y.Permissions.SecurityAction.Demand,Unrestricted:= True)_ Function GetCloseReason() As CloseReason Dim t As Type = GetType(Form) Dim pi As Reflection.PropertyInfo = t.GetProperty("CloseReason",Reflection.BindingFlag s.GetProperty Or Reflection.BindingFlags.Instance OrReflection.BindingFlags.NonPublic) Return CType(pi.GetValue(Me, Nothing), CloseReason) End Function"Bill McCarthy" <Bi**@NOSPAM.comwrote in messagenews:uN**************@TK2MSFTNGP04.phx.gbl. ..Hi,>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.>>>><za***@construction-imaging.comwrote in messagenews:11*********************@q75g2000hsh.go oglegroups.com...>>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?>>>
Aug 8 '07 #6

P: n/a
I'll try that again ...

<Security.Permissions.ReflectionPermission(Securit y.Permissions.SecurityAction.Demand,Unrestricted:= True)_ Function GetCloseReason() As CloseReason Dim t As Type = GetType(Form) Dim pi As Reflection.PropertyInfo = t.GetProperty("CloseReason",Reflection.BindingFlag s.GetProperty Or Reflection.BindingFlags.Instance OrReflection.BindingFlags.NonPublic) Return CType(pi.GetValue(Me, Nothing), CloseReason) End FunctionRegards,Bill."Bill McCarthy" <Bi**@NOSPAM.comwrote in messagenews:uD**************@TK2MSFTNGP05.phx.gbl. ..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 yourvalidating. IF the valeu is None, then validate, otherwise close was called.>
Aug 8 '07 #7

P: n/a
zack,

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

Kerry Moorman
"za***@construction-imaging.com" wrote:
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?

Aug 8 '07 #8

P: n/a
<Security.Permissions.ReflectionPermission(Securit y.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

Aug 8 '07 #9

P: n/a

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


<za***@construction-imaging.comschreef in bericht
news:11*********************@q75g2000hsh.googlegro ups.com...
>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?

Aug 8 '07 #10

P: n/a
On Aug 8, 12:23 pm, "Jay Balapa" <jbal...@hotmail.comwrote:
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.
>
--
Jay Balapahttp://jbalapa.blogspot.com

<za...@construction-imaging.comwrote in message

news:11*********************@q75g2000hsh.googlegro ups.com...
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?- Hide quoted text -

- Show quoted text -

Aug 8 '07 #11

P: n/a
On Aug 8, 12:36 pm, Kerry Moorman
<KerryMoor...@discussions.microsoft.comwrote:
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.
>
"za...@construction-imaging.com" wrote:
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?- Hide quoted text -

- Show quoted text -

Aug 8 '07 #12

P: n/a
On Aug 8, 12:45 pm, "Bill McCarthy" <B...@NOSPAM.comwrote:
<Security.Permissions.ReflectionPermission(Securit y.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.

Aug 8 '07 #13

P: n/a
On Aug 8, 1:33 pm, "Michel Posseth [MCP]" <msn...@posseth.comwrote:
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.
>
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

<za...@construction-imaging.comschreef in berichtnews:11*********************@q75g2000hsh.go oglegroups.com...
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?- Hide quoted text -

- Show quoted text -

Aug 8 '07 #14

P: n/a
HI Zacks,

Did you try the code ? It works here.

Regards,

Bill.
<za***@construction-imaging.comwrote in message
news:11**********************@d55g2000hsg.googlegr oups.com...
On Aug 8, 12:45 pm, "Bill McCarthy" <B...@NOSPAM.comwrote:
<Security.Permissions.ReflectionPermission(Securit y.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.

Aug 8 '07 #15

P: n/a
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]

<za***@construction-imaging.comschreef in bericht
news:11**********************@g4g2000hsf.googlegro ups.com...
On Aug 8, 1:33 pm, "Michel Posseth [MCP]" <msn...@posseth.comwrote:
>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.
>>
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

<za...@construction-imaging.comschreef in
berichtnews:11*********************@q75g2000hsh.g ooglegroups.com...
>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?- Hide quoted text -

- Show quoted text -


Aug 8 '07 #16

P: n/a
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 [MCP]" <MS**@posseth.comschreef in bericht
news:Ob**************@TK2MSFTNGP05.phx.gbl...
>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]

<za***@construction-imaging.comschreef in bericht
news:11**********************@g4g2000hsf.googlegro ups.com...
>On Aug 8, 1:33 pm, "Michel Posseth [MCP]" <msn...@posseth.comwrote:
>>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.
>>>
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

<za...@construction-imaging.comschreef in
berichtnews:11*********************@q75g2000hsh. googlegroups.com...

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?- Hide quoted text -

- Show quoted text -



Aug 8 '07 #17

P: n/a
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
"za***@construction-imaging.com" wrote:
On Aug 8, 12:36 pm, Kerry Moorman
<KerryMoor...@discussions.microsoft.comwrote:
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.

"za...@construction-imaging.com" wrote:
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?- Hide quoted text -
- Show quoted text -


Aug 8 '07 #18

P: n/a
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.

On Wed, 08 Aug 2007 11:01:02 -0700, za***@construction-imaging.com
wrote:
>On Aug 8, 1:33 pm, "Michel Posseth [MCP]" <msn...@posseth.comwrote:
>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.
>>
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

<za...@construction-imaging.comschreef in berichtnews:11*********************@q75g2000hsh.go oglegroups.com...
>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?- Hide quoted text -

- Show quoted text -
Aug 8 '07 #19

P: n/a
On Aug 8, 2:12 pm, "Michel Posseth [MCP]" <M...@posseth.comwrote:
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
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]

<za...@construction-imaging.comschreef in berichtnews:11**********************@g4g2000hsf.go oglegroups.com...
On Aug 8, 1:33 pm, "Michel Posseth [MCP]" <msn...@posseth.comwrote:
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.
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
<za...@construction-imaging.comschreef in
berichtnews:11*********************@q75g2000hsh.go oglegroups.com...
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?- Hide quoted text -
- Show quoted text -- Hide quoted text -

- Show quoted text -

Aug 8 '07 #20

This discussion thread is closed

Replies have been disabled for this discussion.