473,398 Members | 2,165 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Trapping the Form Closing Event

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
19 3194
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
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
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
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
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
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
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
<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

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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Paul Steele | last post by:
Some time ago I tracked down the code for detecting the shutdown event within a C# program. I tested it, it worked, and I moved on. However, I just discovered that the code is no longer working,...
1
by: Chirag Malvi | last post by:
hello all, I am developing the web application using ASP.net and VS.2003 IDE. here is the situation which i want to implement. 1) User is browsing some webform. I want to trap this event....
3
by: =B= | last post by:
Hi all, I was wondering if anyone has had any luck with trapping the <BODY> onUnload() event in ASP.NET? The thing is, I'm writing code for an Intranet site. The code makes a call to a...
5
by: gsb58 | last post by:
Hi! In my application I have code that will give the user a choice when he click's the close button on the ToolBar1. However, when he clicks the close button of the form, the form will shut...
4
by: RSH | last post by:
I am trying to figure out how I can trap the Closing Event that occurs when a user attempts to close the window by clicking on the red"X". I tried this code which I found online but nothing...
19
by: zacks | last post by:
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.