I need assistance with using Keypress to only allow a certain range of numbers (1-26) to be typed into the textbox. When ever I do it, it only allows me to press the numbers "1 and 2".

I'm very new to VB so the help is much obliged. I know Im doing something very stupid! :P

Public Class Form1

Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click


    Dim intMath As Integer
    Dim intReadingWriting As Integer
    Dim intScienceHistoy As Integer
    Dim intSum As Integer
    Dim intAverage As Integer


    intMath = Val(txtMath.Text)
    intReadingWriting = Val(txtReadingWriting.Text)
    intScienceHistoy = Val(txtScienceHistory.Text)

    intSum = intMath + intReadingWriting + intScienceHistoy

    intAverage = intSum / 3

    lblAnswer.Text = intAverage

End Sub


Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
    Close()
End Sub

Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
    lblAnswer.Text = String.Empty
    txtMath.Text = String.Empty
    txtReadingWriting.Text = String.Empty
    txtScienceHistory.Text = String.Empty

    txtMath.Focus()
End Sub

Private Sub txtMath_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtMath.KeyPress
    If (e.KeyChar >= "1" And e.KeyChar <= "26") Or e.KeyChar = ControlChars.Back Then
    Else


        e.Handled = True
    End If
End Sub

Private Sub txtReadingWriting_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtReadingWriting.KeyPress
    If (e.KeyChar >= "1" And e.KeyChar <= "26") Or e.KeyChar = ControlChars.Back Then
    Else

        e.Handled = True
    End If
End Sub

Private Sub txtScienceHistory_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtScienceHistory.KeyPress
    If (e.KeyChar >= "1" And e.KeyChar <= "26") Or e.KeyChar = ControlChars.Back Then
    Else
        e.Handled = True
    End If
End Sub

End Class

Have you tried this

If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso e.KeyChar <> ControlChars.Back Then
            'cancel keys
            e.Handled = True

End If

Thank you for your reply, roemerito!

Is there away to be able to just enter the numbers 1-26....thats the main problem I am struggling with.

Thank you!

Ok so what im trying to understand is that you one to enter the numbers:

1 to 26 or number 1 -minus number26(1-26)

Ok forget it i just got it, 1 to 26, no other numbers apart from those!!

ok let me check brb in a few minutes witht he anwser

Here it is anaconda

the "CommentsTextBox" is where im doing the keyPress event, you will need to change that to where your doing that!

If CommentsTextBox.Text <> "" Then

            Select Case (CommentsTextBox.Text)

                Case "1"
                    If (e.KeyChar < "0" OrElse e.KeyChar > "6") AndAlso e.KeyChar <> ControlChars.Back Then
                        'cancel keys
                        e.Handled = True
                    End If
                Case "2"
                    If (e.KeyChar < "0" OrElse e.KeyChar > "6") AndAlso e.KeyChar <> ControlChars.Back Then
                        'cancel keys
                        e.Handled = True
                    End If
                Case Else
                    If (e.KeyChar < "1" OrElse e.KeyChar > "2") AndAlso e.KeyChar <> ControlChars.Back Then

                        'cancel keys
                        e.Handled = True

                    Else

                    End If
            End Select


        End If

Enjoy!

Also limit the textboxes for those of MaximumLenght = 2, that will make it easier!

Thanks, roemerito!
I figured it out with your help here is my keypress for my math text box :)

Private Sub txtMath_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtMath.KeyPress
    If txtMath.Text <> "" Then

    End If

    Select Case (txtMath.Text)
        Case "1"
            If (e.KeyChar < "0" Or e.KeyChar > "9") AndAlso e.KeyChar <> ControlChars.Back Then
                e.Handled = True
            End If
        Case "2"
            If (e.KeyChar < "0" OrElse e.KeyChar > "6") AndAlso e.KeyChar <> ControlChars.Back Then
                e.Handled = True
            End If

        Case Else
            If (e.KeyChar < "1" And e.KeyChar > "2") AndAlso e.KeyChar <> ControlChars.Back Then
                e.Handled = True
            Else
            End If
    End Select
End Sub



    End If
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.