回车切换控件焦点

一、使用Control的SelectNextControl方法

// 回车切换控件焦点//要想使这个方法起到作用先将窗体的keypreview属性改为true
protected override void OnKeyPress(KeyPressEventArgs e)
{
// 判断是否按下回车键,13是Enter键的ASCII码值
if (e.KeyChar == 13) {
      // 激活下一个控件
      this.SelectNextControl(this.ActiveControl, true, true, true, true); } base.OnKeyPress(e); }

二、ProcessTabKey 方法或SendKeys.Send("{Tab}"); 这需要设置控件的TabIndex顺序:视图→Tab键顺序

       //控件的KeyUp事件:在释放键时发生
        private void txtProductWide_KeyUp(object sender, KeyEventArgs e)
        {
                if (e.KeyCode == Keys.Enter)
                {
                    ProcessTabKey(true);
                    //SendKeys.Send("{Tab}");
                    return;
                }
    }


 

原文地址:https://www.cnblogs.com/lusunqing/p/3242985.html