Serialcoder en Français Serialcoder in English
TEL : +33 (0)9 72 13 15 17

Windows Forms FAQ resources

5. Windows Forms Datagrid

5.65 How can I make the Enter Key behave like the Tab Key and move to the next cell?


You can override ProcessCmdKey, catch the Enter Key, and swap it for a Tab key by sending a Tab, and not processing the Enter Key.

[C#]
     public class MyDataGrid : DataGrid
     {
          protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
          {
               if(msg.WParam.ToInt32() == (int) Keys.Enter)
               {
                    SendKeys.Send("{Tab}");
                    return true;
               }
               return base.ProcessCmdKey(ref msg, keyData);
          }
     }


[VB.NET]
     Public Class MyDataGrid
          Inherits DataGrid

          Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, keyData As System.Windows.Forms.Keys) As Boolean
               If msg.WParam.ToInt32() = CInt(Keys.Enter) Then
                    SendKeys.Send("{Tab}")
                    Return True
               End If
               Return MyBase.ProcessCmdKey(msg, keyData)
          End Function 'ProcessCmdKey

     End Class 'MyDataGrid