Add an expander (collapse/expand) to a Panel WinForm

让人想犯罪 __ 提交于 2019-11-26 20:22:43

问题


I have a panel containing a DataGridView and 3 buttons at the bottom of a form. I want to add the possibility to expand and collapse this panel. Is there a way to do it in a Windows Forms application?

Has someone done something similar?


回答1:


There is another WinForms Expander : http://jfblier.wordpress.com/2011/02/16/window-form-expander/




回答2:


The SplitContainer control has the ability to collapse one of its two panels. You could rig up a button to the Panel1Collapsed property.




回答3:


Take a look at my WinForm expander control - https://github.com/alexander-makarov/ExpandCollapsePanel

In general, it must meet all the basic requirements for this kind of control.

  • Easy editing in Form Designer
  • Put any control that you want into the content region
  • Apply different styles and sizes




回答4:


An alternative to using the SplitContainer collapse is to:

Dock the panel, to where you would like it, and then change it's Visible property to show/hide it. This way other docked items will move to fill the space when it's invisible (depending on their Dock setting).

For example, if the button, panel, and a label are all docked to the top (in that order) when you hide the panel the label will shift up to underneath the button.




回答5:


I couldn't get «SplitContainer» working (don't remember the details, but I've had troubles), so today I went straight up with this function to do it manually. To collapse the control pass a negative argument as «the_sz».

    /// <summary>
    /// (In|De)creases a height of the «control» and the window «form», and moves accordingly
    /// down or up elements in the «move_list». To decrease size pass a negative argument
    /// to «the_sz».
    /// Usually used to collapse (or expand) elements of a form, and to move controls of the
    /// «move_list» down to fill the appeared gap.
    /// </summary>
    /// <param name="control">control to collapse/expand</param>
    /// <param name="form">form to get resized accordingly after the size of a control
    /// changed (pass «null» if you don't want to)</param>
    /// <param name="move_list">A list of controls that should also be moved up or down to
    /// «the_sz» size (e.g. to fill a gap after the «control» collapsed)</param>
    /// <param name="the_sz">size to change the control, form, and the «move_list»</param>
    public static void ToggleControlY(Control control, Form form, List<Control> move_list, int the_sz)
    {
        //→ Change sz of ctrl
        control.Height += the_sz;
        //→ Change sz of Wind
        if (form != null)
            form.Height += the_sz;
        //*** We leaved a gap(or intersected with another controls) now!
        //→ So, move up/down a list of a controls
        foreach (Control ctrl in move_list)
        {
            Point loc = ctrl.Location;
            loc.Y += the_sz;
            ctrl.Location = loc;
        }
    }

I just put a label over groupBox, and added this function to the «onClick» event of the label. And to make expansion ability clearer for users, I added at the beginning of the text the character .



来源:https://stackoverflow.com/questions/3795005/add-an-expander-collapse-expand-to-a-panel-winform

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!