窗口应用程序中的可折叠面板

本文关键字:可折叠 应用程序 窗口 | 更新日期: 2023-09-27 18:35:04

我正在尝试可折叠面板。

为此,我

创建了一个表格布局并放置了 3 个可折叠面板,我使用以下代码

private void ButtonClick(System.Object sender, System.EventArgs e)
{
    Label lbl = (Label)sender;
    Panel pnl = lbl.Parent;
    foreach (Panel p in TableLayoutPanel1.Controls) {
        Label l = (Label)p.Controls(0);
        if (p.Equals(pnl)) {
            //expand or collapse the panel
            if (p.Height == 150) {
                p.Height = 25;
                //Change the imaqge name to YOUR image
                l.Image = My.Resources.Expander_Collapsed16;
            } else {
                p.Height = 150;
                //Change the imaqge name to YOUR image
                l.Image = My.Resources.Expander_Expanded16;
            }
        } else {
            p.Height = 25;
            //Change the imaqge name to YOUR image
            l.Image = My.Resources.Expander_Collapsed16;
        }
    }
}

在该代码中,我错误在以下三行中

panel pnl=lbl.Parent
Label l = (Panel)p.Controls;

我做错了什么?

窗口应用程序中的可折叠面板

是的,我以前做过这个...不是很成功,但它可以关闭面板

private void pnSearch_MouseHover(object sender, EventArgs e)
    {
        ReshapePanel(pnSearch);
    }
    private void ReshapePanel(Panel p)
    {
        int targetSize;
        int threshold;
        int changeDelay = 8000;
        decimal direction;
        if (p.Width == 0)
        {   //Expand
            targetSize = 200;
            threshold = 195;
            direction = 1;
        }
        else
        {   //Contract
            targetSize = 1;
            threshold = 5;
            direction = 2;
        }
        do
        {
            if (pnSearch.Width > threshold)
            {
                pnSearch.Width = targetSize;
            }
            else
            {
                if (direction == 2)
                {
                    pnSearch.Width = pnSearch.Width + 1;
                }
                else
                {
                    pnSearch.Width = pnSearch.Width - 1;
                }
                //pnSearch.Width = pnSearch.Width + parse.int(1 * direction);
                Task.Delay(changeDelay);
            }
        } while (pnSearch.Width != targetSize);
    }