|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
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 , weve 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 youll 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. Well see all about the communications control, such as how to support handshaking and how to hang up, in this chapter. The MonthView And DateTimePicker ControlsWell also cover two more controls in this chapterthe MonthView and DateTimePicker controls (see Figure 13.3). These controls really exist just to make life a little easier for the user, and theyre not all that complex.
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 dateor range of sequential datesin 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 controls UpDown property determines which mode the control is in. When UpDown is False, the control is in drop-down calendar mode (thats 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. Thats it for our overviewits time to turn to the Immediate Solutions. Immediate SolutionsAdding A Timer Control To A ProgramThe Testing Department is calling again. The users of your new program, SuperDuperDataCrunch , turn out to be real clock-watchers. In fact, theyd like your program to display a clock so they dont 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 controlyou 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 dont matter too much. Now that youve added a timer, how do you get it running? See the next topic. Initializing A Timer ControlNow that youve installed a timer control in your programhow do you get it started? You use these two properties:
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.
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 cant 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.
Now that youve 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 EventsWell, youve set your timers Interval property and set its Enabled property to True. Presumably, your timer is doing somethingbut 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 timers Interval property specifies (although note that Timer events are not guaranteed to occurmany other types of programs temporarily suspend the timer interrupt on occasion).
|
|
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. |