TableLayoutPanel:无法使内容行的大小正确

发布于 2024-08-20 00:21:52 字数 2578 浏览 8 评论 0原文

我正在使用 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,其方式与最后一行的设置完全相同,但这似乎不起作用。第二行(中间)的 RowStyle 大小设置为 SizeType.AutoSize,我认为这意味着耗尽剩余空间,因此不要显式设置大小。我可能是错的,但我不确定。

I am using a TableLayoutPanel to split a client area into 3 rows (there is only 1 column). The top and bottom rows are designed to be of fixed height; they will contain a header and a footer which initially each contain a child label control that contains static text (just to start off with). The middle row should be dynamically sized to fill remaining area. This middle pane will eventually contain a listview. I have a manager class that takes as an argument the panel (ExplorerTableLayoutPanel) object being managed:

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

There are 3 methods that create each of the 3 rows in the table layout:

    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);
    }

The problem I am seeing is that the last row is taking up the fixed row height which I have requested as 30. This part is correct. However, the first and second rows are splitting the remaining space equally between them which is not what I want. As you can see, I have explicitly set the row height to 30 for the first row in exactly the same manner as done for the last row, but this doesn't appear to work. The 2nd row (middle) has its RowStyle size set to SizeType.AutoSize which I though was meant to mean use up the remaining space, so don't explicitly set the size. I may be wrong but I'm not sure.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夕嗳→ 2024-08-27 00:21:52

我还没有测试您的代码,但乍一看:

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));
    ...

I haven't tested your code but at a glance:

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

Since you want to fill the remaining space don't want SizeType.AutoSize otherwise the body row will try to shrink to fit the label, even though the label is set to DockStyle.Fill. What you want is to make the row fill up all the space it can by using SizeType.Percent:

private void AddBodyRow()    
{
    LayoutPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 100f));
    ...
昇り龍 2024-08-27 00:21:52

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

.RowStyles.Clear();

然后新样式才能工作:

.RowStyles.Add(new RowStyle(SizeType.AutoSize));

I was struggling a long time with the correct resizing behavior of this object until I found out, that I need to delete the existing styles first, added by the designer tool:

.RowStyles.Clear();

Then the new styles are working:

.RowStyles.Add(new RowStyle(SizeType.AutoSize));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文