Call validating event c accommodating the students learning problems
Alas, users may not be able to click Cancel if the control they're currently on is invalid because focus is retained since the Cancel button's Causes Validation is also set to True by default.
This situation is easily avoided by setting the Cancel button's Causes Validation property to False, thereby preventing Validating from being fired on any controls that focus shifts from, shown in Figure 4. Preventing validation With approximately 60 lines of code, our Add New Employee form supports basic validation.
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.
Consequently, you handle Validating to implement your validation logic, such as ensuring Name is provided: Validating supplies a Cancel Event Args parameter that lets you signal whether the field was valid by setting its Cancel property.
Writing more and more code in the face of increasing UI complexity is not a scalable technique and obviously best avoided.Packaged this way, developer effort is converted to dragging a component or control from the Toolbox onto a form, configuring it from design-time features like the Property Browser, and letting the Windows Forms designer do the hard work of translating our design-time intentions into code that is persisted in Initialize Component. Consequently, Component turns out to be the most suitable choice for packaging our validation classes.Thus, a programmatic experience is transformed into a declarative experience where declarative is synonymous with productive. The next step is to work out what sort of validators we'll need. I'm a big consistency fan and also have "don't reinvent the wheel" tattooed across my forehead (backwards, of course, so I can see it in the mirror when I brush my teeth).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.