将扩展器(折叠/展开)添加到Panel WinForm

我有一个面板包含一个DataGridView和一个窗体底部的3个按钮。 我想添加扩展和折叠此面板的可能性。 有没有办法在Windows窗体应用程序中执行此操作?

有人做过类似的事吗?

还有另一个WinForms扩展器: http : //jfblier.wordpress.com/2011/02/16/window-form-expander/

SplitContainer控件可以折叠其两个面板中的一个。 您可以为Panel1Collapsed属性设置一个按钮。

看看我的WinForm扩展器控件 – https://github.com/alexander-makarov/ExpandCollapsePanel

通常,它必须满足这种控制的所有基本要求。

  • 在Form Designer中轻松编辑
  • 将您想要的任何控件放入内容区域
  • 应用不同的款式和尺寸

在Form Designer中轻松编辑

使用SplitContainer折叠的另一种方法是:

将面板停靠到您希望的位置,然后将其更改为Visible属性以显示/隐藏它。 这样,当其不可见时,其他停靠的项目将移动以填充空间(取决于其Dock设置)。

例如,如果隐藏面板时按钮,面板和标签都停靠在顶部(按此顺序),则标签将向上移动到按钮下方。

我无法让«SplitContainer»工作(不记得细节,但我遇到了麻烦) ,所以今天我直接用这个function手动完成。 要折叠控件,请将负参数作为«the_sz»传递。

  ///  /// (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. ///  /// control to collapse/expand /// form to get resized accordingly after the size of a control /// changed (pass «null» if you don't want to) /// A list of controls that should also be moved up or down to /// «the_sz» size (eg to fill a gap after the «control» collapsed) /// size to change the control, form, and the «move_list» public static void ToggleControlY(Control control, Form form, List 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; } } 

我只是在groupBox上添加了一个标签,并将此函数添加到标签的«onClick»事件中。 为了使用户的扩展能力更加清晰,我在文本的开头添加了字符