Scrollbars on a form(VB6)

Posted on 2002-07-02
Last Modified: 2011-10-03
How do I put working scrollbars on a form. I dynamically create some controls and there is no other way than to pass the size of the form. Yet I am unabler to add working scrollbars to it.
Question by:MsShadow

Accepted Solution

SirNick earned 50 total points
ID: 7124734
Hello MsShadow

Have a look at the following link.

Hope this helps...

Expert Comment

ID: 7124780
There is a really simple trick to this....

Create a PICTURE BOX the size of your form when it is at it's maximum size. Place all your controls inside this picture box (no border is needed on the picture box by the way)

Place your scroll bars on the form (not the picture box) Then you can use the value of the scrollers to move the picture box and all the controlls will move properly.

Realize this is a quick and dirty example and you will need to tweek on it to get the exact effect you are after.

Paste the code below into a file named PicBox.frm (or what ever) and see how this works...

Begin VB.Form Form1
   Caption         =   "Form1"
   ClientHeight    =   7620
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   10350
   LinkTopic       =   "Form1"
   ScaleHeight     =   7620
   ScaleWidth      =   10350
   StartUpPosition =   3  'Windows Default
   Begin VB.CommandButton Command2
      Height          =   255
      Left            =   6780
      TabIndex        =   8
      Top             =   6900
      Width           =   255
   Begin VB.HScrollBar HScroll1
      Height          =   255
      Left            =   300
      TabIndex        =   7
      Top             =   6840
      Width           =   5535
   Begin VB.VScrollBar VScroll1
      Height          =   6435
      Left            =   9240
      TabIndex        =   6
      Top             =   120
      Width           =   255
   Begin VB.PictureBox PB
      BorderStyle     =   0  'None
      Height          =   6075
      Left            =   300
      ScaleHeight     =   6075
      ScaleWidth      =   8475
      TabIndex        =   0
      Top             =   360
      Width           =   8475
      Begin VB.FileListBox File1
         Height          =   3405
         Left            =   360
         TabIndex        =   5
         Top             =   2460
         Width           =   3195
      Begin VB.ListBox List1
         Height          =   3375
         ItemData        =   "Form1.frx":0000
         Left            =   3900
         List            =   "Form1.frx":0016
         TabIndex        =   4
         Top             =   2340
         Width           =   2955
      Begin VB.ComboBox Combo1
         Height          =   315
         Left            =   900
         TabIndex        =   3
         Text            =   "Combo1"
         Top             =   1620
         Width           =   1695
      Begin VB.TextBox Text1
         Height          =   315
         Left            =   2280
         TabIndex        =   2
         Text            =   "Text1"
         Top             =   780
         Width           =   2595
      Begin VB.CommandButton Command1
         Caption         =   "Command1"
         Height          =   675
         Left            =   240
         TabIndex        =   1
         Top             =   120
         Width           =   1455
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Private Sub Command2_Click()
    VScroll1.Value = 0
    HScroll1.Value = 0
End Sub

Private Sub Form_Load()
    HScroll1.SmallChange = Screen.TwipsPerPixelY
    HScroll1.LargeChange = HScroll1.SmallChange * 25
    VScroll1.SmallChange = Screen.TwipsPerPixelX
    VScroll1.LargeChange = VScroll1.SmallChange * 25
    PB.Move 0, 0
    PB.ZOrder 1
End Sub

Private Sub Form_Resize()
    VScroll1.Value = 0
    VScroll1.Move Me.ScaleWidth - VScroll1.Width, 0, VScroll1.Width, Me.ScaleHeight - HScroll1.Height
    VScroll1.Visible = (Me.ScaleHeight < PB.Height)
    VScroll1.Max = Me.ScaleHeight - PB.Height
    HScroll1.Value = 0
    HScroll1.Move 0, Me.ScaleHeight - HScroll1.Height, Me.ScaleWidth - VScroll1.Width, HScroll1.Height
    HScroll1.Visible = (Me.ScaleWidth < PB.Width)
    HScroll1.Max = Me.ScaleWidth - PB.Width
    Command2.Move VScroll1.Left, HScroll1.Top, VScroll1.Width, HScroll1.Height
    Command2.Visible = (VScroll1.Visible Or HScroll1.Visible)
End Sub

Private Sub HScroll1_Change()
    If HScroll1.Visible Then
        PB.Left = HScroll1.Value
        PB.Left = 0
    End If
End Sub

Private Sub HScroll1_Scroll()
End Sub

Private Sub VScroll1_Change()
    If VScroll1.Visible Then
        PB.Top = VScroll1.Value
        PB.Top = 0
    End If
End Sub

Private Sub VScroll1_Scroll()
End Sub
LVL 16

Expert Comment

ID: 7125825
Redesign your app.
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

LVL 19

Author Comment

ID: 7126082
The design has been requested by the client, no way around it.
LVL 19

Author Comment

ID: 7126253
Thanks, worked perfectly.

Expert Comment

ID: 7127289
Thanks for the points...

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question