【问题标题】:Collapsible Panel in windows applicationWindows 应用程序中的可折叠面板
【发布时间】:2016-02-17 10:32:01
【问题描述】:

我正在尝试折叠面板。

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

我做错了什么?

【问题讨论】:

  • 有什么异常?
  • For First Line 获取或设置控件的父容器
  • 那么对于发送行不能将'System.Windows.Forms.Controls.ControlCollection'类型转换为'System.Windows.Forms.Panel'
  • 您确定 TableLayoutPanel1 中的所有控件都是面板吗?您确定每个面板中的所有控件都是标签吗?
  • 是的,首先我放了一个表格布局面板,其中放了 2 个单元格。每个单元格都有一个带有面板标签列表框的面板

标签: c# window


【解决方案1】:

是的,我以前做过这个...虽然没有取得很大的成功,但它可以关闭面板

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

【讨论】:

    猜你喜欢
    • 2015-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多