Click here to Skip to main content
15,881,380 members
Articles / Programming Languages / Visual Basic
Article

TextBox with Validation Functions and MaskEdit

Rate me:
Please Sign up or sign in to vote.
4.07/5 (15 votes)
30 May 20032 min read 93.3K   479   28   7
CustomControl that inherits from TextBox class and adds input validation functions, required validation functions and MaskEdit mode.

Sample Image - ValidText.jpg

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:

  1. ValitationMode (ValidationTypes): Sets the validation mode to use for the text or data field. This validation mode ca be: None, VaidChars, InvalidChars, Letters, Numbers and MaskEdit.
  2. MaskString (String): Sets the allowed mask for the input data when ValidationMode = 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 the ValidationMode 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.
  3. ValidString (String): Sets the valid and not valid characters allowed when ValidationMode = ValidChars or InvalidChars.
  4. ShowErrorIcon (Boolean): Sets if the icon and tooltip of the ErrorProvider is showed in the validation of the data field.
  5. Required (Boolean): Sets if the data field must have at last one character to be validated.
  6. 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

VB.NET
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:

VB.NET
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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Costa Rica Costa Rica
Ing. Luis Alberto Ruiz Arauz
Software Engineering / Industrial Engineering

Comments and Discussions

 
GeneralError found and solution Pin
Jose Juan Calvo15-Nov-04 23:40
sussJose Juan Calvo15-Nov-04 23:40 
QuestionHow do I get it to work in VS 2003 Pin
Picine Karppehd29-Oct-04 4:02
Picine Karppehd29-Oct-04 4:02 
GeneralGreat idea... Pin
Matt Berther5-Apr-03 17:54
Matt Berther5-Apr-03 17:54 
GeneralRe: Great idea... Pin
machocr29-May-03 6:51
machocr29-May-03 6:51 
Generalnice idea Pin
yarix4-Apr-03 1:24
yarix4-Apr-03 1:24 
GeneralRe: nice idea Pin
machocr29-May-03 6:40
machocr29-May-03 6:40 
QuestionWhat do you think about validText? Pin
machocr29-Jan-03 10:29
machocr29-Jan-03 10:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.