473,574 Members | 3,249 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3214
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(Tr ue)
end sub

and in the tab changed

sub TabChanged
ValidateTabs(fa lse)
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***@construc tion-imaging.comwrot e in message
news:11******** *************@q 75g2000hsh.goog legroups.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...@the nbs.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(Tr ue)
end sub

and in the tab changed

sub TabChanged
ValidateTabs(fa lse)
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***@construc tion-imaging.comwrot e in message
news:11******** *************@q 75g2000hsh.goog legroups.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.Permi ssions.Reflecti onPermission(Se curity.Permissi ons.SecurityAct ion.Demand,Unre stricted:=True) _ Function GetCloseReason( ) As CloseReason Dim t As Type = GetType(Form) Dim pi As Reflection.Prop ertyInfo = t.GetProperty(" CloseReason",Re flection.Bindin gFlags.GetPrope rty Or Reflection.Bind ingFlags.Instan ce OrReflection.Bi ndingFlags.NonP ublic) Return CType(pi.GetVal ue(Me, Nothing), CloseReason) End Function"Bill McCarthy" <Bi**@NOSPAM.co mwrote in messagenews:uN* *************@T K2MSFTNGP04.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***@con struction-imaging.comwrot e in messagenews:11* *************** *****@q75g2000h sh.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>FormClos ing 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.Permi ssions.Reflecti onPermission(Se curity.Permissi ons.SecurityAct ion.Demand,Unre stricted:=True) _ Function GetCloseReason( ) As CloseReason Dim t As Type = GetType(Form) Dim pi As Reflection.Prop ertyInfo = t.GetProperty(" CloseReason",Re flection.Bindin gFlags.GetPrope rty Or Reflection.Bind ingFlags.Instan ce OrReflection.Bi ndingFlags.NonP ublic) Return CType(pi.GetVal ue(Me, Nothing), CloseReason) End FunctionRegards ,Bill."Bill McCarthy" <Bi**@NOSPAM.co mwrote in messagenews:uD* *************@T K2MSFTNGP05.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***@construc tion-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.Permi ssions.Reflecti onPermission(Se curity.Permissi ons.SecurityAct ion.Demand,
Unrestricted := True) _
Function GetCloseReason( ) As CloseReason
Dim t As Type = GetType(Form)
Dim pi As Reflection.Prop ertyInfo = t.GetProperty(" CloseReason",
Reflection.Bind ingFlags.GetPro perty Or Reflection.Bind ingFlags.Instan ce Or
Reflection.Bind ingFlags.NonPub lic)
Return CType(pi.GetVal ue(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.ToInt3 2 = 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">T he source of the event.</param>
''' <param name="e">The <see cref="T:System. EventArgs" /instance
containing the event data.</param>
Private Sub btnExit_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) 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***@construc tion-imaging.comschr eef in bericht
news:11******** *************@q 75g2000hsh.goog legroups.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2308
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, and I'm baffled. I've done some more Google searches and have come up with the same thing. A portion of the code is below. I call the...
1
2236
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. this i am doing with Session_Onstart. 2) Now when user is exiting i.e. closing the browser window i want to trap this event and perform some...
3
5867
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 third-party component (think of it as a lock) when the page loads. Now the OK and Cancel server buttons I can handle and use to release the lock as...
5
2117
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 down even if he chooses the cancel button of the messagebox that pop's up. Now...if he, on the other hand, chooses ALT+F4, then I catch this in
4
6437
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 happens. What do I need to do to trap and give the ability to save changes before the window closes? Thanks, Ron
19
2043
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 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...
0
7841
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7760
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8101
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8271
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8137
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5335
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3793
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2273
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1099
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.