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

本文关键字:事件处理 程序 关联 数组 事件 文本 | 更新日期: 2023-09-27 17:53:40

我有一个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中)

关联中是否有语法错误?

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

删除KeyPressEventHandler并添加事件处理程序,如下所示

tb[i].KeyPress += textBoxP_KeyPress;

new

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