Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TableLayoutPanel: Can't get content rows to size correctly

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.

like image 294
Plastikfan Avatar asked Feb 02 '10 17:02

Plastikfan


2 Answers

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));
    ...
like image 118
Stuart Avatar answered Oct 23 '22 03:10

Stuart


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));
like image 5
Dabize Avatar answered Oct 23 '22 01:10

Dabize