Hello world! I am requiring some assistance with my C# program, where I am getting an error message for having a form where the user would enter in text into a text box. I am trying to detect if the user has pressed the Enter key, and on doing so would produce a message box with a message. However, I am trying to call the "KeyEventArgs" class which would allow me to detect the key press, but receive the following error message:

No overload for 'TextBox_KeyDown' matches delegate 'System.EventHandler'

Here is my code for the TextBox I am using:

    private void TextBox_KeyDown(object sender, KeyEventArgs e)
        if (e.KeyCode == Keys.Enter)
            MessageBox.Show("You have entered the correct key.");

Clicking on the error message for further details, leads me to the Designer class for the form I am using, and underlines the following text:

   this.TextBox.KeyDown += new System.EventHandler(this.TextBox_KeyDown);

Any help would be much appreciated. Thanks!

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

Yo can use KeyDown Event, KeyPress Event and KeyUp Event

KeyDown Event : This event raised as soon as the user presses a key on the keyboard, it repeats while the user keeps the key depressed.

KeyPress Event : This event is raised for character keys while the key is pressed and then released. This event is not raised by noncharacter keys, unlike KeyDown and KeyUp, which are also raised for noncharacter keys

KeyUp Event : This event is raised after the user releases a key on the keyboard.

More about ...C# Keypress Event


This article has been dead for over six months. Start a new discussion instead.