Hide and show a cell of the TableLayoutPanel

2020-06-01 06:05发布

My tablelayout panel has one column and three rows. (one docked to Fill panel in each cell.)

Now I would like to be able to hide/show the rows . I want only one row to be visible at any time ( based on a user selection of some radio buttons) and I want to to get resized so it fills all the area of the TableLayoutPanel.

How can I do that? Any thoughts?

8条回答
兄弟一词,经得起流年.
2楼-- · 2020-06-01 06:43

If rows in your TableLayoutPanel is autosized then hiding content panel will hide cell where panel placed too.

查看更多
Bombasti
3楼-- · 2020-06-01 06:43

Try this

TableLayoutPanel1.ColumnStyles[1].SizeType = SizeType.Absolute;
TableLayoutPanel1.ColumnStyles[1].Width = 0;
查看更多
戒情不戒烟
4楼-- · 2020-06-01 06:44

So why did you use a TableLayoutPanel?

Just put three Panels on your form, fill in everyone the content of each row and set the Dock property of all three panels to Fill. Set two panels Visible = false and one to true.

If you like to see another panel, just make it visible and hide the other two (based on your radio button settings).

查看更多
相关推荐>>
5楼-- · 2020-06-01 06:44

I tried fooling around with the Height and SizeType properties, but it was giving me odd results. For example, the Labels on the target row were being hidden, but the TextBoxes were not.

Here is an extension class that I came up with using @arbiter's suggestion of hiding the children Controls of the row.

// these methods only works on rows that are set to AutoSize
public static class TableLayoutPanelExtensions
{

    public static void HideRows(this TableLayoutPanel panel, params int[] rowNumbers)
    {
        foreach (Control c in panel.Controls)
        {
            if (rowNumbers.Contains(panel.GetRow(c)))
                c.Visible = false;
        }
    }

    public static void ShowRows(this TableLayoutPanel panel, params int[] rowNumbers)
    {
        foreach (Control c in panel.Controls)
        {
            if (rowNumbers.Contains(panel.GetRow(c)))
                c.Visible = true;
        }

    }

}
查看更多
贪生不怕死
6楼-- · 2020-06-01 06:49

To hide row try this!!

tableLayoutPanel1.RowStyles[1].SizeType = SizeType.Absolute;
tableLayoutPanel1.RowStyles[1].Height = 0;
查看更多
手持菜刀,她持情操
7楼-- · 2020-06-01 06:51

I would suggest setting the other rows heights to 0 is the easiest way:

Row one:

this.tableLayoutPanel1.RowStyles[1].Height = 0;
查看更多
登录 后发表回答