problem with handling 'Enter Key' event

ashgopal2000

Member
Joined
Jun 24, 2005
Messages
12
Programming Experience
1-3
Hi guys,

Three is a requirement to select a particular button when the user presses the enter key in a textbox(s). For e.g., if a user presses 'Enter' button in Phone Field, Bill round or ClerkID textboxes, the cmdFind button should be automatically clicked, etc.

I try to do this by using the keydown event....

private sub txtClerkID_keydown(....)
if e.keycode = keys.enter
cmdfind_click(sender,eventargs)
end if
end sub

However, when the user presses the 'Enter' button, it does not enter into the above procedure at all! It directly clicks a button, whose tabindex value is the least!!!

Is there any way to work around the problem? The user needs to navigate with tab.

Thanks.
 
A form has an AcceptButton property which when set to a button will cause that button's click event to be fired when the Enter key is pressed (and the CancelButton property for the escape key). If you want the button to be pressed anytime the Enter key is pressed, set the form's AcceptButton property accordingly.

It seems that you haven't shown the entire method, but have you included the Handles clause?:
VB.NET:
Private Sub txtClerkID_keydown(ByVal sender As Object, _
  ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtClerkID.KeyDown
If you do, the method should be executed when the user presses the Enter key while in the textbox.
 
re: problem with handling Enter key event

Hi Paszt,

Thanks for the reply!!

Actually, there are different buttons that have to be selected based on the textbox that the focus is in. So, if the above needs to work we will have to set/unset accept buttons by handling events.

However, the simplest soln is to set a textbox field with tabindex 1!!!!...or the least value. In this case the usual code works great.
 
this would work if there is only 1 textbox calling 1 button and that button never changes

ie txtClerkID will ONLY call the btnFind button

VB.NET:
Private Sub txtClerkID_KeyUp(....) Handles txtClerkID.KeyUp
  If e.KeyValue = 13 Then btnFind.PerformClick() '13 = Enter Key
End Sub
 
Back
Top