Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 520
  • Last Modified:

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,
0
RedSouljaz
Asked:
RedSouljaz
  • 2
1 Solution
 
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
 
RedSouljazAuthor Commented:
Thanks for the information, it is very helpfull.

Thanks a lot guys.

Regards
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now