-
Sep 13th, 2021, 01:13 PM
#1
Thread Starter
Lively Member
[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
-
Sep 13th, 2021, 01:30 PM
#2
Re: Textbox accept only numbers, dots and slashes
Use a MaskedEdit Control?
-
Sep 13th, 2021, 01:31 PM
#3
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.
-
Sep 13th, 2021, 01:43 PM
#4
Thread Starter
Lively Member
Re: Textbox accept only numbers, dots and slashes
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|