What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleMake a scrolled window that contains several other controls
DescriptionThis example shows how to make a scrolled window that contains several other controls in Visual Basic 6.
Keywordsscrolled window, scrolling, scroll bars
CategoriesGraphics, Controls
The example program displays a bunch of controls inside a scrolling area.

The picContainer PictureBox contains several controls that provide the scrolling. It holds vertical and horizontal scroll bars. It also holds PictureBoxes picInner (which contains the controls to scroll) and picOuter (which contains picInner). When you manipulate the scroll bars, the program moves picInner within picOuter to scroll the image.

The most complex code is in the ArrangeControls subroutine, which arranges the scroll bars and picOuter within picContainer. See Make a scrolled window for additional details.

' Arrange the scroll bars.
Private Sub ArrangeControls()
Dim border_width As Single
Dim got_wid As Single
Dim got_hgt As Single
Dim need_wid As Single
Dim need_hgt As Single
Dim need_hbar As Boolean
Dim need_vbar As Boolean

    ' See how much room we have and need.
    border_width = picOuter.Width - picOuter.ScaleWidth
    got_wid = picContainer.ScaleWidth - border_width
    got_hgt = picContainer.ScaleHeight - border_width
    need_wid = picInner.Width
    need_hgt = picInner.Height

    ' See if we need the horizontal scroll bar.
    If need_wid > got_wid Then
        need_hbar = True
        got_hgt = got_hgt - hbarMarble.Height
    End If

    ' See if we need the vertical scroll bar.
    If need_hgt > got_hgt Then
        need_vbar = True
        got_wid = got_wid - vbarMarble.Width

        ' See if we now need the horizontal scroll bar.
        If (Not need_hbar) And need_wid > got_wid Then
            need_hbar = True
            got_hgt = got_hgt - hbarMarble.Height
        End If
    End If

    ' Arrange the controls.
    picOuter.Move 0, 0, got_wid + border_width, got_hgt + _
    If need_hbar Then
        hbarMarble.Move 0, got_hgt + border_width, got_wid _
            + border_width
        hbarMarble.Min = 0
        hbarMarble.Max = picInner.ScaleWidth - got_wid
        hbarMarble.SmallChange = got_wid / 5
        hbarMarble.LargeChange = got_wid
        hbarMarble.Visible = True
        hbarMarble.Value = 0
        hbarMarble.Visible = False
    End If
    If need_vbar Then
        vbarMarble.Move got_wid + border_width, 0, _
            vbarMarble.Width, got_hgt + border_width
        vbarMarble.Min = 0
        vbarMarble.Max = picInner.ScaleHeight - got_hgt
        vbarMarble.SmallChange = got_hgt / 5
        vbarMarble.LargeChange = got_hgt
        vbarMarble.Visible = True
        vbarMarble.Value = 0
        vbarMarble.Visible = False
    End If
End Sub
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.