0

So basically in a form, you have 3 textboxes and each of them you can input a number, then you have a button that check if each of these textboxes are in a range of numbers specified. It's very much like a lock combo, but I need help checking values, for example; enter image description here

the only thing i can figure out is

Dim intOne As Integer
    Dim intTwo As Integer
    Dim intThree As Integer
    Dim blnInputOk As Boolean = True

    If Integer.TryParse(lblOne.Text, intOne) = False Then
        MessageBox.Show("Value must be an integer")
        blnInputOk = False
    End If

    If Integer.TryParse(lblTwo.Text, intTwo) = False Then
        MessageBox.Show("Value must be an integer")
        blnInputOk = False
    End If

    If Integer.TryParse(lblThree.Text, intThree) = False Then
        MessageBox.Show("Value must be an integer")
        blnInputOk = False
    End If

    If intOne >= 6 And intOne <= 8 Then
        If intTwo >= 2 And intOne <= 9 Then
            If intThree >= 0 And intThree <= 8 Then
                MessageBox.Show("Good code!")
            Else
                MessageBox.Show("Wrong, number must be between range 0 to 8")
            End If
        Else
            MessageBox.Show("Wrong, number must be between range 2 to 9")
        End If
    Else
        MessageBox.Show("Wrong, number must be between range 6 to 8")
    End If

So my question is how can you make this code simpler by adding an array for number range for each textbox? I also know there is a possibility of adding a loop, but i'm not sure how to structure it, can anyone help? thanks

3
  • As smead mentioned below, the NumericUpDown() control was made for this as you can specify Minimum() and Maximum() values. The Value() will always be within that range and is guaranteed to be an Integer. Commented Nov 22, 2013 at 0:21
  • @Idle_Mind Actually, the NumericUpDown uses Decimal type (Min, Max, Value and Increment are all Decimal). The DecimalPlaces property defaults to 0 so it is easy to think it is Integer, but it isnt. Commented Nov 22, 2013 at 21:08
  • @Plutonix, very true. It is effectively an "integer" with the default settings. Commented Nov 22, 2013 at 21:39

2 Answers 2

2

There are many ways, all depends on the number of values you are going to compare, the simplest is by adding a comparison function.

Private Function IsInRange(x As Integer, a As Integer, b As Integer) As Boolean
    Dim r As Boolean
    r = (x >= a And x <= b)
    If r Then
        MessageBox.Show("Good code!")
    Else
        MessageBox.Show(String.Format("Wrong, number {0} must be between range {1} to {2}", x, a, b))
    End If
    Return r
End Function

Then according to the code shown in your question, you can do this:

If IsInRange(intOne, 6, 8) Then
    If IsInRange(intTwo, 2, 9) Then
        IsInRange(intThree, 0, 8)
    End If
End If
Sign up to request clarification or add additional context in comments.

1 Comment

woah, the second might help actually! the second code you've shown, is it an array? it would really help if I can store the range values in arrays, is it possible to show your code with the number ranges? (for example the first one, the lower bound number which would be 6 and the higher bound number is 8)
1

.NET has input validation functionality on standard input controls. Here's a decent place to start.

1 Comment

Also, for integers you may as well just use NumericUpDown.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.