隐藏和显示 TableLayoutPanel 的单元格

发布于 2024-09-10 18:38:58 字数 161 浏览 6 评论 0原文

我的表格布局面板有一列三行。 (一个停靠在每个单元格中的“填充”面板。)

现在我希望能够隐藏/显示行。我希望任何时候都只显示一行(基于用户对某些单选按钮的选择),并且我希望调整大小以使其填充 TableLayoutPanel 的所有区域。

我怎样才能做到这一点?有什么想法吗?

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?

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

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

发布评论

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

评论(8

南烟 2024-09-17 18:38:58

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

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

缺⑴份安定 2024-09-17 18:38:58

我建议将其他行高度设置为 0 是最简单的方法:

第一行:

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

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

Row one:

this.tableLayoutPanel1.RowStyles[1].Height = 0;
蘸点软妹酱 2024-09-17 18:38:58

试试这个

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

Try this

TableLayoutPanel1.ColumnStyles[1].SizeType = SizeType.Absolute;
TableLayoutPanel1.ColumnStyles[1].Width = 0;
泡沫很甜 2024-09-17 18:38:58

那么为什么要使用 TableLayoutPanel 呢?

只需在表单上放置三个 Panel,填写每行的内容,并将所有三个面板的 Dock 属性设置为 Fill。将两个面板设置为 Visible = false,并将一个面板设置为 true

如果您想查看另一个面板,只需使其可见并隐藏其他两个面板(根据您的单选按钮设置)。

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

纵山崖 2024-09-17 18:38:58

我的情况类似。我需要一个包含 4 行的 TableLayoutPanel,每行都需要根据复选框选择可见。因此,我可以显示 1 - 4,而不是一次只显示一行。
设计 1 列 4 行的布局后,添加控件并将每个控件的 Dock 设置为 Fill。
然后,在复选框的单个 CheckedChanged 事件处理程序中,我进行了如下编码。这是一种蛮力方法,但是,嘿......它有效!

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

My scenario is similar. I needed a TableLayoutPanel with 4 rows each of which needed to be visible according to a checkbox selection. 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.
Then in a single CheckedChanged event handler for the checkboxes, I coded as shown below. 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();
    }
顾忌 2024-09-17 18:38:58

要隐藏行试试这个!!

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

To hide row try this!!

tableLayoutPanel1.RowStyles[1].SizeType = SizeType.Absolute;
tableLayoutPanel1.RowStyles[1].Height = 0;
冷血 2024-09-17 18:38:58

我有类似的任务要做,我的解决方案如下:

将 TableLayoutPanel 添加到您的表单(或任何容器)。

将 TableLayoutPanel 的列数和行数设置为 1,并将大小设置为 100%。

将 Dock 设置为填充。

将 GrowStyle 设置为固定大小。

将自动调整大小设置为 true。

然后以编程方式添加所有三个表单/控件,您必须根据单选按钮选择显示其中之一。确保只有其中之一可见。这可以通过初始 FirstControl.Show(); 来完成然后在每个 RadioButton 事件上隐藏当前的并显示另一个。您可以在局部变量中“记住”(例如:“currentlyVisibleControl”当前可见的引用)

注意:如果您一次会 .Show() 多个。那么 TableLayoutPanel 将引发异常,它已满并且无法添加更多项目。

PS 在我自己的示例中,我在 MDI 窗口中有 TableLayoutPanel 以及三种表单,它们在单击按钮时相互替换,因此我认为复制我的源代码将使“口头”示例变得复杂。

PPS 根据我的经验,Visual Studio 有时会在设计模式下做一些奇怪的事情。我必须删除并重新添加 TableLayoutPanel 才能正确设置属性并在设计器和运行时获得结果。因此,如果自动调整大小或绝对/百分比值未在设计器屏幕上显示,则可能是设计器的问题而不是您的问题。只需删除它并重试即可。

I had similar task to do and my solution is following:

Add a TableLayoutPanel to your form (or any container).

Set TableLayoutPanel's columns and rows count to 1 and size to 100%.

Set Dock to Fill.

Set GrowStyle to fixedSize.

Set AutoSize to 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(); and then on each RadioButton event hide the current one and show another. you may "remember" in local variable (say: "currentlyVisibleControl" the reference which is currently visible)

note: if you will .Show() more than one at time. then TableLayoutPanel wil fire the exception that it is full and can't add any more item.

P.S. 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.

P.P.S. From my experience Visual Studio does some weird things in design mode sometimes. I had to remove and re-add the TableLayoutPanel to set properties correctly and get the results both in designer and in runtime. 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.

伴梦长久 2024-09-17 18:38:58

我尝试使用 HeightSizeType 属性,但它给了我奇怪的结果。例如,目标行上的标签被隐藏,但文本框未被隐藏。

这是我使用 @arbiter 隐藏行的子控件的建议提出的扩展类。

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

    }

}

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

    }

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