Trap "Enter" key press
-
hey, i tried a new way to trap the enter key press event, so it dose not matter what control on the form has focus the enter key will be trapped, can someone correct me please <pre>Private Shadows Sub KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress If Asc(e.KeyChar) = Keys.Enter Then MsgBox("YES!") e.Handled = True End If End Sub</pre>
J.Hardy
-
hey, i tried a new way to trap the enter key press event, so it dose not matter what control on the form has focus the enter key will be trapped, can someone correct me please <pre>Private Shadows Sub KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress If Asc(e.KeyChar) = Keys.Enter Then MsgBox("YES!") e.Handled = True End If End Sub</pre>
J.Hardy
Hi, you should use the KeyDown event, it offers KeyCode which directly compares to Keys.Enter without needing the ASC function; then set SuppressKeyPress when you want to ignore the key. BTW: you managed to use PRE tags the wrong way! :)
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
-
hey, i tried a new way to trap the enter key press event, so it dose not matter what control on the form has focus the enter key will be trapped, can someone correct me please <pre>Private Shadows Sub KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress If Asc(e.KeyChar) = Keys.Enter Then MsgBox("YES!") e.Handled = True End If End Sub</pre>
J.Hardy
-
hey, i tried a new way to trap the enter key press event, so it dose not matter what control on the form has focus the enter key will be trapped, can someone correct me please <pre>Private Shadows Sub KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress If Asc(e.KeyChar) = Keys.Enter Then MsgBox("YES!") e.Handled = True End If End Sub</pre>
J.Hardy
You can also set the form's KeyPreview property to True, then handle the form's KeyDown or KeyPress events to do the same thing.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008 -
hey, i tried a new way to trap the enter key press event, so it dose not matter what control on the form has focus the enter key will be trapped, can someone correct me please <pre>Private Shadows Sub KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress If Asc(e.KeyChar) = Keys.Enter Then MsgBox("YES!") e.Handled = True End If End Sub</pre>
J.Hardy
I set KeyPreview = true in the form's properties dialog & then trap keydown Private Sub frmInvoice_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown If e.KeyData = Keys.Enter Then 'bla, bla, bla e.Handled = True End If End Sub
-
hey, i tried a new way to trap the enter key press event, so it dose not matter what control on the form has focus the enter key will be trapped, can someone correct me please <pre>Private Shadows Sub KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress If Asc(e.KeyChar) = Keys.Enter Then MsgBox("YES!") e.Handled = True End If End Sub</pre>
J.Hardy
Set the KeyPreview property of the form to True , that would solve your problem
-Regards Bharat Jain bharat.jain.nagpur@gmail.com
-
Set the KeyPreview property of the form to True , that would solve your problem
-Regards Bharat Jain bharat.jain.nagpur@gmail.com
hey guy thanks for all your help :)
J.Hardy