Results 1 to 4 of 4

Thread: [RESOLVED] Textbox accept only numbers, dots and slashes

Hybrid View

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2021
    Posts
    45

    Resolved [RESOLVED] Textbox accept only numbers, dots and slashes

    Hello VbForums
    Thank you for the help you are providing.
    Today I'm stuck.
    I want a textbox to accept only numbers, dots and slashes "/ " and maybe colons ":"
    For accepting only numbers I have this code:
    Private Sub Text2_Change()
    If Len(Text2.Text) > 0 Then
    If Not IsNumeric(Right(Text2.Text, 1)) Then
    Text2.Text = Mid(Text2.Text, 1, Len(Text2.Text) - 1)
    Text2.SelStart = Len(Text2.Text)
    End If
    End If

    End Sub

  2. #2
    PowerPoster
    Join Date
    Feb 2006
    Posts
    22,976

    Re: Textbox accept only numbers, dots and slashes

    Use a MaskedEdit Control?

  3. #3
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    13,828

    Re: Textbox accept only numbers, dots and slashes

    Well One simple way would be something like this
    Code:
    Private Sub Text1_Change()
    If Len(Text1.Text) > 0 Then
        Select Case Right$(Text1.Text, 1)
        Case 0 To 9, ":", "/"
            'These characters will be accepted
        Case Else
            Text1.Text = Left$(Text1.Text, Len(Text1.Text) - 1)
            Text1.SelStart = Len(Text1.Text)
        End Select
    End If
    End Sub
    You could also use a trap in the keypress event to prevent the keys from being entered.
    Of course both of these methods can fail if the user pastes something into the text box.
    The keypress event will not see a paste at all and the change event as coded only looks at the last character so if more than one are pasted it could fail.

    Another option is the masked edit control which will allow only predefined characters to be entered.

  4. #4

    Thread Starter
    Member
    Join Date
    Mar 2021
    Posts
    45

    Re: Textbox accept only numbers, dots and slashes

    Thank you
    solved

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width