简体   繁体   English

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

[英]Associate a keypress event with a event handler in a array of textboxes

I've an array of textboxes that generates run-time a variable number of textboxes such the value of a textbox already created into the form. 我已经阵列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        
}

When I move to the line in which i associate the event to the event-handler it gives the error it isn't a valid construct in the contest" (in particular in the word keypresseventhandler ) 当我移至将事件与事件处理程序相关联的那一行时,它给出了错误,表明它不是比赛中的有效构造”(尤其是在keypresseventhandler

is there a syntactical error in the association? 关联中是否存在语法错误?

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

tb[i].KeyPress += textBoxP_KeyPress;

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

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

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