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

纵然是瞬间 提交于 2019-12-12 17:09:53

问题


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.


回答1:


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.




回答2:


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.




回答3:


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




回答4:


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);
         }
       }
}


来源:https://stackoverflow.com/questions/4626948/how-to-interpret-an-enter-keypress-as-a-tab-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!