Private Sub Text1_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 48 To 57 '0-9,這裏還可以限制只能輸入哪幾個數字
Exit Sub
Case 8 '退格鍵
Exit Sub
Case 46 '小数点不是Delete鍵 ,跟踪发现按Delete鍵是的不触发按下事件的。。。。
Exit Sub
Case Else
KeyAscii = 0
End Select
End Sub
Private Sub Text2_KeyPress(KeyAscii As Integer)
If Len(Text1) - InStrRev(Text1, ".") >= 2 And InStr(Text1, ".") <> 0 Then
If KeyAscii = 13 Or KeyAscii = 8 Or KeyAscii = 9 Then
Else
KeyAscii = 0
End If
End If
End Sub
---------------
下面的代码,也是从网上搜索来的,好象还可以 的。。。。
Public Function Numeric_Only(x As Integer) As Integer
' Bybal
Select Case x
Case 8, 9, 13, &H30 To &H39, Asc(".") '可以根据需要增减
Numeric_Only = x
Case Else
Numeric_Only = 0
End Select
End Function
Public Function Hex_Only(x As Integer) As Integer
'Bybal
Select Case x
Case 8, 9, 13, &H30 To &H39, Asc("A") To Asc("F")
Hex_Only = x
Case Asc("a") To Asc("f")
Hex_Only = x - 32
Case Else
Hex_Only = 0
End Select
End Function
'调用:
Private Sub Text1_KeyPress(KeyAscii As Integer)
'只能输入数字
KeyAscii = Numeric_Only(KeyAscii)
End Sub
Private Sub Text2_KeyPress(KeyAscii As Integer)
'只能输入十六进制数字
KeyAscii = Hex_Only(KeyAscii)
End Sub