You need to add a click event handler to the button - or the event handler method is not called.
Button btnAdd = new Button();
this.Text = counter.ToString();
btnAdd.Name = "click";
btnAdd.Click += new EventHandler(btnAdd_Click);
btnAdd.BackColor = Color.Gray;
btnAdd.Text = "Add";
btnAdd.Location = new System.Drawing.Point(20, 20);
flowLayoutPanel1.Controls.Add(btnAdd);
}
void btnAdd_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
if (btn != null)
{
label1.Text = (btn.Name + " pressed");
}
}
Note the check for
null
in the handler - it's worth always adding this. You can use the same method with a variety of controls and checking stops it throwing a null reference exception.