繁体   English   中英

“…”没有重载匹配委托“ EventHandler”吗?

[英]No overload for “…” matches delegate 'EventHandler'?

这是一个使用的代码片段,因此文本框(“ TxtInput1”)中只有一个小数,并且只有数字:

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;
    }
}

但这给了我以下错误:

CS0123'TxtInput1_TextChanged'的重载与委托'EventHandler'不匹配

我单击了错误,然后弹出此错误:

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);

System.EventHandler(form1.TxtInput1_TextChanged); 带红色下划线表示错误。 这个问题有解决办法吗?

您的方法的签名与处理TextChanged事件所需的签名不匹配。 TextChanged事件的第二个参数就是EventArgs 但是,如果将其更改为该值,则方法的内容将无法编译。

从方法签名的外观来看,您需要将其关联到KeyPress事件。

订阅你的处理器TxtInput1_TextChangedKeyPress的事件TxtInput1而非TextChanged 错误是由于代表签名不匹配所致。

更改为以下内容:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM