Whenever I build a component or control library, I prefer a "two project" approach that involves splitting the solution into a component or control project and a corresponding test harness project, by: A mandatory value does not necessarily mean a value other than "". NET, Control To Validate is a string used to index View State for server-side control instances.
When a control's Causes Validation property is set to True, its Validating event is fired if focus shifts to another control that also has a Causes Validation value of True.One solution could be to identify and package common validation logic into reusable types. Component is the right choice if your logic doesn't require a UI, or either Control or User Control if it does.This would help reduce client code commitment to creating and configuring validation object instances as needed. The difference between Control and User Control is how the UI is rendered.If the default of False (valid field), the Validated event is fired and focus shifts to the new control. Visually speaking, this technique has two problems: The Error Provider component turns out to be a better option as a mechanism for error notification for one or more controls, using a combination of icon and tool tip to notify the user of an error and display an appropriate message in close proximity to the related control, as shown in Figure 3. Error Provider in action Enabling this component is as simple as dragging an Error Provider component onto a form and configuring its icon, blink rate, and blink style, after which the Error Provider can be incorporated into validation code: Causes Validation, Validating, and the Error Provider provide the basic infrastructure for implementing per-control validation in a pattern that we can reuse for other controls, such as txt Date Of Birth and txt Phone Number: The combination of Validating event and Error Provider component provides a great solution that dynamically validates each control at the point of impact, that is, as users enter data.Unfortunately, the reliance on Validating events prevents this solution from automatically scaling up to support form-wide validation that's needed when users click the OK button to complete data entry.It is possible that one or more controls have not had the focus before OK is clicked and, consequently, have not fired their Validating events.Form-wide validation is implemented by manually calling the validation logic tied up in each Validating event, achieved by enumerating all controls on a form, setting focus to each, and calling the form's Validate method, like so: The Cancel button, however, does not need to implement form-wide validation because its job is to simply close the form.With these goals in mind, it's time to get into the nuts and bolts.Let's start off by building the simplest possible validator we can, the Required Field Validator. NET solution in which to implement the Required Field Validator.More than just being amusing, this story is a modern-day fable from which Windows Forms developers can learn a clear lesson—always validate data to avoid dodgy situations on par with dating Steve Guttenberg.Conversely, successfully validated data provides an equivalent experience to dating Rosanna Arquette, which for this author would be quite nice.