简体   繁体   English

如何获得展开折叠面板?

[英]How can I get Expand Collapse Panel?

I need a small button and when I press it there should come up a panel with other content inside it. 我需要一个小按钮,当我按下它时,应该会出现一个面板,里面有其他内容。 Here is example: https://gyazo.com/ce158edc3705191e2b733e2f37ea524f 例如: https//gyazo.com/ce158edc3705191e2b733e2f37ea524f

I'm using windows form application. 我正在使用Windows窗体应用程序。 Maybe I should go WPF ? 也许我应该去WPF?

I recently created a user control that was collapsible. 我最近创建了一个可折叠的用户控件。 Taking some inspiration from here: http://www.codeproject.com/Articles/53318/C-Custom-Control-Featuring-a-Collapsible-Panel and other locations 从这里获得一些启发: http : //www.codeproject.com/Articles/53318/C-Custom-Control-Featuring-a-Collapsible-Panel和其他位置

Basically you add this control onto your form, then attach a panel which should contain all of your subcontrols. 基本上,您将此控件添加到窗体上,然后附加一个面板,其中应包含所有子控件。 Then you assign the Panel control to it like this; 然后,将面板控件分配给它;

 this.collapsibleControl1.LowerPanel = this.panel1;

This was just for some small personal project, so it is not tested a lot and might contain some problems. 这仅用于一些小型个人项目,因此未经大量测试,可能包含一些问题。 But it should get your started. 但这应该可以让您开始。

Here is my usercontrol class: 这是我的usercontrol类:

public partial class CollapsibleControl : UserControl
{

    #region Declarations

    /// <summary>
    /// Declarations.
    /// </summary>
    private Control m_LowerPanel;
    private int FixedHeight = 150;
    private int CollapsibleIntervalValue = 2;
    private int HeaderHeightValue = 20;

    public delegate void CollapsedStateChangedEventHanlder(ControlState controlState);
    public event CollapsedStateChangedEventHanlder CollapsedStateChanged;

    public enum ControlState { Collapsed, Expanded}
    private ControlState _controlCollaspedState = ControlState.Expanded;

    #endregion

    #region Initialize

    public CollapsibleControl()
    {
        InitializeComponent();
    }

    #endregion

    #region Collapsible properties

    [Description("The collasped state of the control, collasped or expanded"), Category("Collapsible Control"), DefaultValueAttribute(ControlState.Expanded)]
    public ControlState ControlCollaspedState
    {
        get { return _controlCollaspedState; }
        set
        {
            this._controlCollaspedState = value;
            DoCollapsible();
        }
    }

    [Description("The test to show at the top of the control"), Category("Collapsible Control")]
    public String DisplayText
    {
        get { return this.lblTop.Text; }
        set { this.lblTop.Text = value; }
    }

    [Description("The background color of the text box"), Category("Collapsible Control"), DefaultValue(typeof(Color), "Window")]
    public Color TextBackgroundColor
    {
        get { return this.lblTop.BackColor; }
        set { this.lblTop.BackColor = value; }
    }

    /// <summary>
    /// 
    /// </summary>
    [Description("The panel which will be minimized"), Category("Collapsible Control")]
    public Control LowerPanel
    {
        get
        {
            return this.m_LowerPanel;
        }
        set
        {
            this.m_LowerPanel = value;
        }
    }

    /// <summary>
    /// Gets/Sets CollapsibleInterval
    /// </summary>
    [Description("The interval determining how long it takes to open and close"), Category("Collapsible Control"), DefaultValueAttribute(2)]
    public int CollapsibleInterval
    {
        get
        {
            return this.CollapsibleIntervalValue;
        }
        set
        {
            this.CollapsibleIntervalValue = value;
        }
    }

    /// <summary>
    /// setting values for Header height
    /// </summary>
    [Description("The height of the header"), Category("Collapsible Control"), DefaultValueAttribute(20)]
    public int HeaderHeight
    {
        get
        {
            return this.HeaderHeightValue;
        }
        set
        {
            this.HeaderHeightValue = value;
            this.lblTop.Height = this.HeaderHeightValue;
        }
    }
    #endregion

    /// <summary>
    /// Method for Collapsible animation.
    /// </summary>
    private void DoCollapsible()
    {
        if (this.m_LowerPanel == null || this.lblTop == null)
            return;

        //if expanded
        if (this.m_LowerPanel.Height > this.lblTop.Height)
        {
            FixedHeight = this.m_LowerPanel.Height;

            while (this.m_LowerPanel.Height > this.lblTop.Height)
            {
                Application.DoEvents();
                this.m_LowerPanel.Height -= CollapsibleIntervalValue;
            }
            this.lblTop.ImageIndex = 1;
            this.m_LowerPanel.Height = 0;
            //this.ControlCollaspedState = ControlState.Collapsed;
        }
        else if (this.m_LowerPanel.Height < this.lblTop.Height)  //if collapsed
        {
            int x = this.FixedHeight;
            while (this.m_LowerPanel.Height <= (x))
            {
                Application.DoEvents();
                this.m_LowerPanel.Height += CollapsibleIntervalValue;
            }
            this.lblTop.ImageIndex = 0;
            this.m_LowerPanel.Height = x;
            //this.ControlCollaspedState = ControlState.Expanded;
        }

        if (this.CollapsedStateChanged != null)
            CollapsedStateChanged(this.ControlCollaspedState);
    }

    /// <summary>
    /// CollapsiblePanel usercontrol load event.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void  CollapsibleControl_Load(object sender, EventArgs e)
    {
        this.FixedHeight = this.Height;
        this.lblTop.Height = this.HeaderHeightValue;
    }

    /// <summary>
    /// click event for Collapsible image.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void lblTop_Click(object sender, EventArgs e)
    {
        DoCollapsible();
    }

    /// <summary>
    /// setting values to label width when resizing the control.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void CollapsibleControl_Resize(object sender, EventArgs e)
    {
        this.lblTop.Width = this.Width;
    }     
}

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

相关问题 当 PaneDisplayMode 为 LeftCompact 时,如何让 Frame 折叠和展开? - How do I get the Frame to collapse and expand when PaneDisplayMode is LeftCompact? 如何获得XP主题的TreeView展开/折叠图标? - How do I get XP themed TreeView expand/collapse icons? 如何在单击事件时为面板提供折叠和展开效果 - How to give collapse and expand effect to panel on click event 我可以在 MVC 中展开/折叠表格吗? - Can I expand/collapse a table in MVC? 我可以在触发的自动生成的电子邮件中为特定数据添加展开和折叠吗? 如果是,怎么办? - Can I add expand and collapse for a specific data in the auto generated emails I trigger? If Yes, how? 将扩展器(折叠/展开)添加到Panel WinForm - Add an expander (collapse/expand) to a Panel WinForm 如何获得一个值以扩展Excel中的多行? - How can I get a value to expand over multiple rows in Excel? 如何获得鼠标位置(RFC)的面板像素的颜色 - How can I get the color of a Panel pixel at the mouse location (RFC) 如何获取在运行时更改的WPF面板的大小? - How can I get the size of a WPF panel that was changed during runtime? 如何在Windows窗体中获取按钮控件 - 面板 - How Can I Get The Button Controls in Windows Form - Panel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM