关于C#中怎样实现点ENTER键就相当于按下确认输出按钮

目标:在输入框中输入任何值后,点击“确认”按钮让输出框中输出“Hello!”。

主界面:

希望在输入框中输入信息后,点击“Enter”键实现点击界面中“确认”的功能。

方法:

1. 找到TextBox的KeyPressed方法:

2.在TextBox的KeyPressed方法体中写:

  private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
      {
            if (e.KeyChar == 13)
            {
                btnConfirm.PerformClick();
            }
       }

3.在Button的Click方法里完成相关功能即可

  private void btnConfirm_Click(object sender, EventArgs e)
       {
            textBox2.Text = "Hello!";
       }

原文地址:https://www.cnblogs.com/CherishFX/p/2851716.html