Thorough Validation in .NET

Printer QR-Code in .NET Thorough Validation
Thorough Validation
QR Generation In Visual Studio .NET
Using Barcode maker for Visual Studio .NET Control to generate, create Quick Response Code image in .NET applications.
As useful as the Validating event is, especially when combined with the ErrorProvider component, there is one validation issue you'll have to deal with separately Because the Validating event is triggered only when focus is moved from one control to another, if a control has invalid data but never receives focus, it will never be validated For example, the form in Figure 35 has three text boxes Even if you were to handle the Validating event for all three text boxes, the user could still enter valid data into the first one (assuming it gets focus first) and press the OK button, causing the form to close and return DialogResultOK The problem is that the other two text boxes will never get focus, will never receive the Validating event, and therefore will never get a chance to cancel the acceptance of the form One way to deal with this problem is to make sure that all controls start with valid data, requiring the user to set focus to invalidate the data and triggering the validation events when focus is lost However, there are lots of cases when you can't fill in valid initial data What's a good default for a phone number Or e-mail address Or mother's maiden name For these cases, you'll need to write the code manually to validate the form in the OK button's Click event handler:
QR Code 2d Barcode Scanner In .NET Framework
Using Barcode scanner for VS .NET Control to read, scan read, scan image in .NET applications.
void okButton_Click(object sender, EventArgs e) { // Validate each direct child control manually foreach( Control control in thisControls() ) { controlFocus(); if( !thisValidate() ) { thisDialogResult = DialogResultNone; break; } } } This code walks the list of controls in the form's control collection, setting each one in turn to have the focus By itself, this will not trigger the Validating event, unfortunately, so we also must to trigger that event by calling the form's Validate method The FormValidate method does not validate all the controls in the form Instead, it validates only the control that just lost focus If validation fails on any one control, we change the form's DialogResult property (preset to DialogResultOK by the OK button's DialogResult property), and that stops the form from closing If you've got controls that contain other controls, such as controls contained by group boxes, you must be a little more thorough about checking each control for child controls to make sure that they're validated, too: // Retrieve all controls and all child controls etc // Make sure to send controls back at lowest depth first // so that most child controls are checked for things before // container controls, eg, a TextBox is checked before a // GroupBox control Control[] GetAllControls() { ArrayList allControls = new ArrayList(); Queue queue = new Queue(); queueEnqueue(thisControls); while( queueCount > 0 ) { ControlControlCollection controls = (ControlControlCollection)queueDequeue(); if( controls == null || controlsCount == 0 ) continue; foreach( Control control in controls ) { allControlsAdd(control); queueEnqueue(controlControls); } } return (Control[])allControlsToArray(typeof(Control)); } void okButton_Click(object sender, EventArgs e) { // Validate each control manually foreach( Control control in GetAllControls() ) { // Validate this control controlFocus(); if( !thisValidate() ) { thisDialogResult = DialogResultNone; break; } } }
Barcode Encoder In .NET
Using Barcode generator for VS .NET Control to generate, create barcode image in VS .NET applications.
The GetAllControls method gets all of a form's controls, as well as all the controls' child controls, all those controls' children, and so on This function is handy whenever you need to dig through the complete set of controls on a form, not just when you're validating You'll see it used a few more times in this chapter The OK or Apply button's Click event handler is also a good place to do any other kind of validation that can't be done using the validation events, such as checking multiple related fields at the same time If for any reason you don't want the form to close, set the form's DialogResult to None, and it won't [ Team LiB ]
Bar Code Recognizer In VS .NET
Using Barcode decoder for .NET Control to read, scan read, scan image in .NET applications.
[ Team LiB ]
Encode QR Code In C#
Using Barcode encoder for .NET framework Control to generate, create QR Code image in Visual Studio .NET applications.
Painting QR Code In Visual Studio .NET
Using Barcode creator for ASP.NET Control to generate, create QR image in ASP.NET applications.
Code-39 Creator In VS .NET
Using Barcode drawer for Visual Studio .NET Control to generate, create Code-39 image in .NET applications.
Encode EAN 128 In .NET Framework
Using Barcode generator for VS .NET Control to generate, create UCC - 12 image in VS .NET applications.
Printing UCC - 14 In .NET Framework
Using Barcode creator for .NET Control to generate, create DUN - 14 image in VS .NET applications.
Recognizing UPC Code In .NET Framework
Using Barcode recognizer for VS .NET Control to read, scan read, scan image in VS .NET applications.
Bar Code Drawer In VS .NET
Using Barcode maker for ASP.NET Control to generate, create bar code image in ASP.NET applications.
Bar Code Maker In Visual C#
Using Barcode printer for VS .NET Control to generate, create bar code image in .NET framework applications.
Recognizing EAN13 In VS .NET
Using Barcode scanner for VS .NET Control to read, scan read, scan image in .NET applications.