Microsoft KB Archive/141736

= How to Create a Clock Class to Show Time in AM/PM Format =

Article ID: 141736

Article Last Modified on 9/30/2003

-

APPLIES TO


 * Microsoft Visual FoxPro 3.0 Standard Edition
 * Microsoft Visual FoxPro 3.0b Standard Edition

-



This article was previously published under Q141736



SUMMARY
The TIME function in Visual FoxPro returns the time as a character string in the format of hh:mm:ss. With this format, 1:35:30 PM displays as 13:35:30 and 9:45:30 AM displays as 09:45:30. In certain situations, you may prefer the standard time display with the AM/PM format. This article shows you how to do it.



MORE INFORMATION
The following steps create a Visual FoxPro Class that displays the current time in the standard AM/PM format when you place it on a form.

Steps to Create Clock Class
  Type the following command in the Command window: CREATE CLASS.  In the dialog box, enter oClock in the Class Name field, Container in the Based On field, and a name for the .VCX file in the Store in field.  Add a Timer control to the container, and give the Timer control the following property values and event codes: Name: Clock Interval: 1000

Timer event method:

This.Parent.tdisplay.Refresh   Add a text box to the container, and give the text box the following property values and event code: Name: tdisplay DisabledBackColor: 255,255,255 DisabledForeColor: 0,0,0 Enabled: .F.  FontName: Arial FontSize: 10 Height: 24 Width: 85

Init event:

This.Top=0 This.Left=0

Refresh method:

This.Value=IIF(BETWEEN(VAL(LEFT(Time,2)),12,23), ;        ALLTRIM(STR(VAL(LEFT(Time,2))-12))+ SUBSTR(Time,3,6)+' PM', ;         IIF(BETWEEN(VAL(LEFT(Time,2)),1,11), ; ALLTRIM(STR(VAL(LEFT(Time,2))))+ SUBSTR(Time,3,6)+' AM', ; '12'+ SUBSTR(Time,3,6)+ ' AM'))   Add the following code to the Init event of Oclock: This.Width=This.tdisplay.Width This.Height=This.tdisplay.Height   Save the container class. Then type the following in the Command window: CREATE FORM seeclock  On the Form Controls toolbar, click the Visual Class tool.</li> On the Visual Class menu, click Add. Locate the .vcx file where you saved the oClock class. Select the oClock object from the Form Controls toolbar and drop it on the form.</li> Save and run the form. The system time will display on the form in the standard AM/PM format.</li></ol>

To add functionality to the clock to allows toggling between both the standard AM/PM and military (24 hour) formats, make the following modifications to the Class:

Steps to Add Format-Toggling Functionality
<ol> While the oClock container is open in design mode, click New Property on the Class menu. In the New Property dialog box, enter lttoggle as the Name, and click OK.</li>  In the Init event of oClock, add the following line of code: This.lttoggle=.t.                       </li>  In the Refresh method of tdisplay, the followings lines of code replace the lines of code for the Refresh method as shown in Step 4 in the previous section: IF This.Parent.lttoggle This.Value=IIF(BETWEEN(VAL(LEFT(Time,2)),12,23), ;        ALLTRIM(STR(VAL(LEFT(Time,2))-12))+ SUBSTR(Time,3,6)+' PM', ;         IIF(BETWEEN(VAL(LEFT(Time,2)),1,11), ; ALLTRIM(STR(VAL(LEFT(Time,2))))+ SUBSTR(Time,3,6)+' AM', ; '12'+ SUBSTR(Time,3,6)+ ' AM')) This.Width=85 This.Parent.Width=85 ELSE This.Value = Time This.Width=60 This.Parent.Width=60 ENDIF </li>  In the oClock DblClick event, enter the following two lines of code: This.lttoggle= !This.lttoggle This.tdisplay.Refresh </li> Save the container class.</li> Run the previously saved seeclock form. When the form initially loads, the time is displayed in standard AM/PM format. Double-click the oClock object, and note that the time now toggles to military format. Then double-click the Clock object to toggle the time display between the two formats.</li></ol>

Additional query words: VFoxWin normal switch

Keywords: kbcode KB141736

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.