Windows应用程序中的可折叠面板

时间:2016-02-17 10:32:01

标签: c# window

我正在尝试使用可折叠面板。

为此我创建了一个表格布局并为可折叠放置了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;

我错了什么?

1 个答案:

答案 0 :(得分:0)

是的,我之前已经这样做了......没有取得很大的成功,但它可以关闭小组

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