的WinForms TableLayoutPanel中添加行编程WinForms、TableLayoutPanel

2023-09-02 01:44:18 作者:溺水的癞蛤蟆

我一直在争取与这一段时间,并发现了一些其他人的TableLayoutPanel中(.NET 2.0的WinForms)斗争为好。

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.

问题

我试图采取了空白TableLayoutPanel中,其中有10列定义,那么在运行时编程方式添加行的控制(即每单元一个控制)。

I am attempting to take a 'blank' tablelayoutpanel, which has 10 columns defined, then at runtime programatically 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));

但是,这并不管用。我已经挖了一圈,发现了从设计时 myTableLayoutPanel.RowCount 使用变更运行时间,因此这样做的 myTableLayoutPanel.RowCount ++; 实际上并没有添加其他行,甚至在此之前/添加RowStyle条目后!

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!

另一个相关的问题,我遇到的是,控制将被添加到显示,但它们都只是得到的TableLayoutPanel中的点0,0处呈现,此外,他们甚至不约束是细胞内界定它们应该是在(即与码头= DockStyle.Fill它们仍出现太大/小)显示出来。

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?

干杯

推荐答案

我就在上星期。设置在TableLayoutPanel中以AddRows,或AddColumns的GrowStyle,那么你的code应该工作:

I just did this last week. Set the GrowStyle on the TableLayoutPanel to AddRows, or AddColumns, then your code should work:

//Adds "myControl" to the first column of each row
myTableLayoutPanel.Controls.Add(myControl1, 0 /* Column Index */, 0 /* Row index */);
myTableLayoutPanel.Controls.Add(myControl2, 0 /* Column Index */, 1 /* Row index */);
myTableLayoutPanel.Controls.Add(myControl3, 0 /* Column Index */, 2 /* Row index */);

下面是一些工作code,这似乎类似于你在做什么:

Here is some working code, that seems similar to what you are doing:

    private Int32 tlpRowCount = 0;

    private void BindAddress()
    {
        Addlabel(Addresses.Street);
        if (!String.IsNullOrEmpty(Addresses.Street2))
        {
            Addlabel(Addresses.Street2);
        }
        Addlabel(Addresses.CityStateZip);
        if (!String.IsNullOrEmpty(Account.Country))
        {
            Addlabel(Address.Country);
        }
        Addlabel(String.Empty); // Notice the empty label...
    }

    private void Addlabel(String text)
    {            
        label = new Label();
        label.Dock = DockStyle.Fill;
        label.Text = text;
        label.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
        tlpAddress.Controls.Add(label, 1, tlpRowCount);
        tlpRowCount++;
    }

在TableLayoutPanel中总是给我以一刀切。在我上面的例子中,我申请可能增大或缩小取决于其地址线两条,或一个国家的账户地址卡。该表的布局面板将伸展的,因为最后一排,或列,我扔的空标签,在那里强迫一个新的空行,那么一切都排队很好。

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.

下面是设计师code,所以你可以看到表我开始:

Here is the designer code so you can see the table I start with:

            // tlpAddress
        // 
        this.tlpAddress.AutoSize = true;
        this.tlpAddress.BackColor = System.Drawing.Color.Transparent;
        this.tlpAddress.ColumnCount = 2;
        this.tlpAddress.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 25F));
        this.tlpAddress.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
        this.tlpAddress.Controls.Add(this.pictureBox1, 0, 0);
        this.tlpAddress.Dock = System.Windows.Forms.DockStyle.Fill;
        this.tlpAddress.Location = new System.Drawing.Point(0, 0);
        this.tlpAddress.Name = "tlpAddress";
        this.tlpAddress.Padding = new System.Windows.Forms.Padding(3);
        this.tlpAddress.RowCount = 2;
        this.tlpAddress.RowStyles.Add(new System.Windows.Forms.RowStyle());
        this.tlpAddress.RowStyles.Add(new System.Windows.Forms.RowStyle());
        this.tlpAddress.Size = new System.Drawing.Size(220, 95);
        this.tlpAddress.TabIndex = 0;