【问题标题】:Method signature doesn't match a textbox eventhandler方法签名与文本框事件处理程序不匹配
【发布时间】:2013-10-22 06:06:21
【问题描述】:

当我使用Char.IsNumber 时,它会在form1.designer.cs 中创建一个错误。

textBox1_TextChanged”没有重载匹配委托“System.EventHandler

private void textBox1_TextChanged(object sender, KeyPressEventArgs e)
{
    if (char.IsNumber(e.KeyChar))
    {
        textBox1.Text = e.KeyChar.ToString();
    }
    else
    {
        MessageBox.Show("Char value");
    }
}


///////////// Text Box ///////////////
this.textBox1.Location = new System.Drawing.Point(73, 57);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(177, 20);
this.textBox1.TabIndex = 0; 
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);  // error occurs on this line 

【问题讨论】:

标签: c# winforms events textbox


【解决方案1】:

KeyPressEventArgs 更改为 TextChangedEventArgs(对于 WPF,对于 winforms 使用 EventArgs)。该错误告诉您TextChanged 事件的签名与您分配给它的方法不匹配。

【讨论】:

  • 我认为TextChangedEventArgs 只在 WPF 中,这看起来像 Windows 窗体。
【解决方案2】:

您的处理程序与 TextChanged 事件的签名不匹配,试试这个:

private void textBox1_TextChanged( object sender, EventArgs e )

此外,当您这样做时,您会发现 TextChanged 事件上没有 e.KeyChar。你确定这是你想要的活动吗?

【讨论】:

  • 我的是一样的。使用“KeyPressEventArgs”的原因是为了获取KeyDown。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-08-09
  • 2020-08-03
  • 2016-03-07
  • 2018-01-15
  • 2018-01-25
  • 1970-01-01
  • 2018-10-06
相关资源
最近更新 更多