All you need to do is copy and past the segment of code below into the keypress event of the control.
If Asc(e.KeyChar) = Keys.Enter Then 'Do something here
e.Handled = True End If
That's it, I hope you find it useful...
Comments: Flat Nested Threaded Embedded Oldest First Newest First trap enter key on keypress event of datagrid in vb.net. By pawan on Tuesday, April 26, 2005 (CST) I see your code use this code but not work on enter key. if i press shift+enter then this work.but i want to use only enter key. so plz give some code + gide for this work. thank you. pawan Reply to this Comment Re: Trap Enter Key By tkey on Wednesday, June 22, 2005 (CST) I am not sure I understand what you are asking here. This is the way it works for me when I test this code. I put this code in the KeyPress event of a text box and no matter if I hit enter or shift enter I am still able to capture the Enter Key. The only difference to this is if you have set a default Accept Button on your form which will override the code you put in the KeyPress. Am I missing what you were asking or does this answer your question? Tim Reply to this Comment Asc() By Collinsauve on Wednesday, July 02, 2008 (CST) Your method uses the Asc() Function, which is contained in Microsoft.VisualBasic and is therefore unmanaged code... Do you know the managed equivilant I am looking for it... Reply to this Comment Re: Asc() By tkey on Wednesday, July 02, 2008 (CST) Try this one on the KeyDown event Private Sub Button1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Button1.KeyDown If e.KeyCode = Keys.Enter Then 'Do something e.Handled = True End If End Sub Reply to this Comment
I see your code use this code but not work on enter key.
if i press shift+enter then this work.but
i want to use only enter key.
so plz give some code + gide for this work.
thank you.
pawan
Reply to this Comment
I am not sure I understand what you are asking here. This is the way it works for me when I test this code.
I put this code in the KeyPress event of a text box and no matter if I hit enter or shift enter I am still able to capture the Enter Key.
The only difference to this is if you have set a default Accept Button on your form which will override the code you put in the KeyPress.
Am I missing what you were asking or does this answer your question?
Tim