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


In AppointmentBook only the text string and the location of the text change each time VerticalText is called. The other parameters could be coded directly into the VerticalText subroutine. Making these values parameters makes the routine much more flexible, however, so it may be useful in other programs.

Sub VerticalText(txt As String, X As Single, ymin As Single, _
    ymax As Single, hgt As Long, wgt As Long, use_italic As Long, _
    use_underline As Long, use_strikeout As Long, font_name As String)
Dim newfont As Long
Dim oldfont As Long
Dim Y As Single

    ‘ Create the sideways font.
    newfont = CreateFont(hgt, 0, 900, 0, wgt, _
        use_italic, use_underline, use_strikeout, _
        DEFAULT_CHARSET, OUT_TT_ONLY_PRECIS, _
        CLIP_LH_ANGLES Or CLIP_DEFAULT_PRECIS, _
        PROOF_QUALITY, TRUETYPE_FONTTYPE, _
        font_name)

    ‘ Select the new font.
    oldfont = SelectObject(ThePictureBox.hDC, newfont)

    ‘ Display the text.
    ThePictureBox.CurrentX = X
    Y = (ymin + ymax - ThePictureBox.TextWidth(txt)) / 2

    ‘ Print only if the text will fit.
    If Y >= ymin Then
        ThePictureBox.CurrentY = _
            (ymin + ymax + ThePictureBox.TextWidth(txt)) / 2
        ThePictureBox.Print txt
    End If

    ‘ Restore the original font.
    newfont = SelectObject(ThePictureBox.hDC, oldfont)

    ‘ Delete the new font to free resources.
    If DeleteObject(newfont) = 0 Then
        Beep
        MsgBox “Error deleting font object.”, vbExclamation
    End If
End Sub

Scrolling Large Amounts of Data

AppointmentBook’s daily appointment area displays a list of the selected date’s appointments. The amount of data this scrolling area must display is relatively limited. When the application is maximized, this area can show about 8 of the 24 possible hours’ appointments.

Because it is fairly easy to draw the complete schedule, and because a large portion of the schedule is visible, the program can use a relatively simple method for scrolling the appointment area. The program draws the entire schedule and then uses picture boxes to adjust the portion of the schedule that is visible. This is similar to the method used by the ExpenseReporter application described in Chapters 1 and 2.

In contrast, AppointmentBook’s month area takes longer to draw and shows a much smaller portion of its total data. Using the same strategy for scrolling this area would be quite slow. As the user dragged the scroll bar’s slider, the frequent slow updates would be particularly annoying. In cases like this, where the program must display a large amount of data and updating the display takes a long time, the program can use a different scrolling strategy to improve performance.

The program should not draw all of the data into a picture box at once and then adjust the picture box’s Top property. Instead, it should draw only the data it needs to display as it is required. The scroll bar, rather than indicating how to adjust the Top property of a picture box, indicates the parts of the data that need to be displayed.

Updating the display will still be slow, however, if the program redraws the data whenever the scroll bar receives a Scroll event. As the user drags the scroll bar’s slider, Visual Basic generates many Scroll events. In order to respond to each event, the program would need to redraw the data constantly. If redrawing the data takes a long time, as it does in AppointmentBook, this will be slow and will make the display seem to jump awkwardly.

To prevent this problem, the program should not update its data display during Scroll events. When the user finishes scrolling and releases the scroll bar’s slider, the scroll bar will receive a final Change event and the program can update the display then.

While the program should not redraw during the Scroll event, it is important to provide the user with some type of feedback. When it receives a Scroll event, AppointmentBook calculates the date that would be selected if the user were to finish scrolling at that point. It then displays the new date in the date label above the daily appointment area. This makes it much easier for the user to select a particular date using the scroll bar.

This strategy is similar to one used by MicrosoftWord. In a long document, if the user grabs the scroll bar slider and drags it, Word does not immediately change the display. Instead the status bar shows the number of the page that would be selected if the slider were released at that point. Only when the user releases the slider does Word take the time to display the new page.

Setting Alarms

Visual Basic’s timer control executes periodic commands. The control’s Interval property determines the number of milliseconds that pass between calls to the control’s Timer event. For instance, if the Interval property is 1000, the control receives a Timer event approximately every 1000 milliseconds or every 1 second.

Timer controls work well for scheduling periodic events that occur fairly frequently, but the Interval property can have values up to only 65,535. That means the longest interval a program can schedule to elapse between Timer events is roughly 65 seconds.

AppointmentBook needs to arrange notification for events far in the future. For example, suppose it is currently 10:00 AM, the program’s next scheduled appointment starts at 3:30 PM, and AppointmentBook needs to give a 30-minute warning before the appointment. In that case, the program needs to warn the user of the appointment at 3:00 PM—five hours in the future. Unfortunately, the program cannot set a timer control’s Interval property to five hours.

The solution to this problem is fairly simple. First, the program stores the time at which it needs to take action. In this example, the program needs to warn the user of the next appointment at 3:00 PM. Next, the program sets a timer control’s Interval property to a relatively large value, for example, 60,000. This will make the Timer event handler trigger once every minute. When it triggers, the event handler checks to see if the desired time has arrived. If so, it disables the timer and takes whatever action is necessary. Otherwise, it does nothing. The Timer event will continue to occur every minute until it is time for action.

The following source code shows how AppointmentBook implements a simple, self-contained alarm system. ScheduleWakeup is a public subroutine defined in the main form CalendarForm. This form contains the timer control AlarmTimer used by the alarm system.


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.