1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
//FIRST : Add a panel (named Pnl) and a button (named createButton) in your main Form.
private void createButton_click(object sender, EventArgs e)
{
// How many buttons dyou need ?
int NumOfButtons = 12;
// X Location of each created button in the panel
int loc = 20;
for (int i = 1; i <= NumOfButtons; i++) {
Button btn = new Button();
{
btn.Name = "Btn-" + i.tostring;
btn.Size = new Size(50, 20);
btn.Tag = i;
btn.Text = "Browse-" + i.tostring;
btn.location = new Point(5, loc);
}
// Add Click event Handler for each created button
btn.Click += Buttons_Click;
loc += 20;
// Add the created btn to panl
pnl.Controls.Add(btn);
}
}
private void Buttons_Click(System.Object sender, System.EventArgs e)
{
// Use "Sender" to know which button was clicked ?
Button btn = sender as Button;
Interaction.Msgbox("Name : " + btn.name + Constants.vbnewline + "Tag : " + Btn.Tag);
} |
Partager