|
|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Formatting Times And DatesWhen working with times and dates in Visual Basic, its valuable knowing how to display them as strings. For example, you can use the Visual Basic Time$ and Date$ functions to get the time and date in string form, suitable for display: Text1.Text = Time$ You can also use string comparisons here; for example, to check if the current time is past a time specified in string form, you can use code like this: If (Time$ > AlarmSetting.Text) Then ... End If Besides Time$ and Date$ , you can use Now . This function refers to the current time in a numeric way, and you can use comparisons this way: If (Now > AlarmTime) Then ... End If To display the current date and time using Now , you use the Format$ function. For example, this use of Format$ and Now : Format$(Now, "dddd, mmmm d, yyy") returns the string with the day of the week, the month, date, and year like this: Friday, January 1, 2000. The different format strings and what they do appear in Table 13.1and some examples appear in Table 13.2 to make all this clearer.
Creating A Clock ProgramCreating a clock in Visual Basic is easy with the timer control. To see how that works, just create a new project now and add a timer control, Timer1. Set the timers Interval property to 1000 (that is, a thousand milliseconds, or one second). Next, add a label that covers most of the form and give it a large font, like 48-point Courier New. Well display the time in that label each time the timer ticks, so add the Timer1_Tick() event handler now: Sub Timer1_Timer() End Sub All we have to do when theres a Timer event is to update the clock, and we use the Visual Basic Time$ function to do that:
Sub Timer1_Timer()
Display.Caption = Time$
End Sub
Thats all we need. Now the clock is functional, as shown in Figure 13.4.
The code for this example is located in the clock folder on this books accompanying CD-ROM. If you want to create more than a simple clockan alarm clock, for examplesee the following topics in this chapter. Creating A StopwatchThe Testing Department is calling. Users are concerned about the time your SuperDuperDataCrunch program takes in executioncan you add a stopwatch to the program to convince them its really pretty fast? You think, a stopwatch? Building a stopwatch is valuable to see how to work with elapsed time instead of simply system time. To build a stopwatch program, create a new Visual Basic project now and add two buttons, labeled Start and Stop, as well as a label control named Display (set the font in the label to something large, like 48-point Courier New). Also add a timer control, Timer1, and set its Enabled property to False so it doesnt do anything until the user clicks the Start button. Now when the user clicks the Start button, Command1 , we can store the current time using Now in a form-wide variable named StartTime (add StartTime to the (General) section of the form), and we can start the timer by setting its Enabled property to True:
Private Sub Command1_Click()
StartTime = Now
Timer1.Enabled = True
End Sub
When the user clicks the Stop button, Command2, we can stop the timer:
Private Sub Command2_Click()
Timer1.Enabled = False
End Sub
Finally, in the Timer event, we just display the time that has elapsed from the starting time, and that time is just the difference between the current value of Now and the StartTime variable:
Sub Timer1_Timer()
Display.Caption = Format$(Now - StartTime, "hh:mm:ss")
End Sub
|
|
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. |