Reputation: 21
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);
Upvotes: 2
Views: 4868
Reputation: 187
try this.TextBox.KeyDown += new System.Windows.Forms.KeyEventHandler(this.TextBox_KeyDown);
Upvotes: 0
Reputation: 69372
Use the KeyEventHandler
instead. Also, is the name of your TextBox
actually TextBox
? This isn't recommended as it becomes ambiguous as to whether you're referring to an instance or the TextBox control object.
this.TextBox.KeyDown += new KeyEventHandler(this.TextBox_KeyDown)
Your error message refers to the fact that your method's signature is (object sender, KeyEventArgs e)
but the delegate you're trying to pass actually has a signature of `(object sender, EventArgs e)
.
Upvotes: 3