C#winform设置回车事件

有些话、适合烂在心里 提交于 2019-12-05 22:03:25

拿登录页设置为例:

输入用户名后回车,自动跳转到密码输入框,密码输入之后回车,触发点击登录事件。

用户名处代码:

 private void txt_user_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
                this.txt_pass.Focus();
        }

密码处代码:

 private void txt_pass_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
                this.btn_login.PerformClick();
        }

登录按钮事件:

 private void btn_login_Click(object sender, EventArgs e)
        {
            if (this.txt_user.Text.Trim() == "")
            {
                MessageBox.Show("请输入用户名!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.txt_user.Focus();
                return;
            }
            if (this.txt_pass.Text.Trim() == "")
            {
                MessageBox.Show("请输入密码!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.txt_pass.Focus();
                return;
            }
            string username = this.txt_user.Text.Trim();
            string password = MD5_class.GetMd5Hash(this.txt_pass.Text.Trim());

            tb_user return_user = role_user_manage.check_login(username, password);
            if (return_user == null)
            {
                MessageBox.Show("用户名或密码错误!请重新输入。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.txt_user.Focus();
                return;
            }

}

喜欢就联系我

 

 

 

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