Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


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

Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Reading Data

You read data when an OnComm event occurs. In the OnComm event handler, you use the CommEvent property to determine what happened. For example, when CommEvent is equal to comEvReceive , we’ve received data and can use the Input property. Here we fill a buffer with data that the communications control has received:

Private Static Sub MSComm1_OnComm()

    Select Case MSComm1.CommEvent
        Case comEvReceive
            Dim Buffer As Variant
            Buffer = MSComm1.Input
...

Setting the InputLen property to some value means you’ll get that number of bytes when you use the Input property (if those bytes are available). Setting InputLen to 0 makes the communications control read the entire contents of the receive buffer when you use Input . The EOFEnable property is used to indicate when an End Of File (EOF) character is found in the data input. If you set this property to True, it makes data input stop (and the OnComm event fire) when the EOF is encountered.

Finally, as each byte of data is received, the InBufferCount property is incremented by 1 (you use the InBufferCount property to get the number of bytes in the receive buffer). You can also clear the receive buffer by setting the value of this property to 0. You can monitor the number of bytes in the transmit buffer by using the OutBufferCount property. You can clear the transmit buffer by setting this value to 0.

We’ll see all about the communications control, such as how to support handshaking and how to hang up, in this chapter.

The MonthView And DateTimePicker Controls

We’ll also cover two more controls in this chapter—the MonthView and DateTimePicker controls (see Figure 13.3). These controls really exist just to make life a little easier for the user, and they’re not all that complex.


Figure 13.3  The MonthView Control and the DateTimePicker Control tools.

In particular, the MonthView control displays a calendar of the current month and lets the user scroll to other months as well. The user can select a date—or range of sequential dates—in MonthView controls.

The DateTimePicker control allows the user to specify a date or time, as its name implies. DateTimePickers can display a MonthView as a drop-down control, or the current time together with an updown control to let users select the time they want. The control’s UpDown property determines which mode the control is in. When UpDown is False, the control is in drop-down calendar mode (that’s the default). When the UpDown property is True, the DateTimePicker is in time format mode.

To add these controls to your program, select the Project|Components menu item, click the Controls tab in the Components dialog box that opens, select the Microsoft Windows Common Controls-2 entry, and click OK to close the Components dialog box. This adds both these controls to the toolbox, as shown in Figure 13.3. The MonthView Control tool is the thirteenth tool down on the left in Figure 13.3, and the DateTimePicker Control tool is the tool just to the right of the MonthView Control tool.

That’s it for our overview—it’s time to turn to the Immediate Solutions.

Immediate Solutions

Adding A Timer Control To A Program

The Testing Department is calling again. The users of your new program, SuperDuperDataCrunch , turn out to be real clock-watchers. In fact, they’d like your program to display a clock so they don’t get neck strain by looking up at the wall every now and then. Can you add a clock to your program?

You can, using the timer control. You add a timer control to your program just as you would any other intrinsic control—you just click the Timer Control tool and draw the timer on your form. The timer control is invisible when the program runs, so the size and location of the control don’t matter too much.

Now that you’ve added a timer, how do you get it running? See the next topic.

Initializing A Timer Control

Now that you’ve installed a timer control in your program—how do you get it started? You use these two properties:

  Enabled determines whether or not the timer creates Timer events.
  Interval sets the number of milliseconds between Timer events.

When you place a timer in your program, you can set its Enabled property to False, which means no Timer events will occur. When you want to start the timer, you can set Enabled to True.


TIP:  Note that a timer’s Enabled property is different from other controls’ Enabled properties; the timer’s Enabled property makes Timer events occur or not, whereas other Enabled properties make controls accessible or inaccessible to the user.

The Interval property sets the interval between Timer events. Although measured in milliseconds (1/1000s of a second), Timer events cannot actually occur faster that 18.2 times a second. The interval can be between 0 (in which case nothing happens) and 64,767, which means that even the longest interval can’t be much longer than 1 minute (about 64.8 seconds); however, you can design your code to wait for several intervals to pass before doing anything.


WARNING!  If the system is busy, your application may not get Timer events as often as the Interval property specifies. That is, the interval is not guaranteed to elapse exactly on time. To be more sure of accuracy, the Timer event handler should check the system clock when needed.

Now that you’ve set up your timer as you want it, how do you use Timer events? See the next topic in this chapter for the details.

Handling Timer Events

Well, you’ve set your timer’s Interval property and set its Enabled property to True. Presumably, your timer is doing something—but what?

The main event for timers is the Timer event, and double-clicking a timer at design time creates a handler function for that event:

Sub Timer1_Timer()

End Sub

All you need to do is to add the code you want executed to this procedure. For example, here we display the current time in a label named Display using the Visual Basic Time$ function:

Sub Timer1_Timer()
    Display.Caption = Time$
End Sub

This code will be called as often as the timer’s Interval property specifies (although note that Timer events are not guaranteed to occur—many other types of programs temporarily suspend the timer interrupt on occasion).


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-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.