将按键事件与文本框数组中的事件处理程序相关联

时间:2013-01-16 22:54:37

标签: c# winforms

我有一个textboxes数组,它生成一个可变数量textboxes的运行时,例如已经在表单中创建的textbox的值。

 int n;
 TextBox[] tb;        

 public void AggiungiArmoniche()
 {
        n = int.Parse(textBox4.Text);
        tb = new 
        TextBox[n];

    for (int i = 1; i < tb.Length; i++)
    {
        tb[i] = new TextBox();            
        tb[i].Name = "textBox" + i;                
        tb[i].Location = new Point(100 *i, 163);
        tb[i].Size = new Size(48, 20);
        tb[i].KeyPress += System.Windows.Forms.KeyPressEventHandler(textBoxP_KeyPress);            
        groupBox1.Controls.Add(tb[i]);            
    }
} 

private void textBoxP_KeyPress(object sender, KeyPressEventArgs e)
{
  // statements of the event        
}

当我移动到我将事件与事件处理程序关联的行时,它会给出错误它不是竞赛中的有效构造“(特别是在单词keypresseventhandler中)

关联中是否存在语法错误?

2 个答案:

答案 0 :(得分:1)

删除KeyPressEventHandler并添加事件处理程序,如此

tb[i].KeyPress += textBoxP_KeyPress;

答案 1 :(得分:0)

tb[i].KeyPress += new System.Windows.Forms.KeyPressEventHandler(textBoxP_KeyPress);