vb中文本框不接受非数字输入该如何操作

2025-02-26 16:59:00
推荐回答(3个)
回答1:

Dim str$

str = TextBox1.Text

For i = 1 To Len(str)

    If IsNumeric(Right(str, 1)) = False Then

        MsgBox "你输入了非数字文本,请重新输入!"

        TextBox1.Text = Left(str, Len(str) - 1)

        TextBox1.SetFocus

        Exit Sub

    End If

Next

回答2:

如果直接拒绝非数字输入就下面方法,KeyAscii 赋予 0 会忽略输入字符。
Private Sub Text1_KeyPress(KeyAscii As Integer)
If Chr(KeyAscii) Like "[!0-9]" And KeyAscii <> 8 Then
KeyAscii = 0
Beep
End If
End Sub

回答3:

在文本框的change事件里面,用isnumeric这个函数判断