Link to home
Start Free TrialLog in
Avatar of MsShadow
MsShadowFlag for Belgium

asked on

Scrollbars on a form(VB6)

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.
ASKER CERTIFIED SOLUTION
Avatar of SirNick
SirNick

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of schworak
schworak

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


VERSION 5.00
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
   End
   Begin VB.HScrollBar HScroll1
      Height          =   255
      Left            =   300
      TabIndex        =   7
      Top             =   6840
      Width           =   5535
   End
   Begin VB.VScrollBar VScroll1
      Height          =   6435
      Left            =   9240
      TabIndex        =   6
      Top             =   120
      Width           =   255
   End
   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
      End
      Begin VB.ListBox List1
         Height          =   3375
         ItemData        =   "Form1.frx":0000
         Left            =   3900
         List            =   "Form1.frx":0016
         TabIndex        =   4
         Top             =   2340
         Width           =   2955
      End
      Begin VB.ComboBox Combo1
         Height          =   315
         Left            =   900
         TabIndex        =   3
         Text            =   "Combo1"
         Top             =   1620
         Width           =   1695
      End
      Begin VB.TextBox Text1
         Height          =   315
         Left            =   2280
         TabIndex        =   2
         Text            =   "Text1"
         Top             =   780
         Width           =   2595
      End
      Begin VB.CommandButton Command1
         Caption         =   "Command1"
         Height          =   675
         Left            =   240
         TabIndex        =   1
         Top             =   120
         Width           =   1455
      End
   End
End
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
    Else
        PB.Left = 0
    End If
End Sub

Private Sub HScroll1_Scroll()
    HScroll1_Change
End Sub

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

Private Sub VScroll1_Scroll()
    VScroll1_Change
End Sub
Avatar of Richie_Simonetti
Redesign your app.
Avatar of MsShadow

ASKER

The design has been requested by the client, no way around it.
Thanks, worked perfectly.
Thanks for the points...