|
-
Jan 15th, 2009, 12:35 PM
#1
Thread Starter
Hyperactive Member
[2008] keychar
I am trying to do an keypress event that only allows numbers, backspace, and enter. I have the part that only allows numbers
Code:
If Not (IsNumeric(e.KeyChar)) Then
e.Handled = True
End If
I need to know how to know what keychar the backspace and enter are. Can someone give me a reference?
-
Jan 15th, 2009, 01:01 PM
#2
Re: [2008] keychar
How about pressing them and trapping the value of e.KeyChar?
-
Jan 15th, 2009, 01:03 PM
#3
Re: [2008] keychar
Use the KeyDown event. That would give you more flexibility.
vb.net Code:
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown Select Case e.KeyCode Case Keys.D0 To Keys.D9, Keys.Enter, Keys.Back e.Handled = True End Select End Sub
-
Jan 15th, 2009, 01:05 PM
#4
Re: [2008] keychar
Or simply look them up in ASCII table (widely available on the Internet) 
Anyway, the ascii value for BS = 8, and enter = 13. So you need to "And" your conditions, something like this
Code:
Dim ch As Char = e.KeyChar
If Not IsNumeric(ch) AndAlso ch <> ChrW(8) AndAlso ch <> ChrW(13) Then
e.Handled = True
End If
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
Jan 15th, 2009, 01:06 PM
#5
Re: [2008] keychar
e.KeyChar will return ASCII value of that key so you can implement this for numeric value.
-
Jan 15th, 2009, 01:08 PM
#6
Re: [2008] keychar
Hi,
You can try this foe the enter Key:
vb Code:
' The keypressed method uses the KeyChar property to check ' whether the ENTER key is pressed. ' If the ENTER key is pressed, the Handled property is set to true, ' to indicate the event is handled. If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then e.Handled = True End If
For the Backspace use:
vb Code:
If e.KeyChar = Microsoft.VisualBasic.Chr(Keys.Back) Then MsgBox("You pressed the Backkey") End If
Wkr,
sparrow1
-
Jan 15th, 2009, 01:38 PM
#7
Thread Starter
Hyperactive Member
Re: [2008] keychar
Anothe quick questions what does this "<>" do I have seen it a few times but dont get it
edit: anothe question is how do I make it do an event when a key is pressed like this
Code:
If e.KeyChar <> ChrW(13) Then
btnEquals_Click()
End If
-
Jan 15th, 2009, 01:42 PM
#8
Re: [2008] keychar
This is new question create new post for it
-
Jan 15th, 2009, 02:21 PM
#9
Re: [2008] keychar
 Originally Posted by ngreenwood6
Anothe quick questions what does this "<>" do I have seen it a few times but dont get it
edit: anothe question is how do I make it do an event when a key is pressed like this
Code:
If e.KeyChar <> ChrW(13) Then
btnEquals_Click()
End If
'<>' is the equal not (the != in C#)
btnEquals.PerformClick() is the method you want.
-
Jan 15th, 2009, 03:08 PM
#10
Thread Starter
Hyperactive Member
Re: [2008] keychar
Thanks that worked great.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|