Gold
Gold

Reputation: 62424

create buttons Dynamically on a panel

Looking through the way for create buttons dynamically on a panel

I tried this.

int top = 50;
int left = 100;

for (int i = 0; i < 10; i++)
{
    Button button = new Button();
    button.Left = left;
    button.Top = top;
    this.Controls.Add(button);
    top += button.Height + 2;
}

but I don't know how to put them on panel

Upvotes: 1

Views: 5342

Answers (1)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236208

Instead of adding buttons to form controls, add them to panel controls (I believe this is your form or user control):

int top = 50;
int left = 100;

for (int i = 0; i < 10; i++)
{
    Button button = new Button();
    button.Left = left;
    button.Top = top;
    panel.Controls.Add(button); // here
    top += button.Height + 2;
}

UPDATE: for handling button click events, you should subscribe all buttons for single event handler (when you create button):

    button.Click += Button_Click;

And in event handler you can use sender to see which button raised event:

private void Button_Click(object sender, EventArgs e)
{
    Button button = (Button)sender;
    // you have instance of button
    // ...
}

Upvotes: 4

Related Questions