using enter as a tab

G

Guest

Hi!
I'm new to c# and I stumbled across this dillema during a project: can
anyone tell me how I can emulate the action af a Tab when enter is pressed? I
saw something in visual basic that looked like this:

"To allow the Enter key to function as a Tab, enter this code in the
KeyPress event of your text boxes:
Sub Text1_KeyPress (KeyAscii As Integer)
If KeyAscii = 13 Then 'Enter
SendKeys "{Tab}"
KeyAscii = 0
End If
End Sub
This allows the user to press Enter to move from one text box to the next.
This technique will not work if you have a command button on the form whose
default property is True. The button will respond to the Enter key before the
text box sees it."

but I have no idea how to implement it in c#
Thanks!
 
T

Tim Wilson

If you're looking to jump to the next control when the Enter key is pressed
while a specific TextBox has focus then you can use code similar to the code
below.

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if ((int)e.KeyChar == (int)Keys.Enter)
{
this.SelectNextControl((Control)sender, true, true, true, true);
}
}
 

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