【问题标题】:Add an expander (collapse/expand) to a Panel WinForm将扩展器(折叠/展开)添加到面板 WinForm
【发布时间】:2010-09-25 18:30:10
【问题描述】:

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

有人做过类似的事情吗?

【问题讨论】:

标签: c# winforms expander


【解决方案1】:

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

【讨论】:

  • 或将 panel2collapsed=!panel2collapsed 挂接到 panel1_clicked 事件上
【解决方案2】:

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

一般来说,它必须满足这种控制的所有基本要求。

  • 在表单设计器中轻松编辑
  • 将所需的任何控件放入内容区域
  • 应用不同的样式和尺寸

【讨论】:

    【解决方案3】:

    【讨论】:

      【解决方案4】:

      使用SplitContainer 折叠的替代方法是:

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

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

      【讨论】:

        【解决方案5】:

        我无法让«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» 事件中。并且为了让用户更清楚的扩展能力,我在正文的开头添加了字符

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-03-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多