Prevent colon in textbox

jamie_pattison

Well-known member
Joined
Sep 9, 2008
Messages
116
Programming Experience
Beginner
The below code allows me to prevent a textbox to only allow numerics, backspace. I would also like to have the textbox to accept ":", but struggling to find the correct way to do this. Could anyone advise? Below code is in the Keypress event.

Thanks

VB.NET:
        If Not Char.IsDigit(e.KeyChar) Then
            If Not e.KeyChar = vbBack Then
?????????????
            End If
        End If
 
Hi,
Try this one :)

VB.NET:
If Not Char.IsDigit(e.KeyChar) Then
   If Not e.KeyChar = vbBack Then
      If Not e.KeyChar = chr(58)


      End if      
   End If
End If

or without so many ifs

VB.NET:
If Char.IsDigit(e.KeyChar) or e.KeyChar = vbBack or e.KeyChar = chr(58) then
' your code goes here
End if
 
The following will permit only numeric input, the backspace key, and also the colon:


VB.NET:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
		If Not Char.IsDigit(e.KeyChar) Then e.Handled = True
		If e.KeyChar = vbBack Or e.KeyChar = ":" Then e.Handled = False
	End Sub
 
Back
Top