Khalid Ahmed
Khalid Ahmed

Reputation: 21

No overload for "..." matches delegate 'EventHandler'?

Here is a snippet of code that is used so a textbox ("TxtInput1") has only one decimal in it and only numbers in it:

private void TxtInput1_TextChanged(object sender, KeyPressEventArgs e)
{
    if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && (e.KeyChar != '.'))
    {
        e.Handled = true;
    }

    // only allow one decimal point
    if ((e.KeyChar == '.') && ((sender as TextBox).Text.IndexOf('.') > -1))
    {
        e.Handled = true;
    }
}

But it gives me the following error:

CS0123 No overload for 'TxtInput1_TextChanged' matches delegate 'EventHandler'

I clicked on the error and it popped up with this:

form1.TxtInput1.Location = new System.Drawing.Point(92, 111);
form1.TxtInput1.Name = "TxtInput1";
form1.TxtInput1.Size = new System.Drawing.Size(43, 20);
form1.TxtInput1.TabIndex = 8;
form1.TxtInput1.TextChanged += new System.EventHandler(form1.TxtInput1_TextChanged);

The line System.EventHandler(form1.TxtInput1_TextChanged); is underlined red meaning it's wrong. Any fix for this issue?

Upvotes: 1

Views: 10852

Answers (2)

Richardissimo
Richardissimo

Reputation: 5773

The signature of your method does not match what is required to handle the TextChanged event. The second parameter for the TextChanged event is just EventArgs. But if you change it to that, the contents of your method won't then compile.

From the look of your method's signature, you need to be hooking this up to a KeyPress event instead.

Upvotes: 2

rahulaga-msft
rahulaga-msft

Reputation: 4164

Subscribe your handler TxtInput1_TextChanged to KeyPress event of TxtInput1 rather than TextChanged. Error is due to signature mismatch of delegates.

change to below :

form1.TxtInput1.KeyPress+= new System.KeyPressEventHandler(form1.TxtInput1_TextChanged);

Upvotes: 1

Related Questions