tags:

views:

672

answers:

5

I have two text boxes. one for login id and another is for password. and there is one button (submit). I need an event that takes the values of login id and password. I.e, without clicking the mouse I need to invoke this event(just by hitting 'enter' on keyboard). can anybody help me!

thanx in advance. srini.

+14  A: 

Set the button to be the accept button on the form. You can do this by setting the forms "AcceptButton" property to be the button you want to trigger. This will make an enter key press trigger the button.

Simon P Stevens
can u help me where shud i set this.im new to c#,don be hesitated if im asking a naive one!!!
srinivas
It's a property on the form. Select your form. Hit F4. Find the "AcceptButton" property, change it to the button you want to be the default button. The button should be highlighted slightly (with a blue border on windows XP) to indicate it is the default button.
Simon P Stevens
Ganesh R.
Setting the button as the AcceptButton won't close the form. All it will do is cause the buttons click event to be triggered when you press enter.
Simon P Stevens
A: 

If you don't want the button to be the default you can hook both TextBoxes KeyPress events to same handler then check if Enter was pressed using KeyEventArgs.KeyCode.

Cedrik
A: 

If you need to be able to navigate the form without the mouse, you'll want to make sure your tab stops are set correctly. I would set Login Id as the first, password as the second, and your submit button as the third. Then the user can navigate easily among them. IIRC, the default behavior of buttons is to fire off the On_Click event if the Enter key is pressed while the button has focus.

AllenG
dude thats ok but when i hit enter the action shud invoke imean to saynot by focusing on submit and when i hit enter the action shud invoke
srinivas
and i would like to ask u ppl waht is the e.KeyChar eqivalent to the enter button
srinivas
+1  A: 

Hi, Simon P Stevens gave you the most adequate answer.

I would just add that you also have the possibility to set a form's CancelButton on which a click event would be triggered when you press the escape ESC key.

Quick summary

Enter --> AcceptButton

Esc --> CancelButton

elpipo
thanx dude.glad to see these kind of responses...very gud site!
srinivas
A: 

use

buttonName.PerformClick();

serhio