开发者

TableLayoutPanel Controls.Add Problem

开发者 https://www.devze.com 2022-12-10 17:48 出处:网络
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:

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:

  1. In design view of the form click your table

  2. Set AutoScroll and AutoSize to True

  3. Go to Edit Rows And Columns

  4. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消