简体   繁体   English

隐藏并显示TableLayoutPanel的单元格

[英]Hide and show a cell of the TableLayoutPanel

My tablelayout panel has one column and three rows. 我的tablelayout面板有一列三行。 (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. 我希望任何时候只能看到一行(根据用户选择的一些单选按钮),我想调整大小,以便填充TableLayoutPanel的所有区域。

How can I do that? 我怎样才能做到这一点? Any thoughts? 有什么想法吗?

如果TableLayoutPanel中的行已自动调整,则隐藏内容面板将隐藏面板放置的单元格。

I would suggest setting the other rows heights to 0 is the easiest way: 我建议将其他行高度设置为0是最简单的方法:

Row one: 第一行:

this.tableLayoutPanel1.RowStyles[1].Height = 0;

Try this 试试这个

TableLayoutPanel1.ColumnStyles[1].SizeType = SizeType.Absolute;
TableLayoutPanel1.ColumnStyles[1].Width = 0;

My scenario is similar. 我的情况类似。 I needed a TableLayoutPanel with 4 rows each of which needed to be visible according to a checkbox selection. 我需要一个包含4行的TableLayoutPanel,每行需要根据复选框选择显示。 So instead of only showing one row at a time, I can show 1 - 4. After designing the layout with 1 column and 4 rows, the controls were added and Dock set to Fill for each one. 因此,我不是一次只显示一行,而是显示1 - 4.在设计了1列和4行的布局后,添加了控件并将Dock设置为Fill for each。 Then in a single CheckedChanged event handler for the checkboxes, I coded as shown below. 然后在复选框的单个CheckedChanged事件处理程序中,我编码如下所示。 It's kind of a brute force method, but, Hey...it works! 这是一种蛮力方法,但是,嘿......它有效!

    private void checkBox_CheckedChanged(object sender, EventArgs e)
    {
        this.SuspendLayout();
        int seldCount = checkBox1.Checked ? 1 : 0;
        seldCount += checkBox2.Checked ? 1 : 0;
        seldCount += checkBox3.Checked ? 1 : 0;
        seldCount += checkBox4.Checked ? 1 : 0;

        float pcnt = 0;
        if (seldCount == 1)
            pcnt = 1;
        if (seldCount == 2)
            pcnt = 0.5f;
        if (seldCount == 3)
            pcnt = 0.33f;
        if (seldCount == 4)
            pcnt = 0.25f;

        int newHeight = (int)(tableLayoutPanel1.Height * pcnt);

        if (checkBox1.Checked)
        {
            tableLayoutPanel1.RowStyles[0].SizeType = SizeType.Percent;
            tableLayoutPanel1.RowStyles[0].Height = newHeight;
        }
        else
        {
            tableLayoutPanel1.RowStyles[0].SizeType = SizeType.Absolute;
            tableLayoutPanel1.RowStyles[0].Height = 0;
        }

        if (checkBox2.Checked)
        {
            tableLayoutPanel1.RowStyles[1].SizeType = SizeType.Percent;
            tableLayoutPanel1.RowStyles[1].Height = newHeight;
        }
        else
        {
            tableLayoutPanel1.RowStyles[1].SizeType = SizeType.Absolute;
            tableLayoutPanel1.RowStyles[1].Height = 0;
        }

        if (checkBox3.Checked)
        {
            tableLayoutPanel1.RowStyles[2].SizeType = SizeType.Percent;
            tableLayoutPanel1.RowStyles[2].Height = newHeight;
        }
        else
        {
            tableLayoutPanel1.RowStyles[2].SizeType = SizeType.Absolute;
            tableLayoutPanel1.RowStyles[2].Height = 0;
        }

        if (checkBox4.Checked)
        {
            tableLayoutPanel1.RowStyles[3].SizeType = SizeType.Percent;
            tableLayoutPanel1.RowStyles[3].Height = newHeight;
        }
        else
        {
            tableLayoutPanel1.RowStyles[3].SizeType = SizeType.Absolute;
            tableLayoutPanel1.RowStyles[3].Height = 0;
        }
        this.ResumeLayout();
    }

So why did you use a TableLayoutPanel ? 那你为什么要使用TableLayoutPanel

Just put three Panel s on your form, fill in everyone the content of each row and set the Dock property of all three panels to Fill . 只需在表单上放置三个Panel ,将每行的内容填入所有内容,并将所有三个面板的Dock属性设置为Fill Set two panels Visible = false and one to true . 将两个面板设置为Visible = false ,将一个面板设置为true

If you like to see another panel, just make it visible and hide the other two (based on your radio button settings). 如果您想看另一个面板,只需将其显示并隐藏另外两个面板(基于您的单选按钮设置)。

To hide row try this!! 要隐藏行尝试这个!!

tableLayoutPanel1.RowStyles[1].SizeType = SizeType.Absolute;
tableLayoutPanel1.RowStyles[1].Height = 0;

I had similar task to do and my solution is following: 我有类似的任务要做,我的解决方案如下:

Add a TableLayoutPanel to your form (or any container). 将TableLayoutPanel添加到表单(或任何容器)。

Set TableLayoutPanel's columns and rows count to 1 and size to 100%. 将TableLayoutPanel的列和行设置为1,将大小设置为100%。

Set Dock to Fill. 将Dock设置为Fill。

Set GrowStyle to fixedSize. 将GrowStyle设置为fixedSize。

Set AutoSize to true. 将AutoSize设置为true。

Then programmatically add all of three forms/controls, one of which you have to show depending on radio button choice. 然后以编程方式添加所有三个窗体/控件,其中一个您必须根据单选按钮选择显示。 Be sure that only one of them is visible. 确保只有其中一个可见。 That could be done with initial FirstControl.Show(); 这可以通过初始FirstControl.Show(); and then on each RadioButton event hide the current one and show another. 然后在每个RadioButton事件中隐藏当前的一个并显示另一个。 you may "remember" in local variable (say: "currentlyVisibleControl" the reference which is currently visible) 你可以在本地变量中“记住”(例如:“currentVisibleControl”当前可见的参考)

note: if you will .Show() more than one at time. 注意:如果你将.Show()不止一次。 then TableLayoutPanel wil fire the exception that it is full and can't add any more item. 然后TableLayoutPanel将触发它已满的异常,并且无法再添加任何项目。

PS In My own example I have TableLayoutPanel in MDI window and three forms which substitute each other on button clicks on them so I think copying my source code will complicate the "verbal" example. PS在我自己的例子中,我在MDI窗口中有TableLayoutPanel和三个在按钮点击时互相替换的表单,所以我认为复制我的源代码会使“口头”示例变得复杂。

PPS From my experience Visual Studio does some weird things in design mode sometimes. PPS根据我的经验,Visual Studio有时会在设计模式中做一些奇怪的事情。 I had to remove and re-add the TableLayoutPanel to set properties correctly and get the results both in designer and in runtime. 我必须删除并重新添加TableLayoutPanel以正确设置属性并在设计器和运行时获得结果。 So if either autosize or absolute/percent values are not depicted on designer screen it may be designers problem rather that yours. 因此,如果在设计器屏幕上没有描绘自动调整大小或绝对/百分比值,则可能是设计师问题而不是您的问题。 JUST DELETE IT AND RETRY. 只是删除它和重试。

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

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