Introduction
This is my first custom control, which I decided to do, to learn about this topic and to make it 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. The validation options are:
- None.
- Valid chars validation.
- Invalid chars validation.
- Letters validation.
- Numbers validation.
- MaskEdit.
- Regular expression validation (E-mail, IP, URL, ZIP, Fecha and Custom).
Using the code
ValidText
as any other .NET component, can be added to the IDE ToolBox to be put graphically into Windows forms and modify their properties from the properties window in the Validation Category. It can also be used like reference, using its methods and modifying its attributes directly from the code, after generating an instance of its class.
Properties
ValitationMode (ValidationTypes)
: Sets the validation mode to use for the text or data field. This validation mode can be: None
, VaidChars
, InvalidChars
, Letters
, Numbers
, MaskEdit
and RegEX
.
MaskString (String)
: Sets the allowed mask for the input data when ValidationMode = MaskEdit
. Character #
suggests a digit, A
suggests upper letter, z
suggests lower letter, $
suggests an upper letter, lower letter, or number. Any other character will be seen as a fixed character in the mask. For example: If the ValidationMode
property = MaskEdit
and MaskString = ###- (AAA) - ##/aa/AA/&&/$$$
, user can be able to write data of 345 - ( ABC ) - 22/ad/CD/Ad/s3G
types in the text control.
ValidString (String)
: Sets the valid and not valid characters allowed when ValidationMode
= ValidChars
, InvalidChars
or RegEx
.
ShowErrorIcon (Boolean)
: Sets if the icon and tool tip of the ErrorProvider
is shown 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.
RegExMode (RegExTypes)
: Sets the regular expression mode to use for the text or data field. This regular expression mode can be: Custom
, Email
, URL
, IP
, Date
or ZIP
.
MessageLanguage (MessageLanguages)
: Sets error icon messages language. (English
, Espa�ol
)
Methods
requiredFieldsCheck
: This share method of the ValidText
class returns true
if the form you send as parameter has at last one ValidText
control with Required
property = TRUE
and Text
property = �� Private Sub btnAceptar_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnAceptar.Click
If ValidText.ValidText.requiredFieldsCheck(Me) = True Then
MsgBox("OK!", MsgBoxStyle.Information, Me.Text)
Me.Close()
Else
MsgBox("There are required fiels in the form!", _
MsgBoxStyle.Exclamation, Me.Text)
End If
End Sub
cancelRequiredFieldsCheck
: Lets avoid the required validation of ValidText
controls of the form you send as parameter. Private Sub btnCancelar_Click(ByVal sender As System.Object,_
ByVal e As System.EventArgs) Handles btnCancelar.Click
ValidText.ValidText.cancelrequiredFieldsCheck(Me)
Me.Close()
End Sub
Note: The buttons or others control used to fire these methods need to set their Causesvalidation
property to FALSE
, in order to have access to their events.
Conclusion
Please send me feedback, comments, and suggest to: ra_luis@yahoo.com