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

G

Guest

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
 
M

Michael Nemtsev

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 Nietzsch
 
G

Guest

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
 
G

Guest

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.
 
C

Cerebrus

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top