Textbox Mask

Hi all,

      I would like to ask, how to put mask in the textbox, for example 012-1010101. And besides that how to avoid character entered into the textbox, I only want to input numbers.
Thanks all

regards,
RedSouljazAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

iboutchkineCommented:
You can use MaskedBox control from VB6
or you can write your own class
something like that

1.Open Class library project (MaskedBox)
2. Save it It will compile as MaskedBox.dll

here is the code
-------------------------
Public Class MaskedBox
    Inherits System.Windows.Forms.TextBox

    Private aMskMask() As Char
    Private aMask() As Char
    Private tmpMask As String
    Private a As Integer

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()
        InitializeComponent()
    End Sub

    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    Private components As System.ComponentModel.IContainer

    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        '
        'MaskedBox
        '
        Me.Name = "MaskedBox"

    End Sub

#End Region

    Public Property Mask() As String

        Get
            Return tmpMask
        End Get

        Set(ByVal Value As String)
            tmpMask = Value
            SetMask()
        End Set

    End Property

    Private Sub SetMask()

        On Error Resume Next

        Me.Text = tmpMask
        Me.Text = Me.Text.Replace("#", "_")
        Me.Text = Me.Text.Replace("&", "_")

        ReDim aMask(Me.Text.Length - 1)
        ReDim aMskMask(Me.Text.Length - 1)

        For a = 0 To tmpMask.Length - 1
            If tmpMask.Substring(a, 1) = "#" Or tmpMask.Substring(a, 1) = "&" Then
                aMask.SetValue(CType("_", Char), a)
            Else
                aMask.SetValue(CType(tmpMask.Substring(a, 1), Char), a)
            End If
        Next

        For a = 0 To tmpMask.Length - 1
            aMskMask.SetValue(CType(tmpMask.Substring(a, 1), Char), a)
        Next

    End Sub

    Protected Overrides Sub OnKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs)
        Dim tmpset = Me.SelectionStart

        On Error Resume Next

        Select Case e.KeyCode
            Case Keys.Delete
                Me.Text = ""
                For a = tmpset To aMask.Length - 1
                    Select Case aMskMask.GetValue(a + 1)
                        Case ".", "-", "\", "/", ","
                            aMask.SetValue(aMask.GetValue(a + 2), a)
                            a = a + 1
                        Case Else
                            aMask.SetValue(aMask.GetValue(a + 1), a)
                    End Select
                Next

                aMask.SetValue(CType("_", Char), aMask.Length - 1)

                e.Handled = True

                Me.Text = ""
                For a = 0 To aMask.Length - 1
                    Me.Text += aMask.GetValue(a)
                Next
                Me.SelectionStart = tmpset

        End Select

    End Sub

    Protected Overrides Sub OnKeyUp(ByVal e As System.Windows.Forms.KeyEventArgs)
        e.Handled = True
    End Sub

    Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
        Dim tmpset = Me.SelectionStart
        Dim tmpCH As Char

        On Error Resume Next

        If Asc(e.KeyChar) = 8 Then
            Select Case CType(aMskMask.GetValue(tmpset - 1), String)
                Case ".", "-", "\", "/", ","
                    tmpset = tmpset - 2
                    aMask.SetValue(CType("_", Char), tmpset)
                    tmpset = tmpset - 1
                Case Else
                    tmpset = tmpset - 1
                    aMask.SetValue(CType("_", Char), tmpset)
                    tmpset = tmpset - 1
            End Select

            e.Handled = True

            Me.Text = ""
            For a = 0 To aMask.Length - 1
                Me.Text += aMask.GetValue(a)
            Next
            Me.SelectionStart = tmpset + 1

        ElseIf Char.IsControl(e.KeyChar) Then

        Else
            Select Case aMskMask.GetValue(tmpset)
                Case ".", "-", "\", "/", ","
                    tmpset = tmpset + 1
                    If aMskMask.GetValue(tmpset) = "#" Then
                        If Char.IsDigit(e.KeyChar) Then
                            aMask.SetValue(e.KeyChar, tmpset)
                        Else
                            tmpset = tmpset - 1
                        End If
                    ElseIf aMskMask.GetValue(tmpset) = "&" Then
                        aMask.SetValue(e.KeyChar, tmpset)
                    End If

                Case Else
                    If aMskMask.GetValue(tmpset) = "#" Then
                        If Char.IsDigit(e.KeyChar) Then
                            aMask.SetValue(e.KeyChar, tmpset)
                        Else
                            tmpset = tmpset - 1
                        End If
                    ElseIf aMskMask.GetValue(tmpset) = "&" Then
                        aMask.SetValue(e.KeyChar, tmpset)
                    End If

            End Select

            e.Handled = True

            Me.Text = ""
            For a = 0 To aMask.Length - 1
                Me.Text += aMask.GetValue(a)
            Next
            Me.SelectionStart = tmpset + 1

        End If
    End Sub

    Public Sub SetText(ByVal txt As String)

        On Error Resume Next

        If txt = "" Then
            For a = 0 To tmpMask.Length - 1
                If tmpMask.Substring(a, 1) = "#" Or tmpMask.Substring(a, 1) = "&" Then
                    aMask.SetValue(CType("_", Char), a)
                Else
                    aMask.SetValue(CType(tmpMask.Substring(a, 1), Char), a)
                End If
            Next
        Else
            For a = 0 To txt.Length - 1
                If tmpMask.Substring(a, 1) = "#" Or tmpMask.Substring(a, 1) = "&" Then
                    aMask.SetValue(CType(txt.Substring(a, 1), Char), a)
                Else
                    aMask.SetValue(CType(tmpMask.Substring(a, 1), Char), a)
                End If
            Next

        End If

        Me.Text = ""
        For a = 0 To aMask.Length - 1
            Me.Text += aMask.GetValue(a)
        Next

    End Sub

End Class





================
To include to project
1. Open project
2.Click on Solution - Add existing project - point to MaskedBox.vbproj
3.Go to toolbox - right click - cusdtomize
4.Net Component
5.Click browse and add MaskedBox.dll
6.From ToolBox put this control on the form and set the mask in the properties



0
RonaldBiemansCommented:
Or use a regular expression in the validating event of the textbox like

Private Sub TextBox1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) _
Handles TextBox1.Validating
        If TextBox1.Text = String.Empty Then Exit
        If System.Text.RegularExpressions.Regex.Match(TextBox1.Text, "^\d{3}-\d{7}$").Success Then
            e.Cancel = False
        Else
            e.Cancel = True
        End If
End Sub

this should match 012-1010101

To allow only numeric  you can do the same

        If System.Text.RegularExpressions.Regex.Match(TextBox1.Text, "^\d*$").Success Then



0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RedSouljazAuthor Commented:
Thanks for the information, it is very helpfull.

Thanks a lot guys.

Regards
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.