在KeyPress事件中处理: Private Sub txtInput_KeyPress(KeyAscii As Integer) If (KeyAscii < 0) Then KeyAscii = 0: Exit Sub If (KeyAscii < 32) Then Exit Sub If ((KeyAscii < 48) OR (KeyAscii > 57) Then KeyAscii = 0End Sub 这样就只能输入数字了。
KeyPress里判断一下Ascii就可以了
If (KeyAscii<vbKey0 or KeyAscii>vbKey9) then KeyAscii=vbCancel endif
判断ASCII值,3楼、5楼的就是!
Public Function Digit_Only(byval keyascii As integer) As Integer Select Case keyascii Case 8, 9, 13, &H2E, &H30 To &H39 '允许Tab, Back Space, Enter, 小数点和数字键 Digit_Only = keyascii Case Else Digit_Only = 0 End Select End FunctionPrivate Sub Text1_KeyPress(KeyAscii As Integer) KeyAscii = Digit_Only(KeyAscii) End SubPrivate Sub Text2_KeyPress(KeyAscii As Integer) KeyAscii = Digit_Only(KeyAscii) End Sub
Private Sub txtInput_KeyPress(KeyAscii As Integer)
If (KeyAscii < 0) Then KeyAscii = 0: Exit Sub
If (KeyAscii < 32) Then Exit Sub
If ((KeyAscii < 48) OR (KeyAscii > 57) Then KeyAscii = 0End Sub 这样就只能输入数字了。
KeyAscii=vbCancel
endif
Select Case keyascii
Case 8, 9, 13, &H2E, &H30 To &H39 '允许Tab, Back Space, Enter, 小数点和数字键
Digit_Only = keyascii
Case Else
Digit_Only = 0
End Select
End FunctionPrivate Sub Text1_KeyPress(KeyAscii As Integer)
KeyAscii = Digit_Only(KeyAscii)
End SubPrivate Sub Text2_KeyPress(KeyAscii As Integer)
KeyAscii = Digit_Only(KeyAscii)
End Sub