《vbpjtip4 面向VisualBasic程序员的杂志 第4版.docx》由会员分享,可在线阅读,更多相关《vbpjtip4 面向VisualBasic程序员的杂志 第4版.docx(82页珍藏版)》请在优知文库上搜索。
1、WelcometotheFourthEditionoftheVBPJTechnicalTipsSupplement!ThesetipsandtricksweresubmittedbyprofessionaldevelopersusingVisualBasic3.0,VisualBasic4.0,VisualBasicforApplications,andVisualBasicScript.ThetipswerecompiledbytheeditorsatVisualBasicProgrammer,sJournal.SpecialthankstoVBPJTechnicalReviewBoardm
2、embersDougHaynes,KarlE.Peterson,andPhilWeberfortestingallthecode.Ifyou,dliketosubmitatiptoVisualBasicProgrammer,sJournal,pleasesendittoUserTips,FawcetteTechnicalPublications,209HamiltonAvenue,PaloAlto,California,USA,94301-2500.Youcanalsofaxitto415-853-0230orsenditelectronicallytovbpjedil.Pleaseinclu
3、deaclearexplanationofwhatthetechniquedoesandwhyitisuseful,indicateifit,sforVBA,VBS,VB3,orVB4,16-or32-bitversion.Pleasetrytolimitlimitcodelengthto20lines.Do11,tforgettoincludeyoure-mailandmailingaddresses.We,11payyou$25ifwepublishyourtip.VB3,VB416/321.evel:BeginningEasyCR/1.FPaddingWhenIwritetextinme
4、ssageboxes,labels,andsoon,and1needtoincludeacarriagereturn/1inefeed,Iusethisfunction,passingitthenumberofCR/1.FsIneed.ThissavesalotoftypingandlookingupofASCIIvalues:SubcmdDelete_Click()msg=Areyousureyouwant_,todeletethisitem?”&N1.(2)&Press0K”rc=MsgBox(msg,4+32+256,ConfirmDelete*)endsubFunctionN1.(nu
5、m_linesAsInteger)AsStringThisfunctionreturnsaNew1.inecharacterforthenumberoftimespassedtothefunction.*DimmsgsStringDimiAsIntegerFori=1Tonumlinesmsg=msg&Chr(13)&Chr(IO)NextN1.-msgEndFunction-BretCutler,1.ayton,UtahVB3,VB416/321.evel:BeginningAutoSelectTextBoxContentsUsersoftenfinditfastertoretypethee
6、ntirecontentsofatextboxratherthanpositionthecursorwithinthedata,andtheneditthedata.Thisisespeciallytrueifthelengthofthedataisshortorifthedataisn,tvisible,aswithapasswordfield,forexample.Doubleclickingorusingamousetoselectthecontroscontentsisslowandinconvenient.Icreatedthissmallroutinetoautomatically
7、selectalldatawithinacontrol.Iplacethisroutineinacodemodulesothatit,saccessiblefromallforms.Icallthisroutinefromacontrol*sGotFocusevent.Thisway,thedataisselectediftheusertabstoorclicksonthecontrol,orifadatavalidationroutinedoesaSetFocus:PrivateSubMyTextBox_GotPocus()AutoSelectMyTextBoxEndSubTheAutoSe
8、lectroutineisquitesimple:SubAutoSelect(SelObjectAsControl)TheAutoSelectroutine,selectsthecontrosentirecontentsasifitweredoubled-clicked.SelObject.SelStart=OIfTypeOfSelObjectIsMaskEdBoxThenSelObject.Sel1.ength=1.en(SelObject.FormattedText)ElseIfTypeOfSelObjectIsTextBoxThenSelObject.Sel1.ength=1.en(Se
9、lObject.Text)EndIfEndIfEndSub-KevinForth,St.ClairShores,MichiganVB416/321.evel:BeginningCleartheClutterProvideyouruserswithaquickwaytocleartheirmessydesktopofextraneousformsbydroppingthiscodeintotheClickeventofacommandbutton:ForEachFormInFormsIfForm.NameMe.NameThenUnloadFormEndIfNextForm-JamesBell,C
10、harlotte,NorthCarolinaVB3,VB416/321.evel:BeginningCalculateAgeUsingDateDiffUsethefunctionDateDifftocalculateanindividuasexactagebasedonbirthdate.DateDifffirstcalculatesthetotalnumberofdaysanindividualhasbeenaliveandthendividesby365.25toaccountforleapyears.TheIntfunctiontruncatesthedivisionresultsbyr
11、emovingthedecimalandnotrounding:FunctionCalcAge(datEmpDateOfBirthasVariant)asIntegerCalcAge=Int(DateDiff(*y*,datEmpDateOfBirth,Date()_/365.25)EndFunction-MichaelFinley,ClarendonHiIls,IllinoisVB416/321.evel:IntermediatePreventUnwantedRecursionAbugaffectingtheSheridan3DControlsappearsinVB4.0a.Ido11,tb
12、elieveitwasin4.0,andIamcertainitwasnotin3.0.Inanycase,evenifyouuse4.0,youshouldpayattentionbecausethenewerOCXmaybeinstalledalreadyonusers*machineswhenyourappsaredistributed.IusetheSheridancommandbuttonsquiteabitfortheirabilitytodisplayaniconaswellastext,andIranintothisproblemwhenIinstalledVB4.0a.Ift
13、heuserdouble-clicksonacommondialogbox(forexample,toselectafile),andthedoubleclickisphysicallylocatedaboveaSheridan3Dcommandbutton,theClickprocedureofthatcommandbuttonwillbefired.Totestthis,startanewprojectandplaceaSheridancommandbuttonandacommondialogcontrolonForml.IntheClickprocedureofthecommandbut
14、ton,includethiscode:PrivateSubSSCommand1_C1ick()ConstCDERR_CANCE1.;&H7FF3ConimonDialogl.DialogTitle=OpenFile”CommonDia1og1.fiIename=*.*ConimonDialogl.DefaultExt=ConimonDialogl.CancelError=TrueCommonDialogl.Filter=,A11Files(*.*)*.*OnErrorResumeNextConimonDialogl.Action=1IfErr=CDERR_CANCE1.ThenExitSub
15、EndIfOnErrorGoToOEndSubRuntheprogram,clickonthecommandbutton,andwhenthecommondialogappears,moveitsothatthenameofanyfileappearsoverthebutton.Double-clickonthefilename.Thecommondialogwilldisappearandanewonewillappear,resultingfromtheSSCommandlClickeventfiringagain.Thesolutionistodeclareastaticflagvariable,FalseClick,withinthebutton*sClickevent.ThenchangethecodeintheSSComniandlClickproceduretoread:PrivateSubSSCOmInand1.CliCkoConstCDERR.CANCE1.=&II7FF3StaticFalseClickAsBooleanIfFalseClickThenExitSubFalseClick=TrueCommonDialogl.DialogTitle=OpenFile”ConimonDialogl.filename=C