wyrd
Senior Contributor
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.
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: