Winforms TableLayoutPanel 以编程方式添加行
我已经为此奋斗了一段时间,并且发现许多其他人也在与 TableLayoutPanel (.net 2.0 Winforms) 斗争。
问题
我试图采用一个“空白”tablelayoutpanel,它定义了 10 列,然后在运行时以编程方式添加控件行(即每个单元格一个控件)。
有人可能认为它应该像这样简单,
myTableLayoutPanel.Controls.Add(myControl, 0 /* Column Index */, 0 /* Row index */);
但是(对我来说)不会添加行。 所以也许添加行样式
myTableLayoutPanel.RowStyles.Clear();
myTableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, 30F));
但这也不起作用。 我仔细研究了一下,发现 myTableLayoutPanel.RowCount
用法从设计时到运行时发生了变化,因此执行 myTableLayoutPanel.RowCount++;
实际上并没有添加另一行,甚至在为其添加 RowStyle 条目之前/之后也不行!
我遇到的另一个相关问题是控件将被添加到显示中,但它们都只是在 TableLayoutPanel 的 0,0 点处呈现,此外它们甚至不被限制在它们应该在的 Cell 边界内显示在(即使用 Dock = DockStyle.Fill 时,它们仍然显得太大/太小)。
有人有添加行和行的工作示例吗? 运行时控制?
I've been fighting with this for a while, and have found that a number of other people struggle with the TableLayoutPanel (.net 2.0 Winforms) as well.
Problem
I am attempting to take a 'blank' tablelayoutpanel, which has 10 columns defined, then at runtime programmatically add rows of controls (i.e. one control per cell).
One might have thought that it should be as simple as
myTableLayoutPanel.Controls.Add(myControl, 0 /* Column Index */, 0 /* Row index */);
But that (for me) doesn't add the rows. So maybe adding in a row style
myTableLayoutPanel.RowStyles.Clear();
myTableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, 30F));
But that doesn't work either. I've dug around and found out that the myTableLayoutPanel.RowCount
usage changes from design time to run time, hence doing myTableLayoutPanel.RowCount++;
doesn't actually add another row, not even before/after adding a RowStyle entry for it!
Another related issue I am encountering is that the controls will be added to the display, but they all simply get rendered at point 0,0 of the TableLayoutPanel, additionally they are not even constrained to be within the Cell bounds that they are supposed to be displayed within (i.e. with Dock = DockStyle.Fill they still appear way too large/small).
Does someone have a working example of adding rows & controls at runtime?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我上周刚做了这个。 将
TableLayoutPanel
上的GrowStyle
设置为AddRows
或AddColumns
,然后您的代码应该可以工作:这是一些工作代码这看起来与您正在做的事情类似:
TableLayoutPanel
总是给我适合的尺寸。 在上面的示例中,我正在归档一张地址卡,该地址卡可能会根据具有第二行地址的帐户或国家/地区而增大或缩小。 因为表格布局面板的最后一行或最后一列将会拉伸,所以我将空标签扔到那里以强制一个新的空行,然后一切都很好地排列起来。这是设计器代码,因此您可以看到我开始的表格:
I just did this last week. Set the
GrowStyle
on theTableLayoutPanel
toAddRows
orAddColumns
, then your code should work:Here is some working code that seems similar to what you are doing:
The
TableLayoutPanel
always gives me fits with size. In my example above, I'm filing an address card that might grow or shrink depending on the account having an address line two, or a country. Because the last row, or column, of the table layout panel will stretch, I throw the empty label in there to force a new empty row, then everything lines up nicely.Here is the designer code so you can see the table I start with:
这是一个奇怪的设计,但是
TableLayoutPanel.RowCount
属性不反映RowStyles
集合的计数,对于ColumnCount
属性和ColumnStyles
集合。我发现我的代码中需要的是在更改
RowStyles
/ColumnStyles
后手动更新RowCount
/ColumnCount
代码>.以下是我使用过的代码示例:
其他想法
我从未使用过
DockStyle.Fill
来让控件填充网格中的单元格; 我通过设置控件的Anchors
属性来完成此操作。如果您要添加大量控件,请确保在该过程中调用
SuspendLayout
和ResumeLayout
,否则由于整个表单在之后重新放置,事情会运行缓慢添加每个控件。It's a weird design, but the
TableLayoutPanel.RowCount
property doesn't reflect the count of theRowStyles
collection, and similarly for theColumnCount
property and theColumnStyles
collection.What I've found I needed in my code was to manually update
RowCount
/ColumnCount
after making changes toRowStyles
/ColumnStyles
.Here's an example of code I've used:
Other thoughts
I've never used
DockStyle.Fill
to make a control fill a cell in the Grid; I've done this by setting theAnchors
property of the control.If you're adding a lot of controls, make sure you call
SuspendLayout
andResumeLayout
around the process, else things will run slow as the entire form is relaid after each control is added.以下是向两列 TableLayoutColumn 添加新行的代码:
标签控件位于左列中,值控件位于右列中。 这些控件通常为 Label 类型,并将其 AutoSize 属性设置为 true。
我认为这不太重要,但作为参考,这里是设置detailTable的设计器代码:
这一切都很好。 您应该意识到,使用 Controls 属性动态地从 TableLayoutPanel 中处置控件似乎存在一些问题(至少在框架的某些版本中)。 如果您需要删除控件,我建议处理整个 TableLayoutPanel 并创建一个新的。
Here's my code for adding a new row to a two-column TableLayoutColumn:
The label control goes in the left column and the value control goes in the right column. The controls are generally of type Label and have their AutoSize property set to true.
I don't think it matters too much, but for reference, here is the designer code that sets up detailTable:
This all works just fine. You should be aware that there appear to be some problems with disposing controls from a TableLayoutPanel dynamically using the Controls property (at least in some versions of the framework). If you need to remove controls, I suggest disposing the entire TableLayoutPanel and creating a new one.
在表单中创建一个包含两列的表格布局面板,并将其命名为
tlpFields
。然后,只需将新控件添加到表格布局面板(在本例中,我在第 1 列中添加了 5 个标签,在第 2 列中添加了 5 个文本框)。
最后,运行代码。
Create a table layout panel with two columns in your form and name it
tlpFields
.Then, simply add new control to table layout panel (in this case I added 5 labels in column-1 and 5 textboxes in column-2).
Finally, run the code.
我刚刚查看了我的代码。 在一个应用程序中,我只是添加控件,但没有指定索引,完成后,我只是循环遍历行样式并将大小类型设置为 AutoSize。 因此,仅添加它们而不指定索引似乎会按预期添加行(假设 GrowStyle 设置为 AddRows)。
在另一个应用程序中,我清除控件并将 RowCount 属性设置为所需的值。 这不会添加行样式。 然后我添加控件,这次指定索引,并添加一个新的 RowStyle (
RowStyles.Add(new RowStyle(...)
),这也有效。因此,选择以下方法之一,我记得它们都有效。我记得表格布局面板给我带来的麻烦。
I just looked into my code. In one application, I just add the controls, but without specifying the index, and when done, I just loop through the row styles and set the size type to AutoSize. So just adding them without specifying the indices seems to add the rows as intended (provided the GrowStyle is set to AddRows).
In another application, I clear the controls and set the RowCount property to the needed value. This does not add the RowStyles. Then I add my controls, this time specifying the indices, and add a new RowStyle (
RowStyles.Add(new RowStyle(...)
) and this also works.So, pick one of these methods, they both work. I remember the headaches the table layout panel caused me.
这非常适合在 TableLayoutPanel 中添加行和控件。
在设计页面中定义一个包含 3 列的空白 Tablelayoutpanel
创建一个按钮 btnAddRow 以在每次单击时添加行
This works perfectly for adding rows and controls in a TableLayoutPanel.
Define a blank Tablelayoutpanel with 3 columns in the design page
Create a button btnAddRow to add rows on each click
我刚刚遇到了一个相关的问题(这就是我找到这个线程的方式),我动态添加的行和列样式没有生效。 我通常将 SuspendLayout()/ResumeLayout() 视为优化,但在这种情况下,将我的代码包装在其中可以使行和列行为正确。
I just had a related problem (which is how I found this thread), where my dynamically added row and column styles were not taking effect. I usually consider SuspendLayout()/ResumeLayout() as optimizations, but in this case, wrapping my code in them made the rows and columns behave correctly.