5

我正在使用 TableLayoutPanel 将客户区拆分为 3 行(只有 1 列)。顶排和底排设计为固定高度;它们将包含一个页眉和一个页脚,最初每个都包含一个包含静态文本的子标签控件(只是开始)。中间行应动态调整大小以填充剩余区域。这个中间窗格最终将包含一个列表视图。我有一个管理器类,它将被管理的面板(ExplorerTableLayoutPanel)对象作为参数:

public class ExplorerTableLayoutPanelManager
{       
    public ExplorerTableLayoutPanelManager(ExplorerTableLayoutPanel panel)
    {
        LayoutPanel = panel;
    }

有 3 种方法可以在表格布局中创建 3 行中的每一行:

    private void AddHeaderRow()
    {
        const int headerHeight = 30;
        LayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, headerHeight));

        Label label = new Label();
        label.BackColor = Color.AliceBlue;
        label.BorderStyle = BorderStyle.None;
        label.ForeColor = Color.LightGray;
        label.TextAlign = ContentAlignment.MiddleRight;
        label.Text = "Header Banner";
        label.Dock = DockStyle.Fill;
        float size = label.Font.SizeInPoints;
        label.Font = new Font(label.Font.Name, size * 2);

        const int column = 0, row = 0;
        LayoutPanel.Controls.Add(label, column, row);
    }


    private void AddBodyRow()
    {
        LayoutPanel.RowStyles.Add(new RowStyle(SizeType.AutoSize));

        Label label = new Label();
        label.BorderStyle = BorderStyle.FixedSingle;
        label.ForeColor = Color.LightGray;
        label.TextAlign = ContentAlignment.MiddleCenter;
        label.Text = "Content Under construction ...";
        label.Dock = DockStyle.Fill;

        float size = label.Font.SizeInPoints;
        label.Font = new Font(label.Font.Name, size * 2);

        const int column = 0, row = 1;
        LayoutPanel.Controls.Add(label, column, row);
    }


    private void AddFooterRoow()
    {
        const int footerHeight = 30;
        LayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, footerHeight));

        Label label = new Label();
        label.BackColor = Color.AliceBlue;
        label.BorderStyle = BorderStyle.None;
        label.ForeColor = Color.LightGray;
        label.TextAlign = ContentAlignment.MiddleRight;
        label.Text = "Footer Banner";
        label.Dock = DockStyle.Fill;

        float size = label.Font.SizeInPoints;
        label.Font = new Font(label.Font.Name, size * 2);

        const int column = 0, row = 2;
        LayoutPanel.Controls.Add(label, column, row);
    }

我看到的问题是最后一行占用了我要求的固定行高 30。这部分是正确的。但是,第一行和第二行在它们之间平均分配剩余空间,这不是我想要的。如您所见,我已将第一行的行高显式设置为 30,其方式与最后一行完全相同,但这似乎不起作用。第 2 行(中间)的 RowStyle 大小设置为 SizeType.AutoSize,虽然我的意思是用完剩余空间,所以不要明确设置大小。我可能错了,但我不确定。

4

2 回答 2

11

我没有测试你的代码,但一目了然:

private void AddBodyRow()    
{
    LayoutPanel.RowStyles.Add(new RowStyle(SizeType.AutoSize));
    ...

由于您要填充剩余空间,因此不需要 SizeType.AutoSize,否则正文行将尝试缩小以适合标签,即使标签设置为 DockStyle.Fill。您想要的是使用 SizeType.Percent 使该行填满它可以填满的所有空间:

private void AddBodyRow()    
{
    LayoutPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 100f));
    ...
于 2010-04-07T00:22:29.623 回答
5

我在这个对象的正确调整大小行为上苦苦挣扎了很长时间,直到我发现我需要先删除由设计器工具添加的现有样式:

.RowStyles.Clear();

然后新样式正在起作用:

.RowStyles.Add(new RowStyle(SizeType.AutoSize));
于 2011-07-15T07:54:35.820 回答