折叠“按钮”以进行拆分容器控制

时间:2011-06-16 23:37:10

标签: c# .net winforms collapse splitcontainer

我在 WinForms 桌面应用程序中使用 Microsoft splitcontainer control (.net 4 / c#/ vs 2010)。

我想在spliiter控件的面板之间放一个小按钮(或任何漂亮的UI元素)来折叠其中一个面板。例如。一个“按钮”有两个部分,如果我点击一个部分,右侧面板会折叠,如果我点击另一个部分,左侧面板会折叠。

我怎么能解决这个问题?

提前谢谢!

3 个答案:

答案 0 :(得分:8)

你必须为此编写自己的活动。你必须决定设计。希望你需要下面的东西。

private void radButton1_Click(object sender, EventArgs e) 
{ 
    splitPanel1.Collapsed = !splitPanel1.Collapsed; 
}

编辑1

你认为没有简单的方法。看一看herehere即可了解相关信息。

编辑2

您可以向两个面板添加两个toolStrips:Dock:Top并添加两个按钮,如下图所示,看起来非常好。只是一个想法...

enter image description here

<强> EDIT3

Splitter是另一种选择。 看看here

答案 1 :(得分:3)

我在我的实现中使用过这个解决方案,对你来说可能为时已晚,但可能对其他人有帮助。

在我的实现中,我还将控件从一个面板移动到另一个面板,这就是为什么我只是将面板折叠状态更改为最后一个操作。

由于我无法发布任何图像,只需按照下图解析([&lt;]和[&gt;]是按钮):

 ╔════════════╤═════════════╗
 ║         [<]│[>]          ║ 
 ║            │             ║ 
 ║            │             ║ 
 ║            │             ║ 
 ║            │             ║ 
 ║            │             ║ 
 ╚════════════╧═════════════╝

以下是左侧面板(panel1)的实现,类似的功能也用于右侧面板。

    private void setSplitterLeftPanelCollapsedState(bool collapse)
    {
        splitContainer1.SuspendLayout();

        // Collapse the left panel
        if (collapse)
        {
            if (!splitContainer1.Panel1Collapsed)
            {
                // restoring the panel in the end to apply layout changes
                buttonOpenPanel1.Text = ">";
                splitContainer1.Panel1Collapsed = true;
            }
        }
        // Open the left panel
        else
        {
            if (splitContainer1.Panel1Collapsed)
            {
                // collapsing the panel in the end to apply layout changes
                buttonOpenPanel1.Text = "<";
                splitContainer1.Panel1Collapsed = false;
            }
        }

        splitContainer1.ResumeLayout();

        comboBoxSearchText.Focus();
    }

答案 2 :(得分:2)

受Lotus Notes布局的启发,我设计了一些我认为在这种情况下有用的东西。它只在面板之间包含一个按钮,用于切换单个面板的展开/折叠状态,但可以轻松修改为使用两个按钮来控制右面板和左面板。它使用两个拆分容器,一个停靠在另一个容器内,以及“中间”面板的mouseMove事件,以模拟拖动拆分器(Moving a control by dragging it with the mouse in C#)。 另外,我使用容器的ClientSizedChanged事件来处理切换按钮图像的逻辑,而不是折叠/展开面板的方法(Detect when SplitContainer collapsed changes)。

设计:

splitContainer1
╔════════════╤═════════════════════════════════╗
║            │ splitContainer2 (docked fill)   ║
║            │ ╔════════════╤════════════════╗ ║
║            │ ║            │                ║ ║
║            │ ║ Button(s)  │                ║ ║
║            │ ║ [<>]       │                ║ ║
║            │ ║            │                ║ ║
║            │ ╚════════════╧════════════════╝ ║
╚════════════╧═════════════════════════════════╝

splitContainer2.Dock = DockStyle.Fill;
splitContainer1 = splitContainer2.IsSplitterFixed = true;
splitContainer2.Panel1.Cursor = Cursors.VSplit;

向左或向右锚定按钮(或在tableLayout控件内停靠几个按钮)。只需确保面板的某些部分可用于单击/拖动。
将中间面板的最大值设置为较小的数字。大小取决于您需要按钮的宽度。

<强>代码:

面板将切换到相反状态
如果你真的需要一个带有两个部分的按钮而不是两个按钮或一个切换按钮,你需要点击鼠标坐标,并根据点击发生的位置有不同的逻辑。

private void btnExpand_Click(object sender, EventArgs e)
{
    splitContainer1.Panel1Collapsed = !splitContainer1.Panel1Collapsed;  
}

与展开/折叠相关联的亨德尔逻辑。我选择使用此事件,因为在我的程序中有多种方法可以让用户折叠/展开面板。

    private void splitContainer1_Panel2_ClientSizeChanged(object sender, EventArgs e)
    {

        if (splitContainer1.Panel1Collapsed)
        {
            splitContainer2.Panel1.Cursor = Cursors.Default;
            this.btnExpand.Image =  imageExpand;
        }
        Else
       {
            splitContainer2.Panel1.Cursor = Cursors.VSplit;
            this.btnExpand.Image = imageCollapse;
      }
    }

Handel由于移动了人造分离器而调整了面板的大小

    private void splitContainer2_Panel1_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {

    /* All you really need is this:
                splitContainer1.SplitterDistance += e.X;  

    Note: Splitter distance must be a positive integer and e.X will be negitive when dragging to the left of the splitContainer. You could handel this check here or on the splitterMoving event.

        The code I have below shows how to “snap” a panel closed if the splitter is moved close enough to the edge 
    Or prevent a panel from being hidden from view (which could also be accomplished by setting the minimum size of a panel). 

    */

            if (e.X + splitContainer1.SplitterDistance < 40)
            {

               while (splitContainer1.SplitterDistance > 1)
                    splitContainer1.SplitterDistance--;
                splitContainer1.Panel1Collapsed = true;

            }
            else if ((e.X + splitContainer1.SplitterDistance) * 1.00 / this.Width * 1.00 < .75)
                splitContainer1.SplitterDistance += e.X;  
            else
                Cursor.Current = Cursors.No;


        }
}
相关问题