简体   繁体   English

TableLayoutPanel的行高不正确

[英]TableLayoutPanel has incorrect row height

I have auto-sized 2x2 table layout and long auto-sized labels in each cell. 我在每个单元格中都有自动调整大小的2x2表格布局和长自动调整大小的标签。 This layout is in other table layout with no-auto-sized cells. 此布局采用其他表格布局,没有自动调整大小的单元格。 Minimal project to reproduce the problem: 重现问题的最小项目:

using System;
using System.Windows.Forms;

namespace TestForms {
    static class Program {
        [STAThread]
        static void Main() {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new TestForm());
        }
    }

    class TestForm : Form {
        public TestForm() {
            var childPanel = new TableLayoutPanel();
            var label8 = new Label();
            var label9 = new Label();
            var label10 = new Label();
            var label7 = new Label();
            var rootPanel = new TableLayoutPanel();

            childPanel.AutoSize = true;
            childPanel.AutoSizeMode = AutoSizeMode.GrowAndShrink;
            childPanel.BackColor = System.Drawing.Color.Silver;
            childPanel.ColumnCount = 2;
            childPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50F));
            childPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50F));
            childPanel.Controls.Add(label8, 1, 0);
            childPanel.Controls.Add(label9, 0, 1);
            childPanel.Controls.Add(label10, 1, 1);
            childPanel.Controls.Add(label7, 0, 0);
            childPanel.Dock = DockStyle.Top;
            childPanel.RowCount = 2;
            childPanel.RowStyles.Add(new RowStyle());
            childPanel.RowStyles.Add(new RowStyle());

            label8.AutoSize = true;
            label8.Text = "2ggggggggggggggggg";

            label9.AutoSize = true;
            label9.Text = "label9";

            label10.AutoSize = true;
            label10.Text = "label10";

            label7.AutoSize = true;
            label7.Text = "label7";

            rootPanel.ColumnCount = 1;
            rootPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100F));
            rootPanel.Controls.Add(childPanel, 0, 0);
            rootPanel.Dock = DockStyle.Fill;
            rootPanel.RowCount = 1;
            rootPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 100F));

            ClientSize = new System.Drawing.Size(205, 197);
            Controls.Add(rootPanel);
        }
    }
}

I get the following result: 我得到以下结果:

截图

Why last row got the wrong height? 为什么最后一排的高度错误? Is there a workaround? 有解决方法吗?

Interestingly, seems like you have hit some bug since it doesn't happen if some of the labels AutoSize is false , or making the first column style SizeType.AutoSize etc. 有趣的是,似乎你遇到了一些bug,因为如果某些标签AutoSizefalse ,或者使第一列样式为SizeType.AutoSize等,则不会发生这种情况。

You can use the following workaround. 您可以使用以下解决方法。 It's ugly, but that's the only way I've found. 这很难看,但这是我找到的唯一方法。 The idea is to add invisible row containing fake Label with AutoSize set to false . 我们的想法是添加包含伪Label不可见行,并将AutoSize设置为false For instance, after setting up the normal layout, add the following lines: 例如,在设置正常布局后,添加以下行:

childPanel.RowCount++;
childPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, 0));
childPanel.Controls.Add(new Label { AutoSize = false }, 0, childPanel.RowCount - 1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM