我想使用NumericUpDown创建一个If语句,以便每当该值小于阈值时,NumericUpDown ForeColor将变为红色,否则变为黑色。
例如:我将阈值设置为3。我的问题是,当NumericUpDown达到10-30时,ForeColor变为红色。10-30大于3,那么为什么会发生这种情况?
Private Sub Txt_item_quantity_ValueChanged(sender As Object, e As EventArgs) Handles txt_item_quantity.ValueChanged
If numericupdown1.value.toString <= lbl_item_threshold.Text Then
numericupdown1.ForeColor = Color.Red
'ToolTip1.Active = 1
Else
numericupdown1.ForeColor = Color.Black
'ToolTip1.Active = 0
End If
End Sub发布于 2019-07-17 22:26:13
您正在IF中进行字符串比较。字符串比较逐个字符比较所以10,如果你看第一个字符1小于3。转换成数字来比较数值。
发布于 2019-07-17 22:39:41
进行这样的比较,其中比较整数值而不是字符串值。
If numericupdown1.value <= CInt(lbl_item_threshold.Text) Then发布于 2019-07-18 22:35:58
@RobertBaron提供了正确的答案。我张贴这篇文章是为了详细解释“为什么”。字符串按字母顺序排序,整数按数字顺序排序。为了演示
Private Sub TestSort()
Dim StringArray() As String = {"1", "2", "30", "10", "4"}
Dim IntegerArray() As Integer = {1, 2, 30, 10, 4}
Array.Sort(StringArray)
Array.Sort(IntegerArray)
For Each s In StringArray
Debug.Print(s)
Next
'Result
'1
'10
'2
'30
'4
For Each i In IntegerArray
Debug.Print(i.ToString)
Next
'Result
'1
'2
'4
'10
'30
End Sub正如您所看到的,在处理字符串时,4大于30,但当使用整数4时,4小于30 -这是预期的结果。
https://stackoverflow.com/questions/57078148
复制相似问题