Create a Scrollable VB Form

I have been asked a million times: "How do I create a form I can scroll?"   Well here it is. All you need is a scrollbar control and a few lines of code. For a sample of creating a scrollable form and adding controls to is at runtime, see my Dynamically Add Controls sample.

Create a Form that you can Scroll
Download Source Code

Form Code

Create a form with a scrollbar on it.

Copy the following code to the Declarations section of a Form:

   Dim oldPos As Integer

Copy the following code to the Form Load event:

   Private Sub Form_Load()
   Dim iFullFormHeigth As Integer
   Dim iDisplayHeight As Integer
   iFullFormHeigth = 3765
   iDisplayHeight = 1800

   Me.Height = iDisplayHeight
   With VScroll1
      .Height = Me.ScaleHeight
      .Min = 0
      .Max = iFullFormHeigth - iDisplayHeight
      .SmallChange = Screen.TwipsPerPixelY * 10
      .LargeChange = .SmallChange
   End With
   End Sub

Create this procedure:

   Private Sub pScrollForm()
      Dim ctl As Control
      For Each ctl In Me.Controls
         If Not (TypeOf ctl Is VScrollBar) And _
            Not (TypeOf ctl Is CommandButton) Then
             ctl.Top = ctl.Top + oldPos - VScroll1.Value
         End If
      oldPos = VScroll1.Value
   End Sub

Call it from these events:

   Private Sub VScroll1_Change()
      Call pScrollForm
   End Sub
Private Sub VScroll1_Scroll() Call pScrollForm End Sub


Download this project and run it. Click away on the scroll bar.

About TheScarms
About TheScarms

Sample code
version info

If you use this code, please mention ""

Email this page

© Copyright 2022 TheScarms
Goto top of page