动态创建项目并将其添加到TableLayoutPanel

时间:2015-04-04 15:39:10

标签: c# tablelayoutpanel

这是我目前必须动态创建TableLayoutPanel的代码。

TableLayoutPanel searchResults_tableLP = new TableLayoutPanel();
searchResults_tableLP.ColumnCount = 2;
searchResults_tableLP.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
searchResults_tableLP.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
searchResults_tableLP.Location = new System.Drawing.Point(3, 3);
searchResults_tableLP.Name = "tableLayoutPanel" + i;
searchResults_tableLP.RowCount = 1;
searchResults_tableLP.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
searchResults_tableLP.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
searchResults_tableLP.Size = new System.Drawing.Size(200, 100);
searchResults_tableLP.TabIndex = 0;

但是我该怎么做呢?我不知道也无法找到添加ColumnStylesRowStyles

的语法
TableLayoutPanel searchResults_tableLP = new TableLayoutPanel
                        {
                            ColumnCount = 2,
                            Location = new System.Drawing.Point(3, 3),
                            Name = "searchResults_tableLP" + i,
                            RowCount = 1, 
                            Size = new System.Drawing.Size(612, 59),
                            TabIndex = 0,
                        };

我如何动态地向表格添加内容或控件? 示例我想在第一列或第二列添加文本?我该怎么做呢?这似乎是一个简单的答案,但我无法在任何地方找到它。

1 个答案:

答案 0 :(得分:0)

ColumnStyles是一个只读属性,它是在TableLayoutPanel初始化时创建的。

Add to a readonly collection in a constructor?