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

Setting e.Cancel outside an event Handler?

P: n/a
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
Share this Question
Share on Google+
1 Reply

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.