Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to check the key pressed in vb.net 1

Status
Not open for further replies.

JawwadLone

Programmer
Mar 17, 2004
57
PK
How can we check the ascii code of the key press in vb.net. I have tried this code

Private Sub txtRNo_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtRNo.KeyPress
If e.KeyChar = "A" Then
MsgBox("A is pressed")
End If
End Sub

But I do not know how can i check the range of values in keypress or keydown event. Like we can do in vb 6.0

If keyascii>=65 and keyascii<=90 then
Msgbox("Value between A to Z")
End If

Kindly provide me any help if possible. I shall be very thankfull to you.

Regards,

Jawwad Rashid Lone.
 
Thanks to both of you. your code was helpfull to me. But i have used unicode in my software so i have solved my problem like this.

Code:
Private Sub txtName_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtName.KeyDown
        nonNumberEntered = False
        ' Determine whether the keystroke is a number from the top of the keyboard.
        If e.KeyCode < Keys.A OrElse e.KeyCode > Keys.Z Then
            ' Determine whether the keystroke is a number from the keypad.
            If e.KeyCode < Keys.NumPad0 OrElse e.KeyCode > Keys.NumPad9 Then
                ' Determine whether the keystroke is a backspace.
                If e.KeyCode <> Keys.Back Then
                    ' A non-numerical keystroke was pressed. 
                    ' Set the flag to true and evaluate in KeyPress event.
                    nonNumberEntered = True
                End If
            End If
        End If
    End Sub

    Private Sub txtName_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtName.KeyPress

        ' Check for the flag being set in the KeyDown event.
        If nonNumberEntered = True Then
            ' Stop the character from being entered into the control since it is non-numerical.
            e.Handled = True
        End If
    End Sub

Thanks for your help once again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top