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 1086
抹茶落季
抹茶落季 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:49

    If you want the return to trigger an action only when the user is in the textbox, you can assign the desired button the AcceptButton control, like this.

        private void textBox_Enter(object sender, EventArgs e)
        {
            ActiveForm.AcceptButton = Button1; // Button1 will be 'clicked' when user presses return
        }
    
        private void textBox_Leave(object sender, EventArgs e)
        {
            ActiveForm.AcceptButton = null; // remove "return" button behavior
        }
    

提交回复
热议问题