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

Setting e.Cancel outside an event Handler?

I am writing a control that relies on its host to validate the contents of
one of its fields. The control fires a custom 'FooNeedsValidating' event and
passes the field's data with the event. The host handles the event,
validates the data and returns the validation results to the control by a
callback to a control method, SetFooError(). All of that works great.

If the host returns false on the validation, I display an error glyph next
to the control field, using an error provider control. That works great,
too. But here is what I am having trouble with: I want to set the
CancelEventArgs for the field to true. I want to lock the user into the
field until the error is corrected, just as if the error had been detected
in a Validating event and I had set e.Cancel to true.

My problem is that in this case, the error is being reported in a callback,
rather than detected in an event handler. So, I've got no 'e' to hook into.

Is there any way to set the 'Cancel' property on the field outside of an
event handler? Thanks in advance.

--
David Veeneman
Foresight Systems

Mar 8 '06 #1
1 5224
I found my answer--Setting e.Cancel from outside an event handler fights the
system in a way that's totally unnecessary. The problem is with the
event --> callback approach; it's really bad design.

Instead, let the host perform the content validation inside the
NeedsValidating event handler. The host doesn't need to call a different
method to return its results; it can simply return them in a property of the
event args passed with the event..

Here is how it works: Declare a set of event args that contain two
properties; one that will pass the content to be validated by the host, and
a second property that will receive the results of the host validation.

In my user control, I created NeedsValidatingEventArgs with two properties;
a FieldToValidate string property, and an ErrorMessage string property. The
control fires the NeedsValidating event and initializes its
NeedsValidatingEventArgs as follows:

-- FieldToValidate property (read-only): Has the field contents to be
validated by the host.

-- ErrorMessage property (read/write): Has an empty string.

The host handles the event, reads the FieldToValidate from the event args,
and performs the content validation. If the validation is successful, the
event handler does nothing--it leaves the ErrorMessage property empty. If
the validation fails, the event handler sets the event args ErrorMessage
property to an appropriate error message.

When the event handler completes, control is returned to the user control,
in the line following the one that fired the NeedsValidating event. The user
control reads the ErrorMessage property. If it's empty, validation succeeds
and the user control exits the validator. If the ErrorMessage property is
not empty, the user control sets an error next to the field, using the
message passed back from the host. Then it sets e.Cancel to true, so that
the user is held in the control until the error is fixed.

Hope that's helpful to someone else somewhere down the road!

--
David Veeneman
Foresight Systems
Mar 8 '06 #2

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

Similar topics

2
by: Ron L | last post by:
I have an MDI application which opens a number of child windows, each of which could have data in a state that needs to be saved. Each child window catches its Closing event and cancels it if the...
6
by: downwitch | last post by:
OK, the last time didn't get an answer, let's see if I can explain it better. (Someone else here _must_ have done this at some point?) I have a progress-meter type thing set up to follow the...
0
by: PeacError | last post by:
Using Microsoft Visual Studio .NET 2003, Visual C# .NET 1.1: I apologize if this question has been addressed elsewhere, but I could not find a reference to it in the search engine for this...
3
by: Brian Henry | last post by:
Hi, I have a bunch of forms in a DLL that I call from my main application. The one form is a login form, and I have the tab order set in the order I want it (password box first, then ok button...
6
by: Peter M. | last post by:
Hi all, If an event has multiple subscribers, is it possible to cancel the invocation of event handlers from an event handler? Or to be more specific: I'm subscribing to the ColumnChanging...
2
by: =?Utf-8?B?Vk1J?= | last post by:
How can I stop an event handler from executing? For example, I my Gridview RowEditing event handler looks like this: protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) {...
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? ...
3
by: Smithers | last post by:
In consideration of the brief sample code at the following link... http://msdn2.microsoft.com/en-us/library/system.componentmodel.canceleventargs.cancel.aspx .... when we set e.Cancel = true,...
5
by: Steve | last post by:
In VB6 I have a screen the has a text box and a datagrid If I click the datagrid, the 1st event is to validate the textbox and then the 2nd event is a datagrid mousedown event. If a get an error...
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
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
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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.