views:

98

answers:

1

I'm trying to implement this pattern in my WinForms application (I don't like it, but it was required by the customer):

  • I edit the properties of an object in a DetailsControl (UserControl developed by us), and when the user tries to leave the control, then it's validated and saved
  • If it's not valid, or an error occurs during save, then the control must not be left (it's in a List/Detail section)

My idea was this:

  • on Validating, I check if my object is fine, otherwise I cancel the event
  • on Validated, I save my object, and if an error occurs, I cancel the event

Unfortunately, Validated is not cancelable, and Leave is fired before Validating/Validated. Is there a cancelable event after Validated, that would prevent losing the focus?

If not, I will move all my logic inside Validating, but I would like to keep the formal validation separated from the save errors.

+1  A: 

This article lists the usual order of events when a Validated/Validating event is raised. There are no cancelable events after Validating. I think that without some extra magic, you're out of luck.

If you're really concerned that people using the class might do other validation checks of their own in Validating event handlers, you could extend the class with another custom, cancelabe validating event. You could then raise this custom event during OnValidating before calling base.OnValidating.

Eric
thanks, I couldn't find that article...my concern was only about separating validation from saving my object, but it's not a big issue, we're the only ones using our control
Filini