Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Enter on a TextBox act as TAB button

Tags:

I've several textboxes. I would like to make the Enter button act as Tab. So that when I will be in one textbox, pressing Enter will move me to the next one. Could you please tell me how to implement this approach without adding any code inside textbox class (no override and so on if possible)?

like image 869
Tom Smykowski Avatar asked Dec 16 '08 13:12

Tom Smykowski


People also ask

How do you make the Enter key work like a tab?

Map [Enter] key to work like the [Tab] key This caputures both Enter and Shift + Enter .

Why is my Enter button tabbing?

If the field you've selected has a dropdown list then CLICKING ON IT ONCE will bring the list down but not place the cursor in the field. Selecting the field a second time will place the cursor in the field. Once the cursor is actually visible in the field you will find that the TAB key works as you expect.


2 Answers

Here is the code that I usually use. It must be on KeyDown event.

if (e.KeyData == Keys.Enter) {     e.SuppressKeyPress = true;     SelectNextControl(ActiveControl, true, true, true, true); } 

UPDATE

Other way is sending "TAB" key! And overriding the method make it so easier :)

protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {                 if (keyData == (Keys.Enter))     {         SendKeys.Send("{TAB}");     }      return base.ProcessCmdKey(ref msg, keyData); } 
like image 183
Behzad Avatar answered Sep 18 '22 07:09

Behzad


You can write on the keyDown of any control:

        if (e.KeyCode == Keys.Enter)         {              if (this.GetNextControl(ActiveControl, true) != null)             {                 e.Handled = true;                 this.GetNextControl(ActiveControl, true).Focus();              }         } 

GetNextControl doesn't work on Vista.

To make it work with Vista you will need to use the code below to replace the this.GetNextControl...:

System.Windows.Forms.SendKeys.Send("{TAB}"); 
like image 33
Patrick Desjardins Avatar answered Sep 21 '22 07:09

Patrick Desjardins