Home > Posts > .NET WinForms splitter with normal/maximize/normal/shrink cycling action at double-click

.NET WinForms splitter with normal/maximize/normal/shrink cycling action at double-click

The following code is from the application I authored for LeViS (see screenshot at that link). I had a vertical splitter (=splitting horizontally) and both left & right panes where split again with horizontal splitters (=splitting vertically) themselves and needed to double-click a splitter to make it cycle between normal/maximized/normal/shrunk state.

Java JFC/Swing’s JSplitter control can show two small button arrows (called accelerator buttons if I remember well) on the splitter that do the maximize/shrink/restore cycle actions, but didn’t find something similar for .NET WinForms, so I implemented the cycle via double-click (keeping at a local variable the current movement direction [a pendulum "cycle"])

So here’s the code I authored to make this work (the real worker is the "splitCycle" method near the end)

#Region "Splitter double-click"

    Protected splitHorizontally_DoubleClickToUp As Boolean = False
    Protected splitLeftVertically_DoubleClickToLeft As Boolean = False
    Protected splitRightVertically_DoubleClickToLeft As Boolean = False

    Private Sub splitHorizontal_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles splitHorizontally.MouseDoubleClick
        splitCycle(splitHorizontally, splitHorizontally.ClientSize.Width, splitHorizontally_DoubleClickToUp)
    End Sub

    Private Sub splitLeftVertical_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles splitLeftVertically.MouseDoubleClick
        splitCycle(splitLeftVertically, splitLeftVertically.ClientSize.Height, splitLeftVertically_DoubleClickToLeft)
    End Sub

    Private Sub splitRightVertically_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles splitRightVertically.MouseDoubleClick
        splitCycle(splitRightVertically, splitRightVertically.ClientSize.Height, splitRightVertically_DoubleClickToLeft)
    End Sub

    Protected Const SPLITTER_MARGIN As Integer = 10    ‘need an accuracy margin >0 since the user can never move with the mouse the splitter exactly to its bounds

    Private Sub splitCycle(ByVal splitter As SplitContainer, ByVal max As Integer, ByRef direction As Boolean)
        With splitter
            If .SplitterDistance <= .Panel1MinSize + SPLITTER_MARGIN Then
                .SplitterDistance = CInt(max / 2)
                direction = False
            ElseIf .SplitterDistance >= max – .Panel2MinSize – .SplitterWidth – SPLITTER_MARGIN Then
                .SplitterDistance = CInt(max / 2)
                direction = True
            ElseIf direction Then
                .SplitterDistance = .Panel1MinSize
                direction = False
            Else
                .SplitterDistance = max – .Panel2MinSize – .SplitterWidth
                direction = True
            End If
        End With
    End Sub

#End Region

Advertisements
Categories: Posts
  1. 2014/08/31 at 17:29

    I don’t even know the way I ended up right here, but I
    thought this post was once good. I do not realize who you are but certainly you’re going to
    a famous blogger if you happen to are not already. Cheers!

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: