TextBox with Validation Functions and MaskEdit






4.07/5 (14 votes)
Jan 26, 2003
2 min read

94084

479
CustomControl that inherits from TextBox class and adds input validation functions, required validation functions and MaskEdit mode.
Introduction
This is my first custom control, which one I decide to do to learn about this
topic and to make me easy to work with forms validation data. This control
inherits all attributes and methods from the TextBox
class and adds
some input and required validations.
Using the code
ValidText
as any other component, can be added to the IDE
ToolBox to put graphically into windows forms and modify theirs properties from
the properties window in the Validation Category, or can be used like reference
using their methods and modify their attributes directly from the code,
generating a instance of its class.
The properties of ValidText
are:
ValitationMode (ValidationTypes)
: Sets the validation mode to use for the text or data field. This validation mode ca be:None
,VaidChars
,InvalidChars
,Letters
,Numbers
andMaskEdit
.MaskString (String)
: Sets the allowed mask for the input data whenValidationMode
=MaskEdit
. Character “#” suggest a digit, “A” suggest upper letter, “z” suggest lower letter, any other characters will be seen as a fixed character in the mask. For example: If theValidationMode
property =MaskEdit
and y MaskString = ###- (AAA) - ##/aa/AA, user can be able to write data of 345 - ( ABC ) - 22/ad/CD types in the text control.ValidString (String)
: Sets the valid and not valid characters allowed whenValidationMode
=ValidChars
orInvalidChars
.ShowErrorIcon (Boolean)
: Sets if the icon and tooltip of theErrorProvider
is showed in the validation of the data field.Required (Boolean)
: Sets if the data field must have at last one character to be validated.Reference (String)
: Sets the link between the text control and the data field to get. This property is used to make the validation error messages.
The unique method or function of ValidText
is:
Validate
: This method or function returns a Boolean
value to set if data field approve the required validation. I recommend
to use this function in the Click
Event
of the button
or the control that process of the data. For example:
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs)
Handles Button1.Click
If Me.ValidText1.Validate = False Then
MsgBox("The field " & Me.ValidText1.Reference & " is
required!", MsgBoxStyle.Critical, Me.Text)
Me.ValidText1.Focus()
Exit Sub
End If
If Me.ValidText2.Validate = False Then
MsgBox("The field " & Me.ValidText2.Reference & " is
required!", MsgBoxStyle.Critical, Me.Text)
Me.ValidText2.Focus()
Future Plans
In the future I am thinking to add custom error messages, more options
in MaskEdit
, other ValidationModes
like email, IP
address, Dates, etc. Please send me feedback, comments, and suggest to: ra_luis@yahoo.com