home account info subscribe login search FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Advanced Visual Basic Techniques
(Publisher: John Wiley & Sons, Inc.)
Author(s): Rod Stephens
ISBN: 0471188816
Publication Date: 06/01/97

Search this book:
 
Previous Table of Contents Next


Viewport’s ScaleHeight property determines how much space is available within Viewport. If Scroll-Area has height H and Viewport has a ScaleHeight value of S, then the farthest distance ExpenseReporter will ever need to move the top of ScrollArea is H – S. If the ScrollArea were moved farther, empty space would be displayed below the ScrollArea controls. To make the scroll bar move ScrollArea no farther than this, ExpenseReporter sets the scroll bar’s Max property to ScrollArea.Height – Viewport.ScaleHeight. Figure 1.6 schematically shows the situation when ScrollArea has been moved up as far as possible.


FIGURE 1.5  The Viewport and ScrollArea picture boxes.

Scroll bars have two other properties that affect the user’s control. The SmallChange property determines by what amount the scroll bar’s value changes when the user presses the scroll bar’s arrow buttons. This value should be small enough that most of the currently displayed fields are still visible after the ScrollArea is moved, but large enough to make some new fields become visible. ExpenseReporter sets this value to be the height of one row of expense fields. When the user presses one of the scroll bar arrows, one new row of fields becomes visible.

A scroll bar’s LargeChange property determines by what amount the scroll bar’s value changes when the user clicks on the scroll bar between the scroll bar’s slider and its arrow buttons. When the user clicks in this area, most or all of the currently visible data should be moved out of view so that the Viewport shows mostly new data.

Some applications move the scrolling area by the full size of the viewport when the user triggers a LargeChange. Others move a large fraction of this distance (80 or 90 percent) so that some of the old information remains visible to give the user some continuity. This is particularly common with applications that display maps or pictures. ExpenseReporter moves the ScrollArea by the full height of the Viewport.


FIGURE 1.6  The ScrollArea moved up as far as possible.

When ExpenseReporter’s form is resized, it may be large enough to display the entire ScrollArea. In that case the program does not need the scroll bar. ExpenseReporter hides the scroll bar by setting its Visible property to false. It then sets ScrollArea.Top to zero so ScrollArea is positioned at the top of the Viewport.

The ResetScrollbar subroutine that follows performs all of these calculations for ExpenseReporter. It determines whether the scroll bar is needed and hides it if it is not. Otherwise, it calculates the correct values for the scroll bar’s Min, Max, SmallChange, and LargeChange properties.

Sub ResetScrollbar()
Dim space_needed As Integer
Dim space_available As Integer

    ‘ See how much room the loaded rows need.
    space_needed = ScrollArea.Height
                  
    ‘ See how much space is available.
    space_available = Viewport.ScaleHeight
   
    ‘ If there is enough room without the scroll
    ‘ bar, do nothing. The scroll bar should
    ‘ already be hidden.
    If space_needed <= space_available Then
        If ScrollBar.Visible Then
            ScrollBar.Value = 0
            ScrollBar.Visible = False
        End If
        Exit Sub
    End If

    ‘ Reset the scroll bar values.
    ‘
    ‘ Min and Max are set so the top of the
    ‘ scrolling area should be at -Value.
    ScrollBar.Min = 0
    ScrollBar.Max = space_needed - space_available
        ‘ SmallChange scrolls by one row.
    ScrollBar.SmallChange = DateText(0).Height + GAP
   
    ‘ LargeChange scrolls by the viewport size.
    ScrollBar.LargeChange = space_available
   
    ‘ Show the scrollbar.
    If Not ScrollBar.Visible Then _
        ScrollBar.Visible = True
End Sub

Arranging Controls

Whenever the user resizes the expense report form, ExpenseReporter rearranges its controls to take full advantage of the form’s new size. If the form becomes taller, it can display more expense rows. If the form becomes smaller, it can display fewer. The Form’s Resize event handler performs the rearrangement.

Before going to all the bother of rearranging the form’s controls, the Resize event handler checks the form’s WindowState property. If the user has just minimized the form so that it appears as an icon in the taskbar, there is no point in rearranging the controls. Because none of the controls are visible anyway, there is no need to move them around.

The event handler also determines whether the form’s height has changed since the last time the controls were rearranged. If the user has just changed the form’s width, there is no need to rearrange the controls for this application. Also, if the user has just restored the form after minimizing it, the form’s height is the same as it was when it was minimized. The form’s height has not changed since the last time the controls were arranged, so they are still in their correct positions.

As the event handler positions the controls, it must be careful not to make any controls too small. In this case Viewport is resized to fill all of the vertical space available after subtracting room for the other controls. If the form is too small, this subtraction will yield a negative number. The application will crash if it tries to give Viewport a negative height. Also, if the application makes controls too small, they will look strange and be difficult or impossible for the user to understand. To prevent these problems, applications should always give controls no less than some reasonable minimum size.

The following code shows the Resize event handler for ExpenseReporter’s main form. It ensures that the Viewport is always large enough to display at least one row of expenses. This approach does not allow the user to view much information at one time, but it is better than displaying a strange little scrolling area only a few pixels tall. And it is much better than allowing the application to crash.

Private Sub Form_Resize()
Static old_height As Single

Dim viewport_hgt As Single
Dim min_hgt As Single
Dim frame_offset As Single
Dim pos As Single
    ‘ If the form is being minimized, don’t bother.
    If WindowState = vbMinimized Then Exit Sub
   
    ‘ If the form hasn’t changed height since the
    ‘ last rearrangement, the controls are in the
    ‘ right places.
    If Height = old_height Then Exit Sub
    old_height = Height

    ‘ See how much space is available for the rows.
    viewport_hgt = ScaleHeight - Viewport.Top - _
        NotesText.Height - NotesLabel.Height - _
        2 * GAP
   
    ‘ Don’t let it become too small.
    min_hgt = DateText(0).Height + 2 * GAP
    If viewport_hgt < min_hgt Then _
        viewport_hgt = min_hgt
   
    ‘ Set the viewport and scrollbar heights.
    Viewport.Height = viewport_hgt
    ScrollBar.Height = viewport_hgt

    ‘ Position the controls that lie below.
    frame_offset = ReconFrame.Top - NotesText.Top
    pos = Viewport.Top + Viewport.Height + GAP
    NotesLabel.Top = pos
    pos = pos + NotesLabel.Height + GAP
    NotesText.Top = pos
    ReconFrame.Top = pos + frame_offset

    ‘ Reset the scrollbar parameters.
    ResetScrollbar
End Sub


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.