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

时间:2013-05-21 22:40:35

标签: c#

大家好。这是我的第一个程序,在5分钟内出现错误。我今天才开始使用C#,所以我知道我应该真的环顾四周,但我不认为我在做什么有问题。

我的程序是一个生成器 取决于用户选择的内容或所有文本框中的类型取决于生成的代码的外观。

我有两个名为textBox1GeneratedCode

的文本框

当我按checkBox1时,可以使用textbox1

当我按下我的按钮时,它创建了一个字符串“Testing”(这是为了确保我做对了)。

当我按F5测试我的构建时,它返回了这个错误:

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

我不知道这意味着什么。

这是我的代码:

    public void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
        switch (checkBox1.Checked)
        {
            case true:
                {
                    textBox1.Enabled = true;
                    break;
                }
            case false:
                {
                    textBox1.Enabled = false;
                    break;
                }
        }
    }
    private void textBox1_TextChanged()
    {

    }

    public void button1_Click(object sender, EventArgs e)
    {
        GenerateBox.Text += "Testing";
    }

    private void GenerateBox_Generated(object sender, EventArgs e)
    {

    }

这是使用C ++的form1.designer:

// 
   // textBox1
   // 
   this.textBox1.Enabled = false;
   this.textBox1.Location = new System.Drawing.Point(127, 3);
   this.textBox1.Name = "textBox1";
   this.textBox1.Size = new System.Drawing.Size(336, 20);
   this.textBox1.TabIndex = 1;
   this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged); //Error
   // 
   // GenerateBox
   // 
   this.GenerateBox.Enabled = false;
   this.GenerateBox.Location = new System.Drawing.Point(84, 6);
   this.GenerateBox.MaxLength = 1000000;
   this.GenerateBox.Multiline = true;
   this.GenerateBox.Name = "GenerateBox";
   this.GenerateBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
   this.GenerateBox.Size = new System.Drawing.Size(382, 280);
   this.GenerateBox.TabIndex = 1;
   this.GenerateBox.TextChanged += new System.EventHandler(this.GenerateBox_Generated);

3 个答案:

答案 0 :(得分:3)

在这种情况下,函数textbox1_textChanged应具有以下两个参数,以便被EventHandler接受

textBox1_TextChanged(object sender, EventArgs e)

答案 1 :(得分:2)

您的textbox1_TextChanged方法与System.EventHandler代表的预期不符。它应该是

private void textBox1_TextChanged(object sender, EventArgs e)
{
}

答案 2 :(得分:1)

编译器告诉您确切的错误,您没有EventHandler名为textBox1_TextChanged

将您的textBox1_TextChanged方法更改为:

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        //Why are you handling this event if you aren't actually doing anything here???
    }

对于我对此问题的其他关注,请参阅我的代码示例的注释部分。

如果您不想为此事件添加处理程序,只需从设计器代码中删除以下内容:

    textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
相关问题