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

5

31

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?

Richard answered 25/9, 2010 at 18:30 Comment(1)
I've Found tis fantastic Rich Panel : jfblier.wordpress.com/2008/12/10/rich-panel-with-expanderRichard
K
14

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

Kablesh answered 28/3, 2011 at 16:12 Comment(0)
W
41

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

Woodbury answered 25/9, 2010 at 18:49 Comment(1)
or hook the panel2collapsed=!panel2collapsed on the panel1_clicked eventInhale
P
20

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

Easy editing in Form Designer

Preposition answered 27/9, 2013 at 15:41 Comment(0)
K
14

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

Kablesh answered 28/3, 2011 at 16:12 Comment(0)
B
8

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.

Bromic answered 27/9, 2013 at 15:52 Comment(0)
T
0

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 .

Tansy answered 21/11, 2014 at 11:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.