By Peter Aitken
If you need to place more controls on a form than you can display at one time, an option is to create a tabbed interface using the TabStrip control. But sometimes the functions of the controls don't lend themselves to division among two or more tabs. In this situation, you can create a scrolling form that lets the user scroll controls into view.
Start by placing a Picture Box control on the form and naming it pbOuter. Then place a second Picture Box within the first one and name it pbInner. This is important: The second Picture Box must be a child of the first one. You might want to change the background color of one of the Picture Box controls so it's easy to tell them apart.
Now you can start placing the form's controls. They all must be placed within the second Picture Box, pbInner. You can increase the Picture Box size, both height and width, as needed to accommodate all the controls. The size of pbOuter doesn't matter; it will be adjusted in code when the program runs. The size of pbInner should be as tall as necessary to fit all of your controls, but no wider than the form. Add a vertical scroll bar on the form itself. Set the scroll bar to the desired width (the height doesn't matter at this time). Finally, add a horizontal scroll bar on the form and set its height as desired (the width doesn't matter).
The remaining action takes place in code. First, put code in the Form_Resize event procedure that positions the scroll bars against the right and bottom edges of the form and sizes pbOuter to fill the remainder of the form:VScroll1.Move Me.ScaleWidth - VScroll1.Width, 0, VScroll1.Width,
HScroll1.Move 0, Me.ScaleHeight - HScroll1.Height, Me.ScaleWidth - _
pbOuter.Move 0, 0, Me.ScaleWidth - VScroll1.Width, _
Me.ScaleHeight - HScroll1.Height
You also need code in Form_Resize to determine if pbOuter is large enough to display all of pbInner without scrolling. In this case, you should hide one or both scroll bars. If it's not large enough, you need to show the scroll bars and set the scrolling parameters for the scroll bars.If pbInner.Height <= pbOuter.Height Then
VScroll1.Visible = False
VScroll1.Visible = True
VScroll1.Max = pbInner.Height - pbOuter.Height
VScroll1.LargeChange = 2500
VScroll1.SmallChange = 250
If pbInner.Width <= pbOuter.Width Then
HScroll1.Visible = False
HScroll1.Visible = True
HScroll1.Max = pbInner.Width - pbOuter.Width
HScroll1.LargeChange = 2500
HScroll1.SmallChange = 250
Finally, you must place code in the Change event procedures for the scroll bars to perform the scrolling:Private Sub HScroll1_Change()
pbInner.Left = -HScroll1.Value
Private Sub VScroll1_Change()
pbInner.Top = -VScroll1.Value
By using this technique, you can place as many controls as necessary on a form and the user will be able to scroll them into view as needed.
Peter Aitken has been programming with Visual Basic since Version 1.0. He has written numerous books and magazine articles on Visual Basic and other computer and programming topics.