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

窗口控件执行KeyPress事件。

下面以textbox控件为例:

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

另外,也可以参考:

作者昵称: 惊风雨 

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

 
原文地址:https://www.cnblogs.com/sunnywam/p/13550685.html