Search Amazon:

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
      Next
  
      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

Instructions

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

Sign In
  User Id 
  Password 


Submit Your Own Code and Articles




About TheScarms
About TheScarms

Ask me your programming questions

I read every email and answer all I can.

User Feedback: Be the first to add a comment! Items to Show:     

     
You must log in to post feedback.
Comment:    
 

If you use this code, please mention "www.TheScarms.com"

Email this page


TheScarms AppSentinel lets you securely copy protect and create evaluation versions of your software

TheScarms(tm) AppSentinel lets you quickly and easily create evaluation versions of your software and stop unauthorized copying and unregistered use of your programs!

Get your free
trial copy today!


      The World's Number 1 Web Host

© Copyright 2008 TheScarms