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开发者_StackOverflow 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?
I've just had the same problem! What helped me was:
In design view of the form click your table
Set AutoScroll and AutoSize to True
Go to Edit Rows And Columns
Click The first (and probably only) row and set its Size Type to AutoSize
All new rows will also have Size Type AutoSize. Thanks to this my rows are located and resized properly.
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.
精彩评论