Texan Programmer
Texan Programmer

Reputation:

How to Interpret an Enter KeyPress as a Tab in C#

I just recently began in C# development, and I was working on a forms-based project and I am trying to perform a "tab" action when the user is on a form and pressed the Enter key.

I know the answer is probably quite simple, but I am a newbie in this realm.

Upvotes: 3

Views: 21126

Answers (4)

Kirtish
Kirtish

Reputation: 1

private void DataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)  
{
        SendKeys.Send("{UP}");
        SendKeys.Send("{Right}");
}

private void onEnterKeyPress(object sender, KeyPressEventArgs e)
{

      if (sender is DataGridView)
      {
         int iColumn = DataGridView1.CurrentCell.ColumnIndex;
         if (iColumn == DataGridView1.Columns.Count - 1)
         {
               SendKeys.Send("{home}");
         }
         else
         {

               this.ProcessTabKey(true);
         }
       }
}

Upvotes: 0

HABJAN
HABJAN

Reputation: 9328

You can handle keyboard events on Form level and on complete application level using Application.AddMessageFilter and IMessageFilter interface.

All those events had "Handled" property which you can set to "True" if you will handle some key manually. (In your case Enter key).

Here is a sample how to trap key events for both levels: Keyboard event handling in .NET applications

Upvotes: 0

user565869
user565869

Reputation:

First, prepare a Dictionary of , where the key is the first control and the value is the second. Loop through all controls in the Form's Control collection, put them in a sorted list by TabIndex, and convert that to a Dictionary.

You'd need code in the KeyPress event for each object, or subclass TextBox to include this logic. Either way, in the KeyPress event, if the input is Enter, get the following control from your dictionary and use Control.GetFocus().

Hope that helps! I can provide more specifics if you'd like.

Upvotes: 1

Rion Williams
Rion Williams

Reputation: 76547

Welcome to SO Tex,

I believe there are two methods to accomplish this, would just involve adding:

Option 1: Grabbing the Next Control if an Enter KeyPress was performed

In the properties of your form, set the KeyPreview property of the form to true.

The code below will capture your "Enter-Press" event and perform the logic that you are looking for:

private void [YourFormName]_KeyDown(object sender, KeyEventArgs e)
{
    Control nextControl ;
    //Checks if the Enter Key was Pressed
    if (e.KeyCode == Keys.Enter) 
    {
        //If so, it gets the next control and applies the focus to it
        nextControl = GetNextControl(ActiveControl, !e.Shift);
        if (nextControl == null)
        {
            nextControl = GetNextControl(null, true);
        }
        nextControl.Focus();
        //Finally - it suppresses the Enter Key
        e.SuppressKeyPress = true;
    }
} 

This actually allows for the user to press "Shift+Enter" to go to the proceeding tab as well.

Option 2: Using the SendKeys method

private void [YourFormName]_KeyDown(object sender, KeyEventArgs e)
{
  if (e.KeyCode == Keys.Enter)
  {
     SendKeys.Send("{TAB}");
  }
}

I am not sure if this method is still commonly used or may be considered a "hack"? I would recommend the first one, but I believe both should work for your needs.

Upvotes: 11

Related Questions