Custom TextBox Validator

wyrd

Senior Contributor
Joined
Aug 23, 2002
Messages
1,405
Location
California
While building my first GUI application I came to realize that just about all of my text boxes validate the text and clean it up in some way or another (need to insure proper values for a db you know). So I figured that I might as well extend the TextBox and make my own validation control so I don't have to worry about messing around with code in the future.

Anyway, if someone else finds this handy, by all means use it or change it to your needs. This is in no way a "professional" control and anyone with half a brain could easily make this, but I figured what the heck, no harm in throwing it up on these forums "just in case."

If you find a bug, please let me know. I only did minimal testing on it (to make sure it fit my needs without caughing up strange errors).

This is what the control does;
It checks to make sure the user types in a valid value based on the constraints you give it (through properties). If the user does not type in a proper value, an error (set by you) will pop up asking the user if they'd like to fix the value or just continue on (in which case the default value will automatically be placed in the text box). On top of this it also cleans up what the user typed in (if the properties are set to true) by doing a Text.Trim() and by capitalizing the first char in the Text.

Here are the added properties;
- Trim (true/false); Trims Text.
- Capitalize (true/false); Capitalizes first char in Text.
- RegexPattern (pattern); Matches Text to a regular expression (won't match any if left blank)
- ValidType (Type); Matches Text to the Type specified (Type is an enumerator which includes String plus all value types).
- DefaultText (text); Default text to use if user doesn't fix an error.
- ErrorMessage (text); Error message to display if a user enters something invalid.

To use the control, right click on your Toolbox, click Customize Toolbox, click .NET Framework Components tab, click Browse, then find the GenericControls.dll and then click Open, then OK. You can then use the control from your Toolbox like any other control.

Feel free to toy around with it. Maybe you'll find it of some use and will save you minor amounts of time *shrug*

EDIT:
Oh yeah, and the control is called TextBoxValidator.
 

Attachments

Last edited:
Yeah okay, changed a few things to make it a little bit more flexible and a tad more professional (if that's even possible, heh);

- No longer uses a message box (crappy idea to begin with). This was replaced with two things...
1) To see if the control is valid, it now provides a readonly property IsValid.
2) If you want to display an error message you must provide an ErrorProvider (there's a property for this) along with the error message to display in the ErrorProvider. Otherwise, no errors are shown and it's up to you to display an error elsewhere basedon the IsValid property.
- The default value now replaces an empty value if the user leaves one.
- Clean up code moved from OnLeave to OnValidating, and also the control will no longer cancel the Validating event even if there's an error (there's no reason to force the user back to the control now that it uses the ErrorProvided)

Also added the following;
- A public Validate() method so you can Validate() the control whenever you wish.
- A public readonly IsValid property to see if the current state of the control is valid or not.

WARNING:
The control defaults to IsValid = true. At least until I can figure out the following questions;

Couple of questions to help me improve this further;
- How can I validate the initial text set in the base.Text property? That way I can appropriately set the initial IsValid property.
- How can I store the initial text in the base.Text property as the default value?
 

Attachments

Back
Top