Tải bản đầy đủ - 0 (trang)
Hack 25. Put a Clock on a Form

Hack 25. Put a Clock on a Form

Tải bản đầy đủ - 0trang

ThisassumesthelabelcontrolisnamedlblClock.TheNow

functionreturnsthesystemtime,andtheFormatfunctiongives

thetimeadesirablelook.Theformatisn'tnecessary,though.If

youdon'tuseit,thefulldateandtimeisreturned.Theformat,

asappliedhere,displaysjustthetime;hh:mm:ssisaformatfor

hours(hh),minutes(mm),andseconds(ss).



3.8.2.HackingtheHack

Youcandoalottoboosttheclock'sappealandfunctionality.

Oneideaistodisplaythetimeforcitiesindifferenttimezones.

Figure3-23showsaformwithtwoclocks.Onedisplaysthe

timeinNewYork,andtheotherdisplaysthetimeinChicago.



Figure3-23.Twoclocksonaform



ChicagoisonehourbehindNewYork.YouaccountfortheonehourdifferencebyapplyingtheDateAddfunction.Hereisthe

updatedOnTimerevent:





Me.lblClockNewYork.Caption=Format(Now(),"hh:mm:ssA

Me.lblClockChicago.Caption=Format(DateAdd("h",-1,Now()





"hh:mm:ssAMPM")



DateAddcanaddorsubtracttime.Inthiscase,avalueof-1



subtractsonehour.

Here'sanotheridea:givetheuserawaytochangetheformat.

Youaccomplishthisbyusingapublicvariableandthelabel

control'sDblClickevent.Whentheformisopened,apublic

variable,namedformat_typeinthisexample,isgivenavalueof

1.Eachtimeauserdouble-clickstheclock,theformat_type

variableincrements.Whenithits4,itissetbackto1.TheOn

Timereventteststheformat_typevariableandappliesthe

particularformat.Hereisthefullcodebehindtheformthat

takescareofthis:





OptionCompareDatabase

Publicformat_typeAsString



PrivateSubForm_Open(CancelAsInteger)

format_type=1

EndSub



PrivateSubForm_Timer()

SelectCaseformat_type

Case1

Me.lblClock.Caption=Format(Now(),"hh:mm:ssAMPM")

Case2

Me.lblClock.Caption=Format(Now(),"hh:mmAMPM")

CaseElse

Me.lblClock.Caption=Format(Now(),"mm/ddhh:mmAMPM

EndSelect

EndSub

PrivateSublblClock_DblClick(CancelAsInteger)

format_type=format_type+1

Ifformat_type=4Thenformat_type=1

EndSub



Now,ausercandouble-clicktheclockuntilitdisplaysthedate

andtimeinawaythatsuitshim.Ofcourse,youcaneasily

increasethenumberofavailableformats.



Hack26.BeTab-Smart



Overrideaform'stabordersothatusersgettotheentry

boxestheyneed.

Settingthetaborderisprobablynotthebiggestthingonyour

mindwhenyoudesignaform.Itdoesn'treallyrequiremuch

planningoranalysis.

Typically,youdoitattheend,afterallthecontrolsareinplace.

Tobehonest,attimesI'vesimplyforgottentosetataborder

onmyforms.But,ofcourse,theusercommunityletsmeknow

whenthathappens.

So,Istartedthinkingthatiftabstopsonaformarereally

importanttousers,whynotgoonestepbetterandmakethe

taborderworkmoreintelligently.Onsomeformsonlysome

boxesreceiveentries.Asmartwaytotabthroughaforminthis

situationistosensewhichentryboxescanbeskipped.

Thismakessensewhentheconditionorentryintoatextboxis

basedonsomeunderstandablelogicflow.Forexample,ifyou're

enteringinformationaboutanewcustomer,usuallyyoufillin

everyfield.However,whenyou'reenteringapieceof

informationonanexistingcustomer,mostlikelyyoutabover

severaltextboxes.

Figure3-24showsaforminwhichaselectionhasbeenmade

fromacombobox.Thecombobox,namedStatus,hasfour

choices:Enrolled,Walk-In,Phone,andRenewal.

IntheExiteventofthecombobox,aSelectCasestatementsets

thefocustodifferentformcontrols,dependingontheselection

madeinthecombobox.Forexample,astatusofWalk-Inis



usedforunregisteredpeoplewhocomeinwithoutan

appointment.Theyneedtobesetupinthesystem,sothenext

entryboxtogotoistheReferralSource.Bycontrast,whenan

Enrolledpersoncomesin,thenextboxtoenterisComments.

HereisthecodeintheExitevent:

PrivateSubstatus_Exit(CancelAsInteger)

SelectCasestatus

Case"Enrolled"

Me.txtComments.SetFocus

Case"Walk-In"

Me.txtReferralSource.SetFocus

Case"Renewal"

Me.txtStudentFirstName.SetFocus

EndSelect

EndSub



Thegistofthisapproachisthatyoucanapplyrulestothetab

orderbasedonwhatmakessensetothebusiness.Asanother

example,adatabasesystematacardealershipshouldskip

rightoverasectionaboutwhattypeofnewcaracustomer

wantstopurchaseifsheisinthemarketforausedcar.Ifthe

sectionaboutnewcarscontains12fields,thesalesperson

needstotabthrough12unneededboxes,unlessabetter

methodisinplace.Instead,someothercontrolontheformcan

actasaflagsothatyoucanimplementsmarttabbing.Using

theExiteventsofcontrolsisagreatwaytosupportthis

businessworkflow.



Figure3-24.Acomboboxselectiontodrivethe

taborder



Hack27.HighlighttheActiveControl



Provideavisualaidonabusyformbyemphasizingthe

controlthathasthefocus.

Asyoutabthroughaformandenterdata,itispossibletoget

lost.Themorecontrolsonaform,themorelikelythisisto

occur.Thishackshowsyouhowtokeepthecurrententrybox

highlighted.

"BeTab-Smart"[Hack#26]showsawayinwhichuserscantab

aroundtheforminasensiblemanner.Thathackworkswitha

littlecodeputintoacontrol'sExitevent.ThishackusestheExit

event,aswellastheEnterevent.Inthishack,allcontrolsthat

acceptentryhavecodeinbothevents.

Figure3-25showstheentryformwithaborderaroundtheFirst

Nameentrybox.Asausertabsthroughtheformorclicks

specificcontrols,theborderwillalwaysappeararoundthe

activecontrol.Theborderisdisplayedononlyonecontrolata

time.



Figure3-25.Aborderappearingaroundthe

activecontrol



Togetthistowork,thecontrolsontheformmusthavetheir

SpecialEffectpropertysettoFlat.Youcanuseothersettings

thatallowborderstobedisplayed,suchasShadow,butFlat

seemstoworkbest.WhenShadowisused,andwhenthecontrol

losestheborder,thecontrolstillretainstheshadowpartofthe

border,andthatdoesn'tlookright.

Figure3-26showsthecodemodulebehindtheform.Two

subroutinesareatthetop:active_control_enterand

active_control_exit.Thesesubroutinesrunwhenthey'recalledby

eachcontrol'sEnterandExitevents.Theactive_control_enter

subroutinesetstheactivecontrol,whateveritisatthetime,to

haveathick,redborder.Theborderstylevalueof1indicatesa

solidborder.Theborderwidthvalueof2indicatesathick



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Hack 25. Put a Clock on a Form

Tải bản đầy đủ ngay(0 tr)

×