SplitContainer in Visual Basic .NET

Tags: VB.NET, VB 2008, VB 2010, VB 2012, VB 2013

This lesson describes the SplitContainer control


SplitContainer

To move a control in the Windows Forms Designer is very easy. Just click and drag the control to its new position. In addition you can drag controls in and out of container controls (controls that contain, group, and help arrange other controls).

These controls include FlowLayoutPanel, TableLayoutPanel, GroupBox, Panel, TabControl, and SplitContainer.

The SplitContainer control allows the user to divide an area between two adjacent regions (two Panel controls in which you can place your own controls).

When the user drags the splitter between the two panels, the control resizes the panels accordingly. In addition, if you set the AutoScroll property of the panels to True, they will automatically provide scrollbars when necessary.

autoscroll property

Actually SplitContainer is helpful when the form isn't big enough to hold all the data the program must display. It is particularly useful when the user must compare values in the two areas by viewing them at the same time.

Though you can nest SplitContainers inside other SplitContainers, they are easiest to use when they separate only two areas, because large groups of SplitContainers separating many areas are usually clumsy and confusing.

You can use the Orientation property to determine whether the Panels will be arranged vertically or horizontally.

horizontal orientation

vertical orientation

If you want to take an action when the user drags the splitter, you use the SplitterMoving and SplitterMoved events.

Private Sub SplitContainer1_SplitterMoved(sender As Object, e As SplitterEventArgs) _
        Handles SplitContainer1.SplitterMoved
        Dim msg As New StringBuilder
        If SplitContainer1.Orientation = Orientation.Vertical Then
            msg.AppendFormat("the number of points moved by x: {0}", e.X)
        Else
            msg.AppendFormat("the number of points moved by y: {0}", e.Y)
        End If
        msg.AppendLine()
        MessageBox.Show(msg.ToString)
End Sub


If you want to take an action when the Panel controls resize, you use the Panel control's sizing events Resize, ResizeBegin, ResizeEnd, and SizeChanged.

Private Sub SplitContainer1_Panel1_SizeChanged(sender As Object, e As EventArgs) _
   Handles SplitContainer1.Panel1.SizeChanged
      If SplitContainer1.Orientation = Orientation.Vertical Then
         If SplitContainer1.Panel1.Width > 200 Then
            SplitContainer1.Orientation = Orientation.Horizontal
            MessageBox.Show("Panel1 can't be wider than 200 when its Orientation is vertical.")
          End If
      End If
    End Sub

Previous Lesson | Next Lesson


Share This

Home | About | Contact | Privacy Policy

Copyright visual-basic-tutorials.com 2017 - All Rights Reserved.