按回车键跳到下一输入框或激活按钮事件的方法

登录窗口有两个输入框:txtUserName、txtPassWrod,两个按钮:btnOK、btnCancel

现需实现txtUserName输入回车键时光标跳到txtPassWord,办法是添加txtUserName组件的KeyPress事件

        private void txtUserName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                txtPassWord.Focus();
            }
        }

另需实现txtPassWord输入回车键时激活btnOK的Click事件,开始也想用上面的办法,后google到更简单的办法:

设置窗体的属性AcceptButton为btnOK即可!
同样道理,设置窗体的属性CancelButton为btnCancel,就可以响应Esc键了!

Leave a Reply

Your email address will not be published. Required fields are marked *