vb中限制一个文本框只能输入数字和一个小数点和退格键。
发布网友
发布时间:2022-05-09 20:23
我来回答
共3个回答
热心网友
时间:2023-10-17 16:44
Private Sub Text1_KeyPress(KeyAscii As Integer)
If InStr(Text1.Text, ".") <> 0 Then
If (Chr(KeyAscii) > "9 " Or Chr(KeyAscii) < "0 ") And KeyAscii <> 48 And KeyAscii <> 8 Then
KeyAscii = 0
End If
Else
If (Chr(KeyAscii) > "9 " Or Chr(KeyAscii) < "0 ") And Chr(KeyAscii) <> "." And KeyAscii <> 48 And KeyAscii <> 8 Then
KeyAscii = 0
End If
End If
End Sub
在窗体中建立一个text1的文本框,输入如上命令即可实现。
热心网友
时间:2023-10-17 16:45
如果系统没有现成的功能的话,那就监听该空间的 OnKeyDown消息,然后判断输入的字符,不是数字和退格的,放弃。
热心网友
时间:2023-10-17 16:45
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 46 Or (KeyAscii >= 48 And KeyAscii <= 57) Or KeyAscii = 8 Then
Else
KeyAscii = 0
End If
End Sub追问小数点要*成只能输入一个
追答Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 46 Or (KeyAscii >= 48 And KeyAscii <= 57) Or (instr(1,text1,",")=0 and KeyAscii = 8 )Then
Else
KeyAscii = 0
End If
End Sub