给你一个函数,这是我以前问过的问题。 你在Text的KeyPress事件中调用此函数就可以了:Public Function KeyNum(str As String, key As Integer, ByVal SelectedLength As Integer) As Integer Select Case key Case 8, 13, 48 To 57 ' 接收退格键(ASCII码为8)、回车键(ASCII码为13)和数字键(ASCII码为48~57) KeyNum = key Case 46 '收小数点(ASCII码为46),但小数点只能出现一次 If InStr(str, ".") = 0 Then KeyNum = key Else KeyNum = 0 Case 45 ' 当输入框为空或其内容全部选上时接收"-"(ASCII码为45),即"-"只能出现在第一位 If str = "" Or SelectedLength = Len(str) Then KeyNum = key Else KeyNum = 0 Case Else KeyNum = 0 ' 其他键不接收,用ASCII码为0的字符表示不接收输入信息 End Select End FunctionPrivate Sub Text1_KeyPress(KeyAscii As Integer) SelectedLength = txtEdit.SelLength ' 如果选择了全部,可接收负号 KeyAscii = KeyNum(txtEdit.Text, KeyAscii, SelectedLength) End Sub
Private Sub text1.KeyPress(KeyAscii As Integer) ' Dim stra As String ' stra="0123456789-." '文本框中可输入的字符 if KeyAscii > 26 Then '控制符不过滤(太多数控制符的Ascii码<26) if InStr(stra,Chr(KeyAscii))=0 Then KeyAscii=0 end if end if End Sub
你在Text的KeyPress事件中调用此函数就可以了:Public Function KeyNum(str As String, key As Integer, ByVal SelectedLength As Integer) As Integer
Select Case key
Case 8, 13, 48 To 57
' 接收退格键(ASCII码为8)、回车键(ASCII码为13)和数字键(ASCII码为48~57)
KeyNum = key
Case 46 '收小数点(ASCII码为46),但小数点只能出现一次
If InStr(str, ".") = 0 Then KeyNum = key Else KeyNum = 0
Case 45
' 当输入框为空或其内容全部选上时接收"-"(ASCII码为45),即"-"只能出现在第一位
If str = "" Or SelectedLength = Len(str) Then KeyNum = key Else KeyNum = 0
Case Else
KeyNum = 0 ' 其他键不接收,用ASCII码为0的字符表示不接收输入信息
End Select
End FunctionPrivate Sub Text1_KeyPress(KeyAscii As Integer)
SelectedLength = txtEdit.SelLength ' 如果选择了全部,可接收负号
KeyAscii = KeyNum(txtEdit.Text, KeyAscii, SelectedLength)
End Sub
'
Dim stra As String
'
stra="0123456789-." '文本框中可输入的字符
if KeyAscii > 26 Then '控制符不过滤(太多数控制符的Ascii码<26)
if InStr(stra,Chr(KeyAscii))=0 Then
KeyAscii=0
end if
end if
End Sub