TableLayoutPanel Controls.Add 问题
我有一个包含 2 列和 4 行的 TableLayputPanel,我试图在运行时向其添加按钮。我想将每个按钮动态添加到第一个单元格:
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++;
}
据我了解,这应该将所有现有按钮向上移动到单元格中。这似乎在前三次有效,但之后将新按钮插入第二个单元格几次,然后第三个单元格,然后第四个等等......
您可以调用 Controls.Add( 的次数是否有限制ctrl、列、行)对于给定的单元格?
我有点卡住了,我做错了什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我刚刚遇到了同样的问题!对我有帮助的是:
在表单的设计视图中单击您的表格
将 AutoScroll 和 AutoSize 设置为 True
单击第一行(可能是唯一的)行并将其大小类型设置为自动调整大小
所有新行也将具有 AutoSize 大小类型。由于这个原因,我的行已正确定位并调整大小。
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.