Click here to Skip to main content
11,491,459 members (69,835 online)
Click here to Skip to main content

FlexMaskEditBox - a masked edit control

, 28 Apr 2003 284.7K 677 46
Rate this:
Please Sign up or sign in to vote.
A very flexible MaskTextbox control

Introduction

Why will you have to use a MaskEditBox? It's not a professional look when the end user can give alphanumeric input when numeric input is required.

It's a piece of cake to validate the input and/or give feedback of wrong input to the type goats.

Using the code

There are 3 types of input viz. alphabetic, numeric and date. The behavior of the three types are different when typing.

When using the numeric type together with decimals (example mask:= "9999D##") and the end user types the decimal notation a (.) or (,) depending on international setting, the cursor automatically goes to the decimal point. Also the input characters are drawn from right to left. This is an example of validation using the FlexMaskEditBox and give feedback to the end user by ErrorTxt.

 
Private Sub FlexMaskEditBox1_Validating(ByVal sender As Object, 
    ByVal e As System.ComponentModel.CancelEventArgs) 
    Handles FlexMaskEditBox1.Validating, FlexMaskEditBox2.Validating, 
    FlexMaskEditBox3.Validating, FlexMaskEditBox5.Validating
    
    Dim fmb As FlxMaskBox.FlexMaskEditBox = CType(sender, 
        FlxMaskBox.FlexMaskEditBox)
    
    If fmb Is FlexMaskEditBox1 Then
        Try
            Dim Value As Decimal = Decimal.Parse(fmb.Text)
            If Value < 201.13 OrElse Value > 4027.55 Then
                fmb.ErrorTxt = "Value MUST between 201.13 
                                    and 4027.55"
                If CheckBox1.Checked Then e.Cancel = True
            Else
                fmb.ErrorTxt = ""
            End If
        Catch 'normal, this will never been evaluate
            fmb.ErrorTxt = "There is no Valid Value given"
            If CheckBox1.Checked Then e.Cancel = True
        End Try
    ElseIf fmb Is FlexMaskEditBox2 Then
        Try
            Dim dt As DateTime = DateTime.Parse(fmb.Text)
            If dt.Year < 1980 OrElse dt.Year > 2010 Then
                fmb.ErrorTxt = "The Range of valid Years 
                                    must be between 1980 an 2010"
                If CheckBox1.Checked Then e.Cancel = True
            Else
                fmb.ErrorTxt = ""
            End If
        Catch
            fmb.ErrorTxt = "Not A Valid Date, Try Again!"
            If CheckBox1.Checked Then e.Cancel = True
        End Try
    ElseIf fmb Is FlexMaskEditBox3 Then
        With fmb
            If .Text.Replace(" ", "").Length < 6 Then
                .ErrorTxt = "Not All Characters Are Given"
                If CheckBox1.Checked Then e.Cancel = True
            Else
                .ErrorTxt = ""
            End If
        End With
    ElseIf fmb Is FlexMaskEditBox5 Then
        With fmb
            If Not Regex.IsMatch(.Text, "^([\w-]+\.)*?
                    [\w-]+@[\w-]+\.([\w-]+\.)*?[\w]+$") Then
                    
                .ErrorTxt = "No valid Email address"
                If CheckBox1.Checked Then e.Cancel = True
            Else
                .ErrorTxt = ""
            End If
        End With
    End If
End Sub

Using the Mask

(#) - (chr 48 - 57)

(9) - (chr 48 - 57) (-) minus sign

(?) - (chr 65 - 90) (chr 97 - 121)

(A)(C)  - (chr 65 - 90) (chr 97 - 121) (chr 48 - 57)

(a)(c)  - (chr 65 - 90) (chr 97 - 121) (chr 48 - 57) (chr 32 )

(&) - (chr 32 - 126) (chr 128 - 255)

(\)  Next char will be no mask char

(>) All chars next to the > sign will transformed in upper-case
    until the next mask char is an (<) or (\) or (|)

(}) Next 1 Char will transformed in upper-case
    
(<) All chars next to the < sign will transformed in lower-case
    until the next mask char is an (>) or (\) or (|)
    
(|) Stops next chars from Upper or Lower casing

({) Next 1 Char will transformed in lower-case

(d)(D) Decimal Point (#####d99) = 12345.67 or 12345,67  
       (depending on country settings)

(g)(G) Group Separator (##g###g###d##) = 12.345.678,90 or 12,345,678.90
       (depending on country settings)
  
(s)(S) Date Separator (##s##s####) = 12-12-2000 or 12/12/2000
       (depending on country settings)

So when you want to include characters in your mask, which are also mask signs, use the \ in front. For instance, ##s##s1\9## (the 9 is also a mask sign, but the 1 is not)

Including the FlexMaskEditBox into your project

Instructions for using the FlexMaskEditControl control in a new project:

  1. Start Microsoft Visual Studio .NET.
  2. Open the FlexMaskEdit.vbproj project.
  3. On the Build menu, click Build Solution to build FlexMaskEdit.dll.
  4. Close the project.
  5. Create a new Windows application using Visual Basic .NET. Form1 is created by default.
  6. On the Tools menu, click Customize Toolbox.
  7. Select the .NET Framework Components tab.
  8. Click the Browse button.
  9. Navigate to the folder where FlexMaskEdit.dll was built (it should be the bin folder located underneath the location where you unzipped the project).
  10. Select FlexMaskEdit.dll and click Open. It will automatically add it to the selected components list and check the item for you.
  11. Click OK to close the Customize Toolbox dialog box. Note that the FlexMaskEdit control now appears on your Toolbox. That's all, now you can draw the FlexMaskEditBox like you do a TextBox.

History

  • Original the source code was written in VB6 (a few years ago) but re-written for better performance.

Acknowledgement

  • Many thanks to Hans Scholten (Email: wonen@wonen.com) for his input.

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

Share

About the Author

Ted Schopenhouer

Netherlands Netherlands
No Biography provided

Comments and Discussions

 
QuestionError at Drap & drop Pin
Member 140939229-Feb-12 11:47
memberMember 140939229-Feb-12 11:47 
GeneralWould be better to follow a standard for formatting Pin
Member 28774012-May-11 8:26
memberMember 28774012-May-11 8:26 
GeneralTotally useless : native in windows ! Pin
kilt20-Feb-09 7:45
memberkilt20-Feb-09 7:45 
GeneralRe: Totally useless : native in windows ! Pin
Mizan Rahman22-Sep-09 4:41
memberMizan Rahman22-Sep-09 4:41 
QuestionIs there any version for VBA Excel? Pin
ironyq@wp.pl23-Jun-07 5:44
memberironyq@wp.pl23-Jun-07 5:44 
GeneralError using flexeditcontrol Pin
The Shrink1-Mar-07 13:59
memberThe Shrink1-Mar-07 13:59 
GeneralRe: Error using flexeditcontrol Pin
Albanaco31-May-07 10:38
memberAlbanaco31-May-07 10:38 
QuestionCan i remove $ sign Pin
devagnanam11-Jan-07 19:17
memberdevagnanam11-Jan-07 19:17 
QuestionObjet disapears when I copy my projet in a new computer Pin
Adrian_Mia31-Jul-06 11:51
memberAdrian_Mia31-Jul-06 11:51 
QuestionFlexMaskEdit control Pin
JohnMcPherson19-Mar-06 11:50
memberJohnMcPherson19-Mar-06 11:50 
AnswerRe: FlexMaskEdit control Pin
Deester25-May-06 6:33
memberDeester25-May-06 6:33 
QuestionVersion for VB6? Pin
Michael F. Henning24-May-05 11:22
memberMichael F. Henning24-May-05 11:22 
GeneralError when copy control Pin
vicopocho30-Mar-05 9:57
membervicopocho30-Mar-05 9:57 
GeneralFlexMaskEditBox on win98 Pin
vicopocho30-Mar-05 9:50
membervicopocho30-Mar-05 9:50 
GeneralLost Focus with &quot;Enter&quot; Pin
samoeln16-Dec-04 0:20
membersamoeln16-Dec-04 0:20 
GeneralDeploying FlexMaskEditBox Pin
rschaefer207-Oct-04 14:05
memberrschaefer207-Oct-04 14:05 
Generaldate mask showing funny Pin
LonnieJohnson7-Sep-04 8:29
memberLonnieJohnson7-Sep-04 8:29 
GeneralRe: date mask showing funny Pin
radaremtjcw30-Jan-05 14:36
memberradaremtjcw30-Jan-05 14:36 
GeneralRe: date mask showing funny Pin
GustavoBalbinot14-Jun-05 2:37
memberGustavoBalbinot14-Jun-05 2:37 
GeneralRe: date mask showing funny Pin
ComputerNerd9270829-Jul-05 10:34
memberComputerNerd9270829-Jul-05 10:34 
Generalcannot access mask Pin
sconnor130-Aug-04 5:50
membersconnor130-Aug-04 5:50 
GeneralFlex Edit Box on Tabpage Pin
amwoodwork29-Jul-04 5:38
memberamwoodwork29-Jul-04 5:38 
GeneralRe: Flex Edit Box on Tabpage Pin
HaggardPete10-Aug-04 1:37
memberHaggardPete10-Aug-04 1:37 
GeneralRe: Flex Edit Box on Tabpage Pin
TheSpiritual112-Nov-04 5:44
memberTheSpiritual112-Nov-04 5:44 
GeneralFlexMaskedit - Problems With Numerics Pin
HaggardPete27-Jun-04 23:58
memberHaggardPete27-Jun-04 23:58 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150520.1 | Last Updated 29 Apr 2003
Article Copyright 2003 by Ted Schopenhouer
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid