跪求一段VB代码,textbox只能输入数字,小数点

高分请问下,跪求一段VB代码,textbox只能输入数字,小数点
最新回答
威猛的小平胸

2024-05-16 13:38:24

Private Sub Text1_KeyPress(KeyAscii As Integer)

If KeyAscii < 48 Or KeyAscii > 57 Then
If KeyAscii = 46 Then
If Text1.Text = "" Or InStr(1, Text1.Text, ".") <> 0 Then
KeyAscii = 0
Else
KeyAscii = 46
End If
Else
KeyAscii = 0
End If
End If

End Sub
山城夏秋

2024-05-16 20:33:55

VB妮可 的代码不错,不过找出点小问题-----不能退格

Private Sub Text1_KeyPress(KeyAscii As Integer)

If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 8 Then
If KeyAscii = 46 Then
If Text1.Text = "" Or InStr(1, Text1.Text, ".") <> 0 Then
KeyAscii = 0
Else
KeyAscii = 46
End If
Else
KeyAscii = 0
End If
End If

End Sub
满栀

2024-05-16 16:18:30

Private Sub Text1_KeyPress(KeyAscii As Integer)
If (KeyAscii < 46 Or KeyAscii > 57 or KeyAscii = 47)and KeyAscii<>8 Then
KeyAscii = 0
End If
End Sub
这样就可以了,可以用退格,KeyAscii为47是"\"符。
怎忘

2024-05-16 13:01:40

Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
If Right(Text1.Text, 1) = "." Then
Text1.Text = Val(Text1.Text) & "."
Else
Text1.Text = Val(Text1.Text)
End If
End Sub
'简单有效,可以试试。
一抹夏凉

2024-05-16 19:23:43

判断按键的asc码,(>=0 and <=9 ) or ="."就把该输入添加到文本框,否则屏蔽