Visual Studio C # Capturing Enter a key in the text box

I am trying to capture the Enter key in a Windows Forms text box. I got this code snippet from the tutorial:

private void textBox1_KeyDown(object sender, KeyEventArgs e) { // // Detect the KeyEventArg key enumerated constant. // if (e.KeyCode == Keys.Enter) { MessageBox.Show("You pressed enter! Good job!"); } else if (e.KeyCode == Keys.Escape) { MessageBox.Show("You pressed escape! What wrong?"); } } 

but now my code is causing a compilation / assembly error:

 The event 'System.Windows.Forms.Control.Enter' can only appear on the left hand side of += or -= Line 44 Column 84 

For one thing, I do not understand the error message. On the other hand, line 44 is an empty line having only a newline character.

Any advice is appreciated.

Sincerely.

+4
source share
1 answer

Check the constructor file (form.Designer.cs)

Your Designer must be:

 this.textBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyDown); 

You may be subscribed to the Enter event. This is actually not an Enter key. This is to be on it, and it is paired with the Leave event.

+1
source

Source: https://habr.com/ru/post/1415625/


All Articles