vb.net 中的 InputBox 数字验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21179990/
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
InputBox Numeric validation in vb.net
提问by Omkar
I've used inputbox in my vb.net2008 application to accept quantity for a particular product, I want to give numeric validation for this inputbox. The characters(a-z) must not be able to entered in this input box, the same that we do on a textbox_keypress event. Please help
我在我的 vb.net2008 应用程序中使用了输入框来接受特定产品的数量,我想为此输入框提供数字验证。字符(az) 不能在这个输入框中输入,就像我们在 textbox_keypress 事件中所做的一样。请帮忙
采纳答案by chris_techno25
Check this out...
看一下这个...
http://www.functionx.com/vb/functions/inputbox.htm
http://www.functionx.com/vb/functions/inputbox.htm
Just what I thought. It says the inputbox does not have filtering events that you can subscribe to. So my suggestion, you have 2 options... Either create your own input box and subscribe to the textbox keypress event to filter data or have the user enter data into the inputbox first and validate data after it's entered, if you insist on using the inputbox. These are your only options.
正是我想的。它说输入框没有您可以订阅的过滤事件。所以我的建议,你有 2 个选择......要么创建自己的输入框并订阅文本框按键事件来过滤数据,要么让用户首先将数据输入输入框并在输入后验证数据,如果你坚持使用输入框。这些是您唯一的选择。
回答by Haji
The default InputBox doesn't provide this validation functionality when enter the text. You'll have to create a window (input box) yourself and add the validation you want
输入文本时,默认的 InputBox 不提供此验证功能。您必须自己创建一个窗口(输入框)并添加所需的验证
The best option is to create a new Form with all validation for that
text box you want and show it with .ShowDialog()to be modal like the InputBox
最好的选择是创建一个新的表单,对您想要的文本框进行所有验证,并将其显示.ShowDialog()为像 InputBox 一样的模态
回答by Nalindar
I realize this has been answered already but there wasn't much given in way of example. I recently came across a similar issue attempting to validate an InputBox for only numeric input.
我意识到这已经得到了回答,但没有太多的例子。我最近遇到了一个类似的问题,试图验证仅用于数字输入的 InputBox。
This is what I came up with, in regards to a Rainfall Statistics gathering application.
这就是我想到的关于降雨统计收集应用程序的内容。
The loop is to collect rainfall amounts for each month. I am passing the user input first in to a string, then validating the string using IsNumeric before converting to a double and assigning it to the current index in the array.
该循环是收集每个月的降雨量。我首先将用户输入传递给一个字符串,然后在转换为双精度值并将其分配给数组中的当前索引之前使用 IsNumeric 验证该字符串。
If the input is numeric it increments the loop counter to move on to the next month.
如果输入是数字,它会增加循环计数器以移动到下个月。
If not numeric it displays an error and loops again to ask for the same month input.
如果不是数字,它会显示一个错误并再次循环以要求输入相同的月份。
Private Sub ...
Dim intMaxIndex As Integer = 11
Dim strMonths() As String = {"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"}
' declare local array and counter
Dim dblRainfallPerMonth(intMaxIndex) As Double
Dim i As Integer = 0
' loop through the months
Do Until i = intMaxIndex
Dim strRainfall As String = ""
' get user input from InputBox and assign to a string
strRainfall = InputBox("Enter the rainfall for " & strMonths(i))
' check if the user input is a number
If IsNumeric(strRainfall) Then
' validation passed, convert user input and assign to current index in the array
dblRainfallPerMonth(i) = CDbl(strRainfall)
' increment the counter
i += 1
Else
' validation failed, display error and do not increment the counter
MessageBox.Show("Please enter a number")
End If
Loop
End Sub

