Question how do i validate textbox input against drop down list value range

beegee

Member
Joined
Nov 26, 2012
Messages
6
Programming Experience
1-3
i have a drop down list with weight range values i.e 1-50 or 51-70 and so forth and i have a textbox that the user enters a value for the weight according to the weight range. i want the user to only enter a value within this range and how do i validate for this
 
Hi,

You can use the TextBox.Validating event to check this. Have a look here:-

VB.NET:
Private Sub TextBox1_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
  If ComboBox1.SelectedIndex > -1 Then
    Dim SplitValues() As String = ComboBox1.SelectedItem.ToString.Split("-"c)
    Dim lowerValue As Integer = Convert.ToInt32(SplitValues(0))
    Dim upperValue As Integer = Convert.ToInt32(SplitValues(1))
    Dim TestValue As Integer
 
    If Integer.TryParse(TextBox1.Text, TestValue) Then
      If TestValue < lowerValue Or TestValue > upperValue Then
        MsgBox("Please enter a Number in the Valid Range Selected!")
        e.Cancel = True
      End If
    Else
      MsgBox("Please enter a Valid Number for the Weight!")
      e.Cancel = True
    End If
  End If
End Sub

Hope that helps.

Cheers,

Ian
 
many thanks for the response. however i am getting a format exception on this line

Dim lowerValue As Integer = Convert.ToInt32(SplitValues(0))

that says input string was not in a correct format. my weight range selected is 2-85

Hi,

You can use the TextBox.Validating event to check this. Have a look here:-

VB.NET:
Private Sub TextBox1_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
  If ComboBox1.SelectedIndex > -1 Then
    Dim SplitValues() As String = ComboBox1.SelectedItem.ToString.Split("-"c)
    Dim lowerValue As Integer = Convert.ToInt32(SplitValues(0))
    Dim upperValue As Integer = Convert.ToInt32(SplitValues(1))
    Dim TestValue As Integer
 
    If Integer.TryParse(TextBox1.Text, TestValue) Then
      If TestValue < lowerValue Or TestValue > upperValue Then
        MsgBox("Please enter a Number in the Valid Range Selected!")
        e.Cancel = True
      End If
    Else
      MsgBox("Please enter a Valid Number for the Weight!")
      e.Cancel = True
    End If
  End If
End Sub

Hope that helps.

Cheers,

Ian
 
Hi,

Depending on how you have populated the ComboBox you may get this error. I should have thought of this first.

Use this statement instead and all should work fine:-

ComboBox1.Text.ToString.Split("-"c")

Cheers,

Ian
 
thanks...yes it has validated...if i put value outside the range it displays the msgbox but if i put correct value on this line

dim uppervalue as integer

it displays an index out of range exception saying index was outside the bounds of the array

Hi,

Depending on how you have populated the ComboBox you may get this error. I should have thought of this first.

Use this statement instead and all should work fine:-

ComboBox1.Text.ToString.Split("-"c")

Cheers,

Ian
 
OK then, I suggest that you put a message box as your first statement in the validating event to see what the value of ComboBox.Text is. This should be 2 numbers seperated by a hyphen. i.e 1-50. If not, then you will get an "index out of range error"

Cheers,

Ian
 
Back
Top