winform 通过左右键,或enter键做类似Tab键的功能

有时候我们需要使用enter,或arrow key来跳到下一个控件,可是我做怎么样做才实现与Tab一样的校果呢. 很简单,我们只要把窗体的keypreview属性设为true.然后在keyup或所有的keyevent事件里.写上如下的代码:

         private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter||e.KeyCode==Keys.Right)
            {
                if (ActiveControl != null)
                {
                    this.SelectNextControl(this.ActiveControl, true, true, true, true);                    
                    //this.SelectNextControl(this.ActiveControl, true, false, false, true);
                    //this.ActiveControl.SelectNextControl(this.ActiveControl, true, false, false, true);
                }
            }      
        }

你可能感兴趣的:(WinForm)