一下是实现你想要的功能的源代码:Private Sub Text1_KeyPress(KeyAscii As Integer) Select Case Chr(KeyAscii) '判断输入的字符的ASCII值 '输入的字符只能时数字0-9和BACKSPACE键,回车键和DELETE键 Case 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, Chr(8), Chr(13), Chr(46) '输入字符“-”只能在第一位 Case "-" If Len(Text1.Text) > 1 Then KeyAscii = 0 End If '当不是以上字符时禁止输入 Case Else KeyAscii = 0 End Select End Sub
看看是否已经实现了你想要的功能了.......
'调用KeyAscii = ValiText(KeyAscii, "0123456789/-", True) '只接受第二个参数提供的字符 '即:"0123456789/-", 而此函数的第三个函数就决定了能否使用 [Backspace] 键。 '此函数对大小写是不敏感的Function ValiText(KeyIn As Integer, ValidateString As String, Editable As Boolean) As Integer Dim ValidateList As String Dim KeyOut As Integer If Editable = True Then ValidateList = UCase(ValidateString) & Chr(8) Else ValidateList = UCase(ValidateString) End If If InStr(1, ValidateList, UCase(Chr(KeyIn)), 1) > 0 Then KeyOut = KeyIn Else KeyOut = 0 Beep End If ValiText = KeyOut End Function真的不要忘了给分阿 【我不是流氓~~~~~~~才怪!】
Select Case Chr(KeyAscii) '判断输入的字符的ASCII值
'输入的字符只能时数字0-9和BACKSPACE键,回车键和DELETE键
Case 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, Chr(8), Chr(13), Chr(46)
'输入字符“-”只能在第一位
Case "-"
If Len(Text1.Text) > 1 Then
KeyAscii = 0
End If
'当不是以上字符时禁止输入
Case Else
KeyAscii = 0
End Select
End Sub
看看是否已经实现了你想要的功能了.......
'只接受第二个参数提供的字符
'即:"0123456789/-", 而此函数的第三个函数就决定了能否使用 [Backspace] 键。
'此函数对大小写是不敏感的Function ValiText(KeyIn As Integer, ValidateString As String, Editable As Boolean) As Integer
Dim ValidateList As String
Dim KeyOut As Integer
If Editable = True Then
ValidateList = UCase(ValidateString) & Chr(8)
Else
ValidateList = UCase(ValidateString)
End If
If InStr(1, ValidateList, UCase(Chr(KeyIn)), 1) > 0 Then
KeyOut = KeyIn
Else
KeyOut = 0
Beep
End If
ValiText = KeyOut
End Function真的不要忘了给分阿
【我不是流氓~~~~~~~才怪!】