473,396 Members | 1,938 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,396 software developers and data experts.

CancelEventArgs.Cancel - How Event Raising Code Know if Client Set .Cancel=true

In consideration of the brief sample code at the following link...

http://msdn2.microsoft.com/en-us/lib...gs.cancel.aspx

.... when we set e.Cancel = true, How does the form subsequently know to NOT
close the form? More generally, after an event is raised, does the event
raising class somehow retain a reference to the CancelEventArgs instance,
and then check the value of the .Cancel property to subsequently know if
further processing (e.g., actually close the form) is to be aborted? Is it
that simple?

The upshot here is that I'm looking into providing a cancellable events -
and I was wondering how I might make use of CancelEventArgs.Cancel. My plan
is to derive a new "event args" class from CancelEventArgs, and let the
client set the .Cancel property - just like we can set that property for the
FormClosing event.
Thanks!
Sep 23 '07 #1
3 7638
Smithers wrote:
In consideration of the brief sample code at the following link...

http://msdn2.microsoft.com/en-us/lib...gs.cancel.aspx

.... when we set e.Cancel = true, How does the form subsequently know to NOT
close the form? More generally, after an event is raised, does the event
raising class somehow retain a reference to the CancelEventArgs instance,
and then check the value of the .Cancel property to subsequently know if
further processing (e.g., actually close the form) is to be aborted? Is it
that simple?
Yes, I believe it is. Note that there's no magic to "somehow retaining
a reference". The code that instantiates the CancelEventArgs instance
is likely the same code that calls OnFormClosing to raise the event. So
all it would have to do is wait for the call to OnFormClosing to return
and then check the flag in the CancelEventArgs instance. The instance
is probably just referenced by a local variable, passed to OnFormClosing.
The upshot here is that I'm looking into providing a cancellable events -
and I was wondering how I might make use of CancelEventArgs.Cancel. My plan
is to derive a new "event args" class from CancelEventArgs, and let the
client set the .Cancel property - just like we can set that property for the
FormClosing event.
Sounds like a fine plan to me. Whatever code is actually instantiating
your args class would check the flag after calling the method that
raises the event, and the flag will be set if any of the code dealing
with the event sets it.

Pete
Sep 23 '07 #2
Very good then. Tomorrow (well, later today, anyway), I'll write a small
sample and report back here on how it went.

-S

"Peter Duniho" <Np*********@NnOwSlPiAnMk.comwrote in message
news:13************@corp.supernews.com...
Smithers wrote:
>In consideration of the brief sample code at the following link...

http://msdn2.microsoft.com/en-us/lib...gs.cancel.aspx

.... when we set e.Cancel = true, How does the form subsequently know to
NOT close the form? More generally, after an event is raised, does the
event raising class somehow retain a reference to the CancelEventArgs
instance, and then check the value of the .Cancel property to
subsequently know if further processing (e.g., actually close the form)
is to be aborted? Is it that simple?

Yes, I believe it is. Note that there's no magic to "somehow retaining a
reference". The code that instantiates the CancelEventArgs instance is
likely the same code that calls OnFormClosing to raise the event. So all
it would have to do is wait for the call to OnFormClosing to return and
then check the flag in the CancelEventArgs instance. The instance is
probably just referenced by a local variable, passed to OnFormClosing.
>The upshot here is that I'm looking into providing a cancellable events -
and I was wondering how I might make use of CancelEventArgs.Cancel. My
plan is to derive a new "event args" class from CancelEventArgs, and let
the client set the .Cancel property - just like we can set that property
for the FormClosing event.

Sounds like a fine plan to me. Whatever code is actually instantiating
your args class would check the flag after calling the method that raises
the event, and the flag will be set if any of the code dealing with the
event sets it.

Pete

Sep 23 '07 #3
<snip>
>>Is it that simple?
>Yes, I believe it is. Note that there's no magic to "somehow retaining a
reference".

Confirmed. Without thinking it through very well it just seemed like
something special might be going on, what with the delegate sitting between
the event publisher and subscribers --- you know what we hear, "the event
handler hides the subscribers from the publisher". That, plus I had some
presumption - don't ask why - that the event handler delegates were invoking
their referenced event handling methods asynchronously. Testing today
confirmed they aren't (news to me!). But I doubt even that would make a
difference at the end of the day (in terms of the publisher being able to
see 'e' (eventargs instance). That _would_ however make a difference in
terms of the timing involved in the publisher giving any/all subscribers a
chance to set the value of .Cancel, with the subscriber needing block until
any/all subscribers returned. But no concern as the calls are synchronous...

-S
Sep 24 '07 #4

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

Similar topics

1
by: WindAndWaves | last post by:
Goodmorning Gurus Here is another of my questions..... When adding code to a control then access sometimes puts in a couple of 'parameters'. Can anyone tell me what the use of them is. I have...
6
by: Arno R | last post by:
Hi all I come across a 'feature' of Access (2000) that I don't like and would like to disable. I am checking if a value already exists in a table. I a subform I use code like : Private Sub...
7
by: MLH | last post by:
I tried the following code to prevent a checkbox from being updated (going from a value of Null to True, from True to False or from False to True). I was surprised it did not work. Can anyone...
4
by: Gates72 | last post by:
Hello All, I am looking for an example showing a custom EventArgs class that derives from CancelEventsArgs, and a form or control using that class in a custom event. The simpler, the better,...
3
by: Oenone | last post by:
I'm writing an application with various MDI child forms. In the Closing event of many of the forms, I have code that asks the user whether he is sure he wants to close the form, because it...
2
by: Bob | last post by:
In a winform with a datagridview using cellvalidating event but also have a save button that is located on a tablebindignnavigator. The behaviour I observe is that if the cellvalidating issues a...
1
by: tony | last post by:
Hello!! I have an application that consist of several windows forms. Lets call these A,B and C for simplicity. I have one main meny where the user can choose window form A or B or C. When a...
2
by: Steve | last post by:
After running into a design wall with my traditional thread approach, I realized that a BackgroundWorker object would fit my needs pretty good. Never really used them before. In a nutshell, my...
4
by: ncarnal | last post by:
I have two events on a control that are wired up to other controls. Both events fire upon clicking a menu item, how do I cancel the second event if I return e.Cancel = True from the first event? ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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...
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
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...
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,...

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.