Reputation: 1
how do I get each click on the button to create a new button and put in the panel?.
I do not know what method to use to create array of button.
private void button1_Click(object sender, EventArgs e)
{
Button c = new Button();
c.Location = new Point(15,40);
c.Text = "novo";
panel1.Controls.Add(c);
}
Upvotes: 1
Views: 206
Reputation: 1794
You do create new button and add it to panel, just you create them all in SAME place.
c.Location = new Point(15,40);
You probably need some counter on class level for X or Y coordinate or for both.
public class Form1 : FOrm {
private int x = 15;
private void button1_Click(object sender, EventArgs e)
{
Button c = new Button();
c.Location = new Point(x,40);
c.Text = "novo";
panel1.Controls.Add(c);
x += 10 + c.Size.Width;
}
}
You might want to check whether you are out of boundaries of form and to start at the beginning of "new row".
Upvotes: 1
Reputation: 63065
you can create list of buttons like below
public partial class Form1 : Form
{
List<Button> ButtonList = new List<Button>();
then you can create buttons as you did
private void button1_Click(object sender, EventArgs e)
{
Button c = new Button();
c.Location = new Point(10 , 40);
c.Text = "novo";
ButtonList.Add(c); // add to list as well
panel1.Controls.Add(c);
}
Note that you may want to change the location for each button otherwise all buttons are overlap and you only see one button which is on the top
Upvotes: 1