C#:按enter键自动执行button事件

 

窗口控件执行KeyPress事件。

下面以textbox控件为例:

 

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if(e.KeyChar == '\r')//或者e.KeyChar == (char)Keys.Enter
{
button1.Focus(); //将鼠标焦点移到Button1按钮上
button1_Click(this, new EventArgs());   //执行按钮事件
}

 

另外,也可以参考:

作者昵称: 惊风雨 

https://www.cnblogs.com/winformasp/articles/11870708.html

 

 

posted on 2020-08-23 21:01  万马阳光  阅读(1153)  评论(0编辑  收藏  举报