views:

310

answers:

1

I have a TableLayputPanel with 2 columns and 4 rows and I'm trying to add buttons to it at runtime. I want to dynamically add each button to the first cell:

private int nextIndex = 1;

private void bAddButton_Click(object sender, EventArgs e)
{
    Button newButton = new Button();
    newButton.Text = nextIndex.ToString();
    tableLayoutPanel1.Controls.Add(newButton, 0, 0);  // first cell
    nextIndex++;
}

As I understand it this should shifts all existing buttons up a cell. This seems to work the first three times but after that is inserts the new button into the 2nd cell a few times then the 3rd cell, then the 4th etc...

Is there a limit on how many times you can call Controls.Add(ctrl, column, row) for a given cell?

I bit stuck, what am I doing wrong?

A: 

To add a number of buttons to a single cell you need to add a panel as the only control in the cell and then add your buttons to that panel.

Matt Breckon
Hi Matt, Thank you for your quick response, sorry I probably wasn't clear enough, I only want one button per cell, but I want to add the new button to the fist cell making all existing buttons shift along by one cell. My code does work the first three times but after that the same code insert into the wrong cell.
TimD
hmmm, I see what you mean it does do something quite strange doesn't it.
Matt Breckon