vb.net 你如何检查输入是否为VB中的负数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26805094/
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 do you check if an input is a negative number in VB
提问by user2970059
I am trying to do some validation which checks if the value in a textbox is an integer then checks if the the value is negative. It correctly checks if the value is an integer but I can't get it to check if the value is negative.
我正在尝试进行一些验证,检查文本框中的值是否为整数,然后检查该值是否为负。它正确检查该值是否为整数,但我无法检查该值是否为负。
Note: The value being entered is the number of competitions attended so comps = competition etc...
注意:输入的值是参加的比赛次数,因此 comps = 比赛等...
Dim comps As Integer
Dim value As Double
If Integer.TryParse(txtCompsEntered.Text, integer) Then
value = txtCompsEntered.Text
If value < 0 Then
lblcompsatten.ForeColor = Color.Red
txtCompsEntered.ForeColor = Color.Red
lblcompsatten.Text = "No negative numbers"
Else
lblcompsatten.ForeColor = Color.Black
txtCompsEntered.ForeColor = Color.Black
lblcompsatten.Text = ""
End If
lblcompsatten.ForeColor = Color.Black
txtCompsEntered.ForeColor = Color.Black
lblcompsatten.Text = ""
Else
lblcompsatten.ForeColor = Color.Red
txtCompsEntered.ForeColor = Color.Red
lblcompsatten.Text = "Not a number"
End If
I have already looked at this thread but it didn't seem to work how-to-check-for-negative-values-in-text-box-in-vb
我已经看过这个线程,但它似乎不起作用 how-to-check-for-negative-values-in-text-box-in-vb
回答by deloaf
There are a couple of things off with your code but the main issue is using Integer.TryParse incorrectly.
您的代码有一些问题,但主要问题是错误地使用 Integer.TryParse。
Incorrect:
不正确:
Dim value As Double
If Integer.TryParse(txtCompsEntered.Text, integer) Then
value = txtCompsEntered.Text
If value < 0 Then
Correct:
正确的:
Dim value As Integer
If Integer.TryParse(txtCompsEntered.Text, value) Then
If value < 0 Then
The things to note are that Integer.TryParse will return a boolean value (true if the value can be convertan integer, false if not). It will them dump the converted value into the second parameter you pass into it. In your case, you had 'integer', which is incorrect. You should be passing in a variable and then using that variable for your comparison.
需要注意的是 Integer.TryParse 将返回一个布尔值(如果该值可以转换为整数则为真,否则为假)。它们会将转换后的值转储到您传递给它的第二个参数中。在您的情况下,您有“整数”,这是不正确的。您应该传入一个变量,然后使用该变量进行比较。
Also, be careful with your types. You have 'value' as a double when you seem to be working with integers.
另外,请注意您的类型。当您似乎在使用整数时,您将“价值”作为双精度值。
回答by maxedev
Tryparse will convert the input to an integer if it succeeds - you don't need both the comps and value variables. Here's an example of how it works:
如果成功,Tryparse 会将输入转换为整数 - 您不需要 comps 和 value 变量。这是它如何工作的示例:
Dim comps As Integer
Dim input As String = "im not an integer"
Dim input2 As String = "2"
'tryparse fails, doesn't get into comps < 0 comparison
If Integer.TryParse(input, comps) Then
If comps < 0 Then
'do something
End If
Else
'I'm not an integer!
End If
'tryparse works, goes into comps < 0 comparison
If Integer.TryParse(input2, comps) Then
If comps < 0 Then
'do something
End If
End If
回答by Stefan ?or?evi?
Maybe try this?
也许试试这个?
If myinteger.toString.Contains("-") Then
'it's negative
Else
'it isn't
End If
Or even simplier
或者更简单
If myinteger < 0 Then
'it's not negative
Else
'it is negative
End if

