C#--监控Enter和Esc事件

1,对于winform自带的Button按钮

this.CancelButton = this.button1;//设置Esc键
this.AcceptButton = this.button2;//设置Enter键  

2,对于自定义的按钮

        public FrmUserLogin()
        {
            InitializeComponent();
            this.KeyPreview = true;//WinForm下的键盘事件(KeyPress、KeyDown)不响应的大多数原因
            this.KeyDown += FrmUserLogin_KeyDown;
        }

        private void FrmUserLogin_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.btn_login_Click(null,null);
            }
        }

  

posted @ 2022-01-24 19:17  包子789654  阅读(838)  评论(0编辑  收藏  举报