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?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
aleroot
  • 71,077
  • 30
  • 176
  • 213
  • I've Found tis fantastic Rich Panel : http://jfblier.wordpress.com/2008/12/10/rich-panel-with-expander/ – aleroot Sep 30 '10 at 20:05

5 Answers5

42

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

Bradley Smith
  • 13,353
  • 4
  • 44
  • 57
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

Community
  • 1
  • 1
14

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

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
Jack
  • 156
  • 1
  • 3
7

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.

noelicus
  • 14,468
  • 3
  • 92
  • 111
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 .

Hi-Angel
  • 4,933
  • 8
  • 63
  • 86