Giu
Giu

Reputation: 95

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.

 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)

is there a syntactical error in the association?

Upvotes: 0

Views: 1256

Answers (2)

mosca125
mosca125

Reputation: 131

Remove the KeyPressEventHandler and add the event handler like so

tb[i].KeyPress += textBoxP_KeyPress;

Upvotes: 1

user959729
user959729

Reputation: 1187

new

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

Upvotes: 0

Related Questions