【发布时间】:2010-09-25 18:30:10
【问题描述】:
我在表单底部有一个包含 DataGridView 和 3 个按钮的面板。我想添加扩展和折叠此面板的可能性。有没有办法在 Windows 窗体应用程序中执行此操作?
有人做过类似的事情吗?
【问题讨论】:
我在表单底部有一个包含 DataGridView 和 3 个按钮的面板。我想添加扩展和折叠此面板的可能性。有没有办法在 Windows 窗体应用程序中执行此操作?
有人做过类似的事情吗?
【问题讨论】:
SplitContainer 控件可以折叠其两个面板之一。您可以为Panel1Collapsed 属性设置一个按钮。
【讨论】:
看看我的 WinForm 扩展器控件 - https://github.com/alexander-makarov/ExpandCollapsePanel
一般来说,它必须满足这种控制的所有基本要求。
【讨论】:
还有另一个 WinForms 扩展器:http://jfblier.wordpress.com/2011/02/16/window-form-expander/
【讨论】:
使用SplitContainer 折叠的替代方法是:
将面板停靠在您想要的位置,然后将其更改为Visible
属性来显示/隐藏它。这样,其他停靠的项目会在不可见时移动以填充空间(取决于它们的Dock 设置)。
例如,如果按钮、面板和标签都停靠在顶部(按此顺序),则当您隐藏面板时,标签将向上移动到按钮下方。
【讨论】:
我无法让«SplitContainer»工作(不记得细节,但我遇到了麻烦),所以今天我直接使用这个功能手动完成。要折叠控件,请将否定参数传递为 «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;
}
}
我只是在 groupBox 上放了一个标签,并将这个函数添加到标签的 «onClick» 事件中。并且为了让用户更清楚的扩展能力,我在正文的开头添加了字符⇘。
【讨论】: