I'm trying to validate my textbox so that the user must enter a Table Number between 1 and 20. The click event of the exit button checks this and if invalid input, displays a message box. It works for all numbers except 3-9 where it displays the warning even though it is in range. Anyone see where I'm going wrong?

Public Class Form1

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        If (txtTableNo.Text >= "1" And txtTableNo.Text <= "20") Then
            Me.Close()
        Else
            MessageBox.Show("Table Number must be between 1-20", "Warning")

        End If

    End Sub

    Private Sub txtTableNo_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtTableNo.KeyPress
        If (e.KeyChar < "0" OrElse e.KeyChar > "9") _
            AndAlso e.KeyChar <> ControlChars.Back Then
            e.KeyChar = ""
            Beep()

        End If
    End Sub

End Class

Changed:

If (txtTableNo.Text >= "1" And txtTableNo.Text <= "20") Then

To

If (txtTableNo.Text >= 1 And txtTableNo.Text <= 20) Then

Works fine that way.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.