检查文本框中输入的值是否介于其他两个文本框的值之间



我有三个文本框。如果在文本框3(数字)中输入的值介于文本框1(数字)和文本框2(数字)之间,或者介于文本框2和文本框1之间,则结果为正常,否则为不正常。

如何用VB.NET做到这一点?

我多年没有写过任何VB,但我认为你正在寻找的公式可以表示为:

Return (z >= x And z <= y) Or (z >= y And z <= x)

地点:

  • x为文本框1
  • 中的值。
  • y为文本框2
  • 中的值。
  • z为文本框3
  • 中的值

这是我在VB中处理这个问题的(生锈的)尝试:

Function IsOk(ByVal firstValue As String, ByVal secondValue As String, ByVal thirdValue As String) As String
If firstValue.IsInteger() And secondValue.IsInteger() And thirdValue.IsInteger() Then
Dim x As Integer = Integer.Parse(firstValue)
Dim y As Integer = Integer.Parse(secondValue)
Dim z As Integer = Integer.Parse(thirdValue)
If (z >= x And z <= y) Or (z >= y And z <= x) Then
Return "OK"
End If
End If

Return "Not OK"
End Function
Public Module MyExtensions
<System.Runtime.CompilerServices.Extension()> _
Public Function IsInteger(ByVal value As String) As Boolean
If String.IsNullOrEmpty(value) Then
Return False
Else
Return Integer.TryParse(value, Nothing)
End If
End Function
End Module

做事总是不止一种方式。上述代码示例的简化版本返回一个布尔值,如下所示

Function IsOk(firstValue As String, secondValue As String, thisValue As String) As Boolean
If IsNumeric(firstValue) And IsNumeric(secondValue) And IsNumeric(thisValue) Then
Dim x As Integer = firstValue
Dim y As Integer = secondValue
Dim z As Integer = thisValue
If (z >= x And z <= y) Or (z >= y And z <= x) Then
Return True
End If
End If
Return False
End Function

相关内容

最新更新