Click here to Skip to main content
13,252,537 members (58,067 online)
Click here to Skip to main content
Add your own
alternative version


30 bookmarked
Posted 17 Jan 2003

VB.NET Maskedbox Control - Final Release

, 17 Jan 2003
Rate this:
Please Sign up or sign in to vote.
A very useful maskedbox control using

Sample Image - MaskedBox_Control.jpg


VB.NET does not have a masked texbox and it's very important for user input. So I have made it myself because the components I have found is not good enough for me.


  • The "#" character in the mask is used for digit only
  • The "&" character accepts letter or digit
  • Supported mask symbols are "-", ".", ",", "/", "\", ":"

Using Component

The use of this component is very simple. You have to select the toolbox panel, right click in the windows forms section and select customize toolbox.

Select .NET framework components, then click browse and select maskedbox.dll in the download path. The maskedbox component will appear at the end of windows forms panel. Now select the component and drop it on a form.

Then set the mask property with any combination of valid characters

Ex. ##.###.####-###.###/###


  • Initial - I fixed a bug where the space key is not recognized. This version inherits a textbox component to publish all features of the textbox plus maskedbox
  • 20-jan-2003 Fixes - Function keys now are not handled by control. They cause an invalid character in between the text. The function SetText has been created. Programatically changing the text value in control din't change the masked text, so I created this function where the parameter is a string to fill the text (with no mask). If the text is "", then the text and mask is cleared with a empty mask.
  • 21-jan-2003 - The Final Release!!! I hope! Accept all characters and function keys. The delete key is working now !


Any comments ? Email me at Visit my business site at


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


About the Author

Web Developer
Brazil Brazil
Im a 33 years old and work developing applications since 14 years old

COBOL, my first language (i love)
BASIC, i born know

Visual Basic Rock's

You may also be interested in...

Comments and Discussions

GeneralGreat Help - Thanks Pin
mbusfield26-Nov-06 5:14
membermbusfield26-Nov-06 5:14 
GeneralSuggested enhancements Pin
VBProEd31-Dec-05 9:45
memberVBProEd31-Dec-05 9:45 
Thanks for the simplistic code that was easy to read and modify for my purposes. There are several masked edit controls in this forum that honestly don't work or were poorly tested. I thought I would share the enhancements to my version for the stated valid reasons. My scope for these changes was around date formatting/editing.

1. If a user selects the entire field for delete (to re-type), the control should clear out the entire field. Once the user starts typing, the mask automatically fills in and the user can start typing the new date. Here is my code addition:

Protected Overrides Sub OnKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs)<br />
        Dim tmpset = Me.SelectionStart<br />
<br />
        On Error Resume Next<br />
     Select Case e.KeyCode<br />
            Case Keys.Delete<br />
                If Len(Me.SelectedText) = Len(Me.Text) Then<br />
                    Me.Text = String.Empty<br />
                    Exit Select<br />
                End If                <br />
                Me.Text = ""<br />
                For a = tmpset To aMask.Length - 1<br />
                    Select Case aMskMask.GetValue(a + 1)<br />
                        Case ".", "-", "\", "/", ","<br />
                            aMask.SetValue(aMask.GetValue(a + 2), a)<br />
                            a = a + 1<br />
                        Case Else<br />
                            aMask.SetValue(aMask.GetValue(a + 1), a)<br />
                    End Select<br />
                Next<br />
<br />
                aMask.SetValue(CType("_", Char), aMask.Length - 1)<br />
<br />
                e.Handled = True<br />
<br />
                Me.Text = ""<br />
                For a = 0 To aMask.Length - 1<br />
                    Me.Text += aMask.GetValue(a)<br />
                Next<br />
                Me.SelectionStart = tmpset<br />
<br />
        End Select<br />
 End Sub

2. With SQL Server, DateTime values with leading zeroes are stored as m/d/yyyy, mm/d/yyyy and m/dd/yyyy (no leading zeroes). This creates havoc in a control when attempting to edit the "as stored" value since most data entry is done heads down without concern of field delimiters. I added the following events to handle re-formatting the date when first entering then leaving the control. These events also handle any input field defined with a mask of ##/##/#### and the user does not enter the Century date value.

Note: You should always set the "MaxLength" property on MaskedBox to either 8 or 10 (for dates) so that the user can automatically move to the next control.

Protected Overrides Sub OnEnter(ByVal e As System.EventArgs)<br />
     MyBase.Text = Me.FormatData(Me.Text())<br />
End Sub<br />
<br />
Protected Overrides Sub OnLeave(ByVal e As System.EventArgs)<br />
     MyBase.Text = Me.FormatData(Me.Text())<br />
End Sub<br />
<br />
Protected Function FormatData(ByVal sText As String) As String<br />
<br />
        If sText Is Nothing Or sText Is String.Empty Then<br />
            Exit Function<br />
        Else<br />
            sText = sText.Replace("_", "")<br />
<br />
            If Len(Me.Mask) < 10 Then<br />
                Try<br />
                    Dim MyDate As DateTime = CType(sText, Date)<br />
                    Return MyDate.ToString("MM/dd/yy")<br />
<br />
                Catch<br />
<br />
                    Return sText<br />
<br />
                End Try<br />
            Else<br />
                Try<br />
                    Dim MyDate As DateTime = CType(sText, Date)<br />
                    Return MyDate.ToString("MM/dd/yyyy")<br />
<br />
                Catch<br />
<br />
                    Return sText<br />
<br />
                End Try<br />
            End If<br />
<br />
        End If<br />
<br />
    End Function<br />

3. For those folks using SQL Server with controls bound to datasets that don't want to show the "as stored" date value from SQL Server, add the following handler and subroutine for all date field reformatting in your form.

Note: User-Defined Date/Time Formats (Format Function) - Unlike previous versions of Visual Basic, the Format function characters are case-sensitive. "MM" is not the same as "mm".
<br />
AddHandler txtMyDateField1.DataBindings("Text").Format, AddressOf Format<br />
AddHandler txtMyDateField2.DataBindings("Text").Format, AddressOf Format<br />
<br />
Public Sub Format(ByVal sender As Object, ByVal e As Windows.Forms.ConvertEventArgs)<br />
    'This subroutine converts any SQL Server timedate to mm/dd/yy format<br />
    If Not IsDBNull(e.Value) Then<br />
        Dim MyDate As DateTime = CType(e.Value, Date)<br />
        e.Value = MyDate.ToString("MM/dd/yy")<br />
    End If<br />
End Sub

Enjoy!Big Grin | :-D
JokeRe: Suggested enhancements Pin
RicardoPDV3-Jan-06 2:45
memberRicardoPDV3-Jan-06 2:45 
Generalmiguelfantastic Pin
miguelfantastic27-May-05 14:03
membermiguelfantastic27-May-05 14:03 
GeneralProblem in Using this Control Pin
namasteall20001-Oct-04 5:21
membernamasteall20001-Oct-04 5:21 
GeneralBackspace Bug Pin
AnAverageJoe9-Jun-04 6:54
memberAnAverageJoe9-Jun-04 6:54 
GeneralRe: Backspace Bug Pin
MrGTI1-Oct-04 5:32
memberMrGTI1-Oct-04 5:32 
GeneralRe: Backspace Bug Pin
buffbuh28-Mar-07 13:54
memberbuffbuh28-Mar-07 13:54 
GeneralAbsolutely Clueless.... Pin
TheKanuck27-May-04 4:02
memberTheKanuck27-May-04 4:02 
GeneralRe: Absolutely Clueless.... Pin
Xpto00129-Oct-04 19:24
memberXpto00129-Oct-04 19:24 
GeneralPoor Control Load Pin
MrGTI23-Sep-03 5:30
memberMrGTI23-Sep-03 5:30 
GeneralRe: Poor Control Load Pin
namasteall20001-Oct-04 5:13
membernamasteall20001-Oct-04 5:13 
GeneralBUG Pin
balu_vee14-Aug-03 4:34
memberbalu_vee14-Aug-03 4:34 
GeneralRe: BUG Pin
namasteall20001-Oct-04 5:14
membernamasteall20001-Oct-04 5:14 
GeneralOption strict please Pin
nemaroller30-Apr-03 5:18
membernemaroller30-Apr-03 5:18 
GeneralRe: Option strict please Pin
namasteall20001-Oct-04 5:14
membernamasteall20001-Oct-04 5:14 
Questionsmall bug ??? Pin
zefrit22-Apr-03 8:13
memberzefrit22-Apr-03 8:13 
AnswerHelp me Pin
namasteall20001-Oct-04 5:19
membernamasteall20001-Oct-04 5:19 
GeneralSmall bug Pin
Richard Deeming20-Jan-03 4:18
memberRichard Deeming20-Jan-03 4:18 
GeneralRe: Small bug Pin
RicardoPDV20-Jan-03 6:50
memberRicardoPDV20-Jan-03 6:50 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171114.1 | Last Updated 18 Jan 2003
Article Copyright 2003 by RicardoPDV
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid