Visual Basic Window/Form Routines
Pure VB: Vertical Splitter Bar
     
Posted:   Thursday December 26, 1996
Updated:   Monday December 26, 2011
     
Applies to:   VB3, VB4-16, VB4-32, VB5, VB6
Developed with:   VB4-32, Windows 95
OS restrictions:   None
Author:   VBnet - Randy Birch
     
 Prerequisites
None.

The following adds code to a picture box that can then be used to simulate a split-window display.

The SPLT_WIDTH parameter sets the width of the Splitter picture box. The CTRL_OFFSET variable sets the distance the controls will indent from the top and sides of the form. In this demo only one offset is used for all four sides, but separate variables for horizontal and vertical offset can be easily added to the Resize code.

Finally, should you find it difficult to "grab" the splitter, try increasing the SPLT_WIDTH around by 5 pixels. And if you can grab it, but can not see the resize bar overtop the controls when positioning, add a Picture1.ZOrder 0 command to the MouseDown event.

 BAS Module Code
None.

 Form Code
Start a new project, and on the form place a list box (List1), a text box (Text1), and a picture box (Picture1). Paste the following code into the general declarations area of the form to contain the splitter bar:

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'variable to hold the width of the splitter bar
Private Const SPLT_WDTH As Integer = 30

'variable to hold the last-sized position
Private currSplitPosX As Long

'variable to hold the horizontal 
'and vertical offsets of the 2 controls
Dim CTRL_OFFSET As Integer

'variable to hold the Splitter bar colour
Dim SPLT_COLOUR As Long

Private Sub Form_Load()

  Dim msg As String    

 'set the startup variables
  CTRL_OFFSET = 20
  SPLT_COLOUR = vbRed

 'set the current splitter bar position to an
 'arbitrary value that will always be outside
 'the possible range.  This allows us to check
 'for movement of the splitter bar in subsequent
 'mousexxx subs.
  currSplitPosX = &H7FFFFFFF
  
  List1.AddItem "Left list Item 1"
  List1.AddItem "Left list Item 2"
  List1.AddItem "Left list Item 3"
  List1.AddItem "Left list Item 4"
  List1.AddItem "Left list Item 5"

  msg = msg & "This code demonstrates how to "
  msg = msg & "implement a splitter bar in a "
  msg = msg & "Visual Basic Project." & vbCrLf & vbCrLf
  msg = msg & "It's actions are controlled through "
  msg = msg & "the MouseDown, MouseMove and MouseUp "
  msg = msg & "subs of Picture1, and the "
  msg = msg & "Resize event of the form."
  
  Text1.Text = msg
  
 'assure that the splitter will appear over the controls 
  Picture1.ZOrder 0
  
End Sub


Private Sub mnuEnd_Click()
   
   Unload Me

End Sub


Private Sub Picture1_MouseMove(Button As Integer, _
                               Shift As Integer, _
                               x As Single, y As Single)
 
  'change the icon to a left-right sizing icon   
   Picture1.MousePointer = 9
   
   If Button = vbLeftButton Then
    
     'if the splitter has been moved...
      If currSplitPosX <> &H7FFFFFFF Then
  
        'if the current position <> default, reposition
        'the splitter and set this as the current value
         If CLng(x) <> currSplitPosX Then
         
            Picture1.Move Picture1.Left + x, _
                          CTRL_OFFSET, SPLT_WDTH, _
                          ScaleHeight - (CTRL_OFFSET * 2)
                          
           currSplitPosX = CLng(x)
           
         End If
      End If  'If currSplitPosX
   End If  'If Button

End Sub


Private Sub Picture1_MouseUp(Button As Integer, _
                             Shift As Integer, _
                             x As Single, y As Single)
    
 'if the splitter has been moved...
  If currSplitPosX <> &H7FFFFFFF Then
      
     'if the current position <> the last 
     'position do a final move of the splitter
      If CLng(x) <> currSplitPosX Then
      
        Picture1.Move Picture1.Left + x, _
                     CTRL_OFFSET, SPLT_WDTH, _
                     ScaleHeight - (CTRL_OFFSET * 2)
                     
      End If
      
     'call this the default position
      currSplitPosX = &H7FFFFFFF
      
     'restore the normal splitter colour
      Picture1.BackColor = &H8000000F
     
     'and check for valid sizing.
     'Either enforce the default minimum & maximum widths for
     'the left list, or, if within range, set the width
     
      If Picture1.Left > 60 And Picture1.Left < (ScaleWidth - 60) Then
            'the pane is within range 
             List1.Width = Picture1.Left - List1.Left  
      
      ElseIf Picture1.Left < 60 Then           
            'the pane is too small
             List1.Width = 60
      
      Else 'the pane is too wide
             List1.Width = ScaleWidth - 60  
      End If
      
     'reset the cursor
      Picture1.MousePointer = 0  
      
     'reposition both lists, and the splitter bar
      Form_Resize
  
  End If
  
End Sub


Private Sub Picture1_MouseDown(Button As Integer, _
                               Shift As Integer, _
                               x As Single, y As Single)
    
    If Button = vbLeftButton Then
    
       'change the splitter colour
        Picture1.BackColor = SPLT_COLOUR
       
       'set the current position to x
        currSplitPosX = CLng(x)
    
    Else
    
       'not the left button, so...
       'if the current position <> default, cause a MouseUp
        If currSplitPosX <> &H7FFFFFFF Then
           Picture1_MouseUp Button, Shift, x, y
        End If
       
       'set the current position to the default value
        currSplitPosX = &H7FFFFFFF
    
    End If
End Sub


Private Sub Form_Resize()
    
   Dim x1 As Integer
   Dim x2 As Integer
   Dim height1 As Integer
   Dim width1 As Integer
   Dim width2 As Integer
    
   On Error Resume Next

  'set the height of the controls
   height1 = ScaleHeight - (CTRL_OFFSET * 2)
  
   x1 = CTRL_OFFSET
   width1 = List1.Width
  
   x2 = x1 + List1.Width + SPLT_WDTH - 1
   width2 = ScaleWidth - x2 - CTRL_OFFSET
  
  'move the left list
   List1.Move x1 - 1, CTRL_OFFSET, width1, height1
 
  'move the right list
   Text1.Move x2, CTRL_OFFSET, width2 + 1, height1
  
  'reposition the splitter bar
   Picture1.Move x1 + List1.Width - 1, _
                 CTRL_OFFSET, SPLT_WDTH, height1
  
End Sub
 Comments
Run the project. When the mouse passes over the splitter bar, it will change into a left-right arrow. Click & drag to a new position, and release.

 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

Hit Counter