string 如何在 Visual Basic 中将文本框中的字符串检查为整数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13413142/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 01:43:06  来源:igfitidea点击:

How can I check string in textbox as integer in Visual Basic?

vb.netstringtextbox

提问by Alex

I have 3 textboxes (day, month, year) and I want to check if input is e.g. day has to be from 1 to 31 and so on.

我有 3 个文本框(日、月、年),我想检查输入是否是例如日必须是从 1 到 31 等等。

My code is:

我的代码是:

If InputDan.Text < "1" Or InputDan > "31" Then Warning.Text = "Not a valid day input." Else Warning.Text = ""

Also I have day and month input limited to 2 characters and year to 4. It works fine with numbers from 10 to 31 and it properly puts an warning message when input is 0 or 32 and on.

此外,我的日期和月份输入限制为 2 个字符,年份限制为 4。它适用于 10 到 31 之间的数字,并且在输入为 0 或 32 时正确地显示警告消息。

Here's the problem...

问题来了……

When I put in numbers from 4 to 9 it puts on a warning message, as I figured out later that program considers empty space after one character input as 0. So if I enter 4 the program will read it as 40, and so on.

当我输入从 4 到 9 的数字时,它会显示一条警告消息,正如我后来发现的那样,该程序将输入一个字符后的空格视为 0。因此,如果我输入 4,程序会将其读取为 40,依此类推。

Can I solve this problem with converting String input as Int somehow?

我可以通过某种方式将 String 输入转换为 Int 来解决这个问题吗?

回答by Tim Schmelter

You need to parse the numbers to integer before you can compare them, otherwise >"11"will compare them alphabetically and not by their numerical order.

您需要先将数字解析为整数,然后才能比较它们,否则>"11"将按字母顺序而不是按数字顺序进行比较。

Dim day As Integer
Dim valid As Boolean = Int32.TryParse(InputDan.Text, day)

Now you know if that input was a correct number and you could show a warning if it was not.

现在您知道该输入是否是正确的数字,如果不是,您可以显示警告。

I would suggest a different approach to check whether or not the input was a correct day since you must take the number of days in that month into account(also leap years, different calendars etc). So use the current culture's calendarand look if the number of days is correct for the given month in this way:

我建议采用不同的方法来检查输入是否正确,因为您必须考虑该月的天数(还有闰年、不同的日历等)。因此,使用当前文化的日历并以这种方式查看给定月份的天数是否正确:

Dim daysInMonth = CultureInfo.CurrentCulture.Calendar.GetDaysInMonth(year, month)
If day > daysInMonth OrElse day < 1 Then
    ' show warning '
End If

(assuming you have already checked the year and month part with Int32.TryParse)

(假设您已经检查了年和月部分Int32.TryParse

回答by Nathan

Better than doing this from the code behind, asp.net has already validations here is an example of a textbox that represents the day, and it has to be between 1 and 31:

比从后面的代码做这个更好,asp.net 已经验证了这里是一个文本框的例子,它代表了一天,它必须在 1 到 31 之间:

<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>

<asp:RangeValidator ID="RangeValidator1" runat="server" 
    ErrorMessage="You have to insert a valid day" ControlToValidate="TextBox2" MaximumValue="31" MinimumValue="1"></asp:RangeValidator>

check it out

一探究竟

回答by igrimpe

Your problem is, that "9" (the string) IS "larger" than "31". Because sorting is done on the first char, then the second and so on.

你的问题是,“9”(字符串)比“31”“大”。因为排序是在第一个字符上完成的,然后是第二个,依此类推。

    Dim Value As Integer
    ' is there an (integer) NUMBER in the textbox?
    If Integer.TryParse(InputDan.Text, Value) Then
        If Value > 0 AndAlso Value < 31 Then
            ' do something
        Else
            MessageBox.Show("please enter a number!")
        End If
    Else
        MessageBox.Show("please enter a number!")
    End If

With "TryParse" you can test if a String can be converted to an Integer (or Double, Single, whatever implements a TryParse method) and if it can be converted, the value is stored in the second parameter.

使用“TryParse”,您可以测试字符串是否可以转换为整数(或双精度、单精度、任何实现 TryParse 方法的方法),如果可以转换,则该值存储在第二个参数中。

回答by Christian Sauer

You should use Strict on" to avoid the coding problems - basically you are comparing strings against each other. They do NOT behave like Integers for comparisons.

你应该使用 Strict on" 来避免编码问题——基本上你是在比较字符串。它们的行为不像整数进行比较。

回答by NeverHopeless

Try like this: (Assuming framework above/or 3.5)

试试这样:(假设框架高于/或 3.5)

If Not IsNumeric(InputDan.Text) OrElse _
   Not Enumerable.Range(1, 31).Contains(CInt(InputDan.Text)) Then
    Warning.Text = "Not a valid day input."
Else
    Warning.Text = ""
End If

It will first validate the input must be a number and then will validate if it lies within range of 1 and 31. I assume Days can not be 1.5so I called CInt.

它将首先验证输入必须是一个数字,然后验证它是否在 1 和 31 的范围内。我认为 Days 不能,1.5所以我称之为CInt

OrElseis what we call ShortCircuit. The second condition will not evaluate if the first one failed.

OrElse就是我们所说的ShortCircuit。如果第一个条件失败,则不会评估第二个条件。