C#Winform Alter发送的击键

C#Winform Alter发送的击键,c#,winforms,key,C#,Winforms,Key,嗨,我有一个C#winform应用程序,其中一个特定的表单填充了许多文本框。我想通过按向右箭头键来模仿按tab键的行为。我真的不知道怎么做 我根本不想改变tab键的行为,只要在表单上使用正确的箭头键即可 有人能提供一些建议吗?我认为这将实现您的要求: private void form1_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Right) { Control activeCon

嗨,我有一个C#winform应用程序,其中一个特定的表单填充了许多文本框。我想通过按向右箭头键来模仿按tab键的行为。我真的不知道怎么做

我根本不想改变tab键的行为,只要在表单上使用正确的箭头键即可


有人能提供一些建议吗?

我认为这将实现您的要求:

private void form1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Right)
    {
        Control activeControl = form1.ActiveControl;
        // may need to check for null activeControl
        form1.SelectNextControl(activeControl, true, true, true, true);
    }
}

您可以使用表单上的KeyDown事件捕捉按键笔划,然后执行任何您想要的操作。例如:

 private void MyForm_KeyDown(object sender, KeyEventArgs e)
 {
     if(e.KeyCode == Keys.Right)
     {
         this.SelectNextControl(....);
         e.Handled = true;
     }
 }

不要忘记将表单上的KeyPreview属性设置为True。

您应该覆盖表单中的OnKeyUp方法以执行此操作

protected override void OnKeyUp(KeyEventArgs e)
{
    if (e.KeyCode == Keys.Right)
    {
       Control activeControl = this.ActiveControl;

       if(activeControl == null)
       {
            activeControl = this;
       }

       this.SelectNextControl(activeControl, true, true, true, true);
       e.Handled = true;
    }

    base.OnKeyUp(e);
}

您好,Brian,这似乎工作正常,但它只遍历它所在容器上的控件,例如groupbox。因此,如果我有3个带有控件的GroupBox,它不会跳转到下一个控件,比如tab键,willI会将其更新为使用SelectNext,我认为SelectNext在容器上会递归工作。我会重写该方法,而不是使用事件。另外,我不会在按下键时这样做,你应该在按下键和向上键之间进行跟踪,以确保你有一个相关的完整按键。