469,627 Members | 936 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,627 developers. It's quick & easy.

Keystrokes..............

Hello. I have managed to consume the key 9 when it is pressed,
by using the following code:

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

'If key 9 on keyboard is pressed show this in a messagebox
'and then consume the stroke using e.handled=true

If e.KeyChar = ChrW(57) Then
MessageBox.Show("hello", "57", MessageBoxButtons.OK,
MessageBoxIcon.Information)
e.Handled = True
End If
End Sub

However the above code will not consume the use of the "Backspace" key. That
code looks like the following:

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

'If Backspace key on keyboard is pressed show this in a messagebox
'and then consume the stroke using e.handled=true

If e.KeyChar = ChrW(8) Then
MessageBox.Show("hello", "8 Backspace", MessageBoxButtons.OK,
MessageBoxIcon.Information)
e.Handled = True
End If
End Sub

Anyone happen to know why??????

Thank you,

Brenton Garman

Apr 4 '06 #1
4 1220
Hello Brenton,

Backspace is Keys.Back

if (e.KeyCode == Keys.Back)
{
....
}

BG> Hello. I have managed to consume the key 9 when it is pressed, by
BG> using the following code:
BG>
BG> Private Sub RichTextBox1_KeyPress(ByVal sender As Object, ByVal e As
BG> System.Windows.Forms.KeyPressEventArgs) Handles
BG> RichTextBox1.KeyPress
BG>
BG> 'If key 9 on keyboard is pressed show this in a messagebox
BG> 'and then consume the stroke using e.handled=true
BG> If e.KeyChar = ChrW(57) Then
BG> MessageBox.Show("hello", "57", MessageBoxButtons.OK,
BG> MessageBoxIcon.Information)
BG> e.Handled = True
BG> End If
BG> End Sub
BG> However the above code will not consume the use of the "Backspace"
BG> key. That code looks like the following:
BG>
BG> Private Sub RichTextBox1_KeyPress(ByVal sender As Object, ByVal e As
BG> System.Windows.Forms.KeyPressEventArgs) Handles
BG> RichTextBox1.KeyPress
BG>
BG> 'If Backspace key on keyboard is pressed show this in a
BG> messagebox
BG> 'and then consume the stroke using e.handled=true
BG> If e.KeyChar = ChrW(8) Then
BG> MessageBox.Show("hello", "8 Backspace",
BG> MessageBoxButtons.OK,
BG> MessageBoxIcon.Information)
BG> e.Handled = True
BG> End If
BG> End Sub
BG> Anyone happen to know why??????
BG>
BG> Thank you,
BG>
BG> Brenton Garman
BG>
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche

Apr 4 '06 #2
> However the above code will not consume the use of the "Backspace" key. That
code looks like the following:


I think you need to handle KeyDown rather than KeyPress to consume some of
the keys. I sympathize as the documentation is very fuzzy about keystroke
handling. What works for me in most settings is a form with KeyPreview true
and this event handler:

Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) _
Handles MyBase.KeyDown
' KeyDown event handling goes here
End Sub

Apr 4 '06 #3
Thank you Gentlemen Both of you! AMercer I used your code and suggestion and
part
of Mr.Nemstev's code and got the app to work. The following code will
consume both
Del and Bs keys, but allows all numeric and letters to be used.

Private Sub frmGuitarWorks_KeyDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) _
Handles MyBase.KeyDown

e.Handled = True

End Sub

That is it! Awesome! Thank you Both! Now I can further tailor the code to my
needs!
Very Grateful! Oh, ya, also set the form's KeyPreview Property to true.

"AMercer" wrote:
However the above code will not consume the use of the "Backspace" key. That
code looks like the following:


I think you need to handle KeyDown rather than KeyPress to consume some of
the keys. I sympathize as the documentation is very fuzzy about keystroke
handling. What works for me in most settings is a form with KeyPreview true
and this event handler:

Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) _
Handles MyBase.KeyDown
' KeyDown event handling goes here
End Sub

Apr 4 '06 #4
Hi,

AMercer is right.

The KeyPress event cannot be used to trap values that cannot be seen in
a TextBox, for instance. These and all other keys such as the Control
keys, arrow keys, Function keys, backspace etc. will only trigger the
KeyDown event, not KeyPress.

So, you might consider coding only the KeyDown event in your
application.

Regards,

Cerebrus.

Apr 4 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Olli Piepponen | last post: by
5 posts views Thread by Lucas Tam | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.