C#: How to make pressing enter in a text box trigger a button, yet still allow shortcuts such as “Ctrl+A” to get through?

后端 未结 5 1027
抹茶落季
抹茶落季 2021-02-03 22:01

Sorry for the long title, but I couldn\'t think of another way to put it.

I have this:

    private void textBoxToSubmit_KeyDown(object sender, KeyEventAr         


        
5条回答
  •  逝去的感伤
    2021-02-03 22:30

    You can Use KeyPress instead of KeyUp or KeyDown its more efficient and here's how to handle

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                e.Handled = true;
                button1.PerformClick();
            }
        }
    

    hope it works

提交回复
热议问题