vb.net 如何让 InputBox 只接受数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29195423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I make InputBox only accept numbers
提问by RossCol
The code is just asking for the pass rates for 6 different colleges. The rate must be a value between 1 and 100.
代码只是询问 6 所不同大学的通过率。比率必须是 1 到 100 之间的值。
I would like it to only allow numbers. I have tried using IsNumeric, but I must not be using it correctly. Here's what I have so far:
我希望它只允许数字。我曾尝试使用IsNumeric,但我一定没有正确使用它。这是我到目前为止所拥有的:
For i As Integer = 0 To 5
passRate(i) = InputBox("Enter the pass rate for " & colleges(i) & " college")
While passRate(i) > 100 Or passRate(i) < 0
MsgBox("Error must be a number and be between 0 and 100")
passRate(i) = InputBox("Enter the pass rate for " & colleges(i) & " college")
End While
Next i
回答by ashkufaraz
use this function for check value
使用此函数检查值
Private Function IsInputNumeric(input As String) As Boolean
If String.IsNullOrWhiteSpace(input) Then Return False
If IsNumeric(input) Then Return True
Dim parts() As String = input.Split("/"c)
If parts.Length <> 2 Then Return False
Return IsNumeric(parts(0)) AndAlso IsNumeric(parts(1))
End Function
and use like this
并像这样使用
For i As Integer = 0 To 5
passRate(i) = InputBox("Enter the pass rate for " & colleges(i) & " college")
If IsInputNumeric(passRate(i)) Then
'handle numeric input
Else
'handle not a number
End If
While passRate(i) > 100 Or passRate(i) < 0
MsgBox("Error must be a number and be between 0 and 100")
passRate(i) = InputBox("Enter the pass rate for " & colleges(i) & " college")
End While
回答by JStevens
I see a couple of things to note.
我看到有几点需要注意。
- IsNumeric does not test for just an integer. It will return true for "1.5" since it is numeric. I am assuming that you are looking for an integer value of 1 to 100 as an input result.
- An input box returns an string. Some form of parsing (implicit or explicit) must happen if you are going to compare it to an integer type.
- IsNumeric 不只测试一个整数。它将为“1.5”返回 true,因为它是数字。我假设您正在寻找 1 到 100 的整数值作为输入结果。
- 输入框返回一个字符串。如果要将其与整数类型进行比较,则必须进行某种形式的解析(隐式或显式)。
To avoid that conversion you could use a simple RegEx. This one will validate it to be an integer between 1 and 100:
为了避免这种转换,您可以使用一个简单的 RegEx。这将验证它是 1 到 100 之间的整数:
Function IsValid(Byval inputString as String) as Boolean
Dim validValues As New Regex("^[1-9]?[0-9]{1}$|^100$")
Return validValues.IsMatch(inputString )
End Function
Admitily RegEx can be a bit much... So here is another way to check that the input string will cast to an integer and if so is it between 1 and 100 in value.
诚然,正则表达式可能有点多......所以这里有另一种方法来检查输入字符串是否会转换为整数,如果是的话,它的值在 1 到 100 之间。
Function IsValid(Byval inputString as String) as Boolean
Dim result As Boolean = False
Dim inputAsInteger as Integer = 0
If Integer.TryParse(value, inputAsInteger) Then
result = inputAsInteger > 0 And inputAsInteger <= 100)
End If
Return result
End Function
You can call either of these like so:
您可以像这样调用其中任何一个:
Dim validInput as Boolean = False
While Not validInput
validInput = IsValid(InputBox("Enter the pass rate for " & colleges(i) & " college")
Loop
回答by Rick FlyFish
Private Sub txbDwellTime_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txbDwellTime.KeyPress
pNumDecOnly(e)
'txbDwellTime.Text = ""
End Sub
Public Sub pNumDecOnly(ByVal e As System.Windows.Forms.KeyPressEventArgs)
If (Asc(e.KeyChar) >= 48 And Asc(e.KeyChar) <= 57 Or Asc(e.KeyChar) = 46) Then
'good job do nothing we only allow positive Decimal numbers in this field
'Asc(e.KeyChar) 48 Through 57 i.e. 0 through 9 Or Asc(e.KeyChar) 46 (dot= .)
Else
e.Handled = True
MsgBox("Only Positive Decimal Numbers Allowed this field")
End If
End Sub
This will allow only positive Decimal numbers using the ASCII Table, you should be able to modify this for your own use
这将只允许使用 ASCII 表的正十进制数,您应该能够修改它以供您自己使用

