《vbpjtip5 面向VisualBasic程序员的杂志 第5版.docx》由会员分享,可在线阅读,更多相关《vbpjtip5 面向VisualBasic程序员的杂志 第5版.docx(69页珍藏版)》请在优知文库上搜索。
1、WelcometotheFifthEditionoftheVBPJTechnicaltipsSupplement!ThesetipsandtricksweresubmittedbyprofessionaldevelopersusingVisualBasic3.0,VisualBasic4.0,VisualBasic5.0,VisualBasicforApplications,andVisualBasicScript.ThetipswerecompiledbytheeditorsatVisualBasicProgrammer,sJournal.SpecialthankstoVBPJTechnic
2、alReviewBoardmembersFrancescoBalenaandPhilWeberfortestingallthecode.IfyoudliketosubmitatiptoVisualBasicProgrammer,sJournal,pleasesendittoUsertips,FawcetteTechnicalPublications,209HamiltonAvenue,PaloAlto,California,USA,94301-2500.Youcanalsofaxitto415-853-0230orsenditelectronicallytoVbpjeditQor74774.3
3、050CompuS.Pleaseincludeaclearexplanationofwhatthetechniquedoesandwhyitisuseful,indicateifit,sforVBA,VBS,VB3zVB416-or32-bitzorVB5.Pleasetrytolimitcodelengthto20lines.Dontforgettoincludeyoure-mailandmailingaddress.We,11payyou$25ifwepublishyourtipVB3,VB416/321.evel:BeginningCOMBOBOXEVENTHAND1.INGTwopro
4、blemscanarisewhenaconfuseduserscrollsupordownwiththemouseandthenmakesaselectionwiththeEnterKey.First,thedownarrowfirestwoevents:ChangeandClick.Second,theEnterkeymovesfocustothenexttabstop,whilethemouseclickdoesn,tcausealossoffocusfromthecombobox.Therefore,ifyouplaceyouractioncodeintheChangeevent,anU
5、porDownarrowwillfireit,whichyoudon,twant.Onthecontrary,ifyouplaceyouractioncodeonlyinthe1.ostFocuseventandtheuserclicksonaselection,focuswon,tmovefromthecombobox,andtheuserisleftstaringattheselectedtextinthecomboboxandwonderingwhynoactionoccurred.ThissolutionfiltersoutClickeventsgeneratedwitharrowke
6、ys,andforcesthecontroltolosefocus.IntheDeclarationssectionoftheform,enterthiscode:Note:UseanIntegerflagvariablein,VB3DimbNoiseasBoolean,TruedenotesaNoiseEventwhichisto,beignoredEnterthiscodeintheForm_1.oadevent:bNoise=FalseEnterthiscodeinthecomboboxKeyDownevent:PrivateSubcbTest_KeyDown(KeyCodeAs_Int
7、eger,ShiftAsInteger),Iftheuserisbrowsingwiththe,arrows,ignoretheClickEventsIfKeyCode=VbKeyDownOrKeyCode_=VbKeyUpThenbNoise=TrueEndSubevent:EnterthiscodeinthecomboboxClickPrivateSubcbTest_Click()IfbNoiseThen,IgnoreNoiseevents,(upordownarrow)bNoise=FalseElseForcelossoffocusSendKeys”TAB”,TrueEndIfEndSu
8、bWritecodethatreactstoanewuserselectioninthecombobox1.ostFocusevent.Don,tsendaTabkeystrokebecausefocushasalreadyshifted,andthecombobox,sbehaviorisconsistentregardlessofhowtheuserselectsanewvalue.-1.esSmith,Concord,NorthCarolinaVB51.evel:BeginningCOMMENTANDUNCOMMENTB1.OCKSOFCODEVisualBasic5.0letsyouc
9、ommentablockofcodeinasnapanduncommentitlater.Thisfeatureisusefulinthedebugphase,whenyoudon,twanttoexecuteanumberofstatements,butyoudon,twanttophysicallydeletethemeither.However,theComment/Uncommentcommandpairisn,tpresentinanymenuoftheenvironment,andyoucanonlyreachitbyenablingtheEdittoolbar.Todothisq
10、uickly,right-clickonanytoolbarintheenvironmentandselecttheEditcommand.-FrancescoBalena,Bari,ItalyVB51.evel:BeginningDEFAU1.TVA1.UESFOROPTIONA1.PARAMETERSIfyoueverprogrammedunderVB4,youprobablytookadvantageofthepowerfulOptionalparameters.VB5enhancedtheminseveralways:theycanbeofanytypenow(notonlyVaria
11、nts),andtheycanappearinPropertyprocedures.Interestingly,youcannowstateadefaultvalueforthem:PropertyGetValue_(OptionalindexAs1.ong=1)EndPropertyYoucandothiswithoutanexplicit(andslower)IsMissingtest:PropertyGetValue_(OptionalindexAs1.ong)IfIsMissing(index)Thenindex=1EndProperty-FrancescoBalena,Bari,It
12、alyDON*TCREATEA1.IASEDVARIAB1.ESNeverpassaglobalvariableasanargumenttoaprocedurethatalsoaccessesthevariabledirectly.Ifyou,re100percentsureyouadheredtothisrulewithinyourapplication,checktheAssumeNoAliasingoptionintheAdvancedOptimizationsdialogthatyouinvokefromtheCompiletaboftheProjectPropertiesdialog
13、.Ifthenativecodecompilerknowsnoaliasedvariablesexist,itcanfreelycachevariablevaluesintofasterCPUregisters,andstorethembacktoRAMmemoryonlywhenleavingtheprocedure.Thisimprovestheexecutionspeedofcompiledprograms.-FrancescoBalena,Bari,ItalyVB51.evel:BeginningCENTERFORMSONSCREENApopularcodesnippetletsyou
14、centeranyformonthescreen,regardlessofthecurrentscreenresolution.YounowcanreachthesameresultbysimplyassigningthevalueVbStartUpScreen(=2)totheformsStartUpPositionnewproperty.YoucanevencenteraformwithinitsparentwindowbyassigningtheVbStartUpOwner(=1)value.YoucansetthispropertyfromthePropertywindow.Whena
15、formissupposedtobecenteredwithinitsparentwindow,remembertoaddasecondargumenttotheShowmethod:Form2.ShowVbModalzMe-FrancescoBalena,Bari,Italy0N-E16aDON,TAUTO-OPTIMIZEFORFASTCODEIfyoutakealookatVB,snativecodeoptimizationoptionsforthefirsttime,youmightbetemptedtoclick,onOptimizeforFastCoderightaway.Stra
16、ngeasitmaysound,though,thisdoesnotalwaysguaranteethebestperformance.Applicationsoptimizedforperformancegenerallydon,trunthatmuchfaster,butdohavealargermemoryfootprint.Thiscausesthemtoloadslower,especiallyonmemory-constrainedmachines,givingtheusertheimpressionthatyourappisactuallyslowerthanoneoptimizedforcompactcode.Forthesamereason,considerl