Sorry for the poor formatting, but this should give you the idea. I am only
displaying the buttons on the form, so I didn't need an Arraylist or
anything to keep track of them myself, not sure what you need to do, if you
need to keep track of the buttons I would suggest an ArrayList and add the
new button to the list right after you create it.
private void dynamicButtonClick(object sender, System.EventArgs e)
{
Button button = (Button)sender;
MessageBox.Show(button.Text);
}
private void Form1_Load(object sender, System.EventArgs e)
{
Button curButton; //place holder for the current button
int x = 5; //Keep track of where the current button is to be placed
int y = 5;
for (int i = 0; i < 20; i++)
{
curButton = new Button(); //Create new button
curButton.Click += new EventHandler(dynamicButtonClick); //assign the
click event to the button
curButton.Parent = this; //show the button on the form.
curButton.Location = new System.Drawing.Point(x, y); //set the Buttons
left, top
curButton.Text = i.ToString(); //Set the Text of the button
y += curButton.Height + 5; //Inc the top so the next button appears
below this one.
if (y + curButton.Height > this.ClientRectangle.Height)//Prevent the
buttons from going off the bottom of the form.
{
y = 5;
x += curButton.Width + 5;
}
}
}
--
Thanks
Wayne Sepega
Jacksonville, Fl
"When a man sits with a pretty girl for an hour, it seems like a minute. But
let him sit on a hot stove for a minute and it's longer than any hour.
That's relativity." - Albert Einstein