I’ve been deeply involved with a .Net 2 WinForms application recently and needed to do some user input validation. The framework now offers a very nice ErrorProvider which allows you to associate a control to it and display a visual error icon. I’ve had the situation where I needed to have a group of Controls validated for input before a user could continue. I’ve build a Validation Class which uses the ErrorProvider and maintains a Generic List of controls that have failed validation.
The validation class allows you to add a textbox to the validation list as simply as
Validation validation = new Validation(this);
validation.TextBoxValid(this.textBox1, "Please enter text");
This will validate the control and then display the error provider with the passed in text if validation fails.
I’ve put to together a simple example which displays a simple textbox validation scenario. At the moment the validation class just deals with empty TextBoxes but other control validations could be easily added such as Date of Birth checks, Post Codes (Zip for the US readers) etc etc.
The example can be downloaded from here.
I’d be interested to hear how people have handled their validation issues in WinForms .Net 2.