techtips8 面向VisualBasic程序员的杂志 第8版.docx

上传人:李司机 文档编号:7145349 上传时间:2024-06-15 格式:DOCX 页数:53 大小:141.43KB
返回 下载 相关 举报
techtips8 面向VisualBasic程序员的杂志 第8版.docx_第1页
第1页 / 共53页
techtips8 面向VisualBasic程序员的杂志 第8版.docx_第2页
第2页 / 共53页
techtips8 面向VisualBasic程序员的杂志 第8版.docx_第3页
第3页 / 共53页
techtips8 面向VisualBasic程序员的杂志 第8版.docx_第4页
第4页 / 共53页
techtips8 面向VisualBasic程序员的杂志 第8版.docx_第5页
第5页 / 共53页
点击查看更多>>
资源描述

《techtips8 面向VisualBasic程序员的杂志 第8版.docx》由会员分享,可在线阅读,更多相关《techtips8 面向VisualBasic程序员的杂志 第8版.docx(53页珍藏版)》请在三一办公上搜索。

1、WelcometotheEighthEditionoftheVBPJTechnicalTipsSupplement!ThesetipsandtricksweresubmittedbyprofessionaldevelopersusingVisualBasic3.0,VisualBasic4.0,VisualBasic5.0,VisualBasic6.0,VisualBasicforApplications(VBA),andVisualBasicScript(VBS).TheeditorsatVisualBasicProgrammer,sJournalcompiledthetips.Instea

2、doftypingthecodepublishedhere,downloadthetipsfromthefree,Registered1.evelofTheDevelopmentExchangeat.Ifyou,dliketosubmitatiptoVisualBasicProgrammer,sJournal,pleasesendittoUserTips,FawcetteTechnicalPublications,209HamiltonAve.,PaloAlto,California,USA,94301-2500.Youcanalsofaxitto650-853-0230orsenditele

3、ctronicallytovbpjedit.Pleaseincludeaclearexplanationofwhatthetechniquedoesandwhyit,suseful,andindicateifit,sforVBA,VBS,VB3,VB416-or32-bit,VB5,orVB6.Pleaselimitcodelengthto20lines.Don,tforgettoincludeyoure-mailandmailingaddress.Ifwepublishyourtip,we,llpayyou$25orextendyourVBPJsubscriptionbyoneyear.VB

4、5,VB61.evel:BeginningPreventCheckboxChangesYou,11oftenwanttodisplayacheckbox-stylelistboxtoshowusersthevaluestheyhaveselectedinanunderlyingdatabase.However,youdon,twanttoallowuserstochangetheselections-thatis,tochangewhichboxestheychecked.Youcan,tdisablethelistboxbecausethatstopstheuserfromscrolling

5、thelisttoseewhichitemstheychecked.Youcan,tuse1.ocked,becausethelistboxdoesn,thavea1.ockedproperty.Here,sonesolution:PaintaCommandbuttonwiththecaptionttClicktotoggleenabledproperty,andalistboxonaform,thenchangethelistboxstyleto1-Checkbox.Addthiscode:OptionExplicitDimmbDisabledAsBooleanPrivateSubComma

6、ndl_Click()mbDisabled=NotmbDisabledEndSubPrivateSub1.istl_ltemCheck(ltemAsInteger)IfmbDisabIedThen1.istl-SeIected(Item)=Not1.istl.SeIected(Item)EndIfEndSubWhenmbDisabledissettoTrue,thechangesmadebytheusertothelistboxselectionsareimmediatelyreversed.Itwillappearasiftheselectionshaven,tchangedatall,an

7、dthelistisstillscrollable.IanChamp,receivedbye-mail牟*余字*余*拿木*今*车窄*专*车*车*车窄*余*余*车本*余申*余*拿*拿*余*东*卒*车*余*京字*车*车*余辛*率*多*拿*VB432,VB5,VB61.evel:IntermediateEstablishaDataDictionaryIfyourSQ1.lookslikethis,youneedtoaskyourselfhowmuchcodeyou,dhavetoinspectandreviseifyoudecidedtochangeadatabasefieldortablename

8、,asfrequentlyhappensduringdevelopment:SQ1.String=SE1.ECTfirstname,lastname,&_linepreferences&_FROMimagingusersWHEREusercode=_,&Trim(UCase(UserlDText.Text)&,11ODBCstatus=SQ1.ExecDirect(ODBChandlel,SQ1.String,_1.en(SQ1.String)WhathappensifSQmandconventions(fieldnamedelimiters)change?Becauseacompiledoe

9、sn,trevealsuchnamemisspellingsorconventionflaws,codeinobscureprocedurescanbeinproductionbeforedefectsaredetected.Ourgroupestablishedatableandfielddictionaryinamoduleusedforarecentlargeproject.ThishelpedusensurethatwecorrectlypastedtableandfieldnamesintoallSQmands.Italsoprovidedarepositorythatsimplif

10、iedmaintenance.Asdatabaseresourcenameschangedornewname-delimitingconventionswererequired,werevisedthedictionarybeforerecompiling.Wealsousedthedictionarytoconveydescriptiveinformationabouttablesandfieldstodevelopers.Ourdictionarylookslikethis:tables:PublicConsttblUsersAsString=(imagingusersdatafields

11、:PublicConstfldFirstNameAsString=(firstname16charactersPublicConstfld1.astNameAsString=(lastname16charactersPublicConst(Id1.inePreferencesAsString=_linepreferences20charactersPublicConstfldUserCodeAsString=usercodeIOcharactersOurSQ1.lookslikethis:SQ1.String=SE1.ECT&fldFirstName&_,&fld1.astName&_,&fl

12、d1.inePreferences&_FROM,&tblUsers&_,WHERE&fldUserCode&=&_&Trim(UCase(UserlDTet.Text)&,ODBCstatus=SQ1.ExecDirect(ODBChandlel,SQ1.String,_1.en(SQ1.String)Programmersdonthavetoknowtheactualnamesofdatabasecomponents.Theyalwaysusetheconstantsthatrefertothedatabasecomponents.Acleancompileensuresyou,11usec

13、orrectnamesandname-delimitingconventionsinyourSQ1.statements.DougIIagy,Greensburg,Pennsylvania余*牟申*率*率*牟*辛*牟*拿*本*车申*率*牟*车*拿*拿*率*多*余*牟*牟*辛*专*牟*柴*VB432,VB5,VB61.evel:IntermediateContext-SensitiveHelpforDisabledControlsIfyouwantaformtosupportcontext-sensitivehelp,settheWhatsThisButtonandWhatsThisHeIppr

14、opertiesontheformtoTrue,andsettheWhatsThisHelpIDpropertytoacorrespondinghelp-filetopicIDforanycontrolonthatformforwhichyouwanthelptobedisplayed.Unfortunately,thehelpisn,tshownifthecontrosEnabledpropertyissettoFalse.Tosolvethisproblem,createalabelunderthecontrolwiththesamedimensions,andclearitscaptio

15、ntomakeitinvisible.SettheWhatsThisHelpIDpropertytothesamevalueasthedisabledcontrosproperty.FrankAddati9Melbourne,Australia车*余*辛*车申*车*多*拿车*拿*拿*木*余辛*车*辛*车*车本*余*拿辛*专车*率*余?*拿*车*专*车本*车*拿*拿?*辛*拿*多*车*余本*VB3,VB416/32,VB5,VB61.evel:IntennediateImproveontheBubbleSortAbubblesort,sexecutiontimeisamultipleofthes

16、quareofthenumberofelements.Becauseofthis,thebubblesortissaidtobeann-squaredalgorithm.Youcaneasilymakeimprovementstoabubblesorttospeeditup.Onewayistoreversethedirectionofpassesreadingthearray,insteadofalwaysreadingthearrayinthesamedirection.Thismakesout-of-placeelementstravelquicklytotheircorrectposi

17、tion.Thisversionofabubblesortiscalledtheshakersort,becauseitimpartsashakingmotiontothearray:PublicSubShaker(ltem()AsVariant)DimExchangeAsBooleanDimTempAsVariantDimxAsIntegerDoExchange=FalseForX=(UBound(Item)To(1.Bound(Item)+1)Step-1Ifltem(-1)ltem()ThenTemp=ltem(-1)ltem(x-1)=ltem(x)ltem(x)=TempExchan

18、ge=TrueEndIfNextXForx=(1.Bound(Item)+1)To(UBound(Item)Ifltem(x-1)ltem(x)ThenTemp=ltem(x-1)ltem(x-1)=ltem()ltem(x)=TempExchange=TrueEndIfNext1.oopWhileExchangeEndSubAlthoughtheshakersortimprovesthebubblesort,itstillexecutesasann-squaredalgorithm.However,becausemostprogrammerscancodeabubblesortwiththe

19、ireyesclosed,thisisanicewaytoshave25to33percentofftherequiredexecutiontimewithouthavingtodigoutthealgorithmbooks.Still,youdontwanttouseeitherabubbleorshakersortforextremelylargedatasets.TanShingHo,Kuala1.umpur,WestMalaysia专*专本*专*宇*今*车*专*与*冬*车才*拿*牟*多牟*专*车*今*拿*牟本*今*牟*拿*专今*牟*有今*卒*多*专*牟*专*VB432,VB5,VB61

20、.evel:IntermediateSlamSelectedItemsintoanArrayUsethiscodetoretrieveallselectedlistitemsinamultiselect-stylelistboxinoneAPIcall.It,saloteasierthaniteratingthroughalargelistusingFor.Next.Thiscodeworksagainstbothnormalandcheckbox-stylelists:PrivateDeclareFunctionSendMessage1.ibuser32Alias_SendMessageA(

21、ByVaIhWndAs1.ong,ByVaIwMsg_As1.ong,ByVaIwParamAs1.ong,IParamAsAny)As1.ongPrivateConst1.B_GETSE1.COUNT=&H190PrivateConst1.B_GETSE1.ITEMS=&H191PrivateSubCommandl_Click()DimnumSelectedAs1.ongConst1.B_ERR=-1DimrAs1.ongDimiAsInteger,getthenumberofitemsselected.,lfthelistboxissingle-selectstyle,numSelecte

22、dwillreturn-1(1.B_ERR).Ifthelistboxismultiselectstyle,andnothingisselected,numSelectedreturns0.Otherwise,numSelectedreturnsthenumberselected(ala1.istl.SeICount)numSelected=SendMessage(1.istl.hWnd,1.B_GETSE1.COUNT,_0&,ByVaI0&)debug.Debug.PrintnumSelected;itemsselected:Debug.Printindex,itemIfnumSelect

23、ed1.B_ERRThendimanarraylargeenoughtoholdtheindexesoftheselecteditemsReDimsSelected(lTonumSelected)As1.ongpassthearraysoSendMessagecanfillitwiththeselecteditemindexesCallSendMessage(1.istl.hWnd,1.B_GETSE1.ITEMS,_numSelected,SSeIected(I)debug.printouttheitemsselectednotethattheirindexisO-basedFori=1To

24、numSelectedDebug.Print1.istl.1.ist(sSelected(i)NextEndIfEndSubRandyBirch,EastYork,Ontario,Canada*VB5,VB61.evel:IntermediateCallUpWindowsShellFeaturesHere,salittleroutinethatprovidesaquickanddirtywaytocallupsomeofthemoreoddballfeaturesoftheWindowsshell.Itworksbyemulatinguserkeystrokes,soyou,llneedtom

25、odifythekeysfornon-Englishversions.SimplypastethiscodeintoastandardmoduleandpasstheEnumofchoice:PrivateDeclareSubkeybd_event1.ibuser32(ByVaIbVkAs_Byte,ByVaIbScanAsByte,ByVaIdwFlagsAs1.ong,_ByVaIdwEtralnfoAs1.ong)PublicEnumSystemKeyShortcutsExpIorerNew=&H45FindFiIes=&H46MinimizeAII=&H4DRunDiaIog=&H52

26、StartMenu=&H5BStandbyMode=&H5EEndEnum,Asc(E),Asc(F),Asc(M)Asc(R)Asc(,)Asc(a)-Win98only!PublicSubSystemActionVkActionAsSystemKeyShortcuts)ConstVK1.WIN=&H5BConstKEYEVENTF_KEYUP=&H2Callkeybd_event(VK_1.WIN,0,0,O)Callkeybd_event(VkACtion,0,0,0)Callkeybd_event(VK_1.WIN,O,KEYEVENTF.KEYUP,O)EndSubRandyBirc

27、h,EastYork,Ontario,Canada*VB5,VB61.evel:BeginningKeepTrackofIndexNumbersWhenusingcontrolarrays,Ifinditdifficulttokeeptrackoftheindexnumberofeachcontrol.Evenif1useconstants,Ioftenhavetolookuptheconstantnameforeachfield.Now,insteadofusingconstantsforeachindexnumber,Iusethiscode.First,Ideclareanenumera

28、tedtypefortheindexnumbers:EnumFieIdConstants1.astName三OFirstName=1Age=2Address=3EndEnumIthencreateapropertywrapperforthecontrolarray.Thewrappertakesinanenumeratedconstantthatrepresentstheindexnumberandreturnsthecontrolinthearray:PropertyGetFields(ByValFieIdNumAsFieIdConstants)_AsTextBoxSetFields=txt

29、Fields(FieldNum)EndPropertyTheadvantagetothiswrapperisthatwhenyoutypethepropertyname,Fields,VBpromptsyouwiththeconstantnameslistedintheenumeratedtype.Thisway,youcanrefertocontrolsinthearraybyname,andyouneverhavetolookupconstantnamesagain.Also,itmakesthecodemorelegible:PrivateSubForm_1.oad()FieIds(1.

30、astName),Text=MojicaEndSubJoscMojica,Davie,Florida*拿*/*拿*拿*VB5,VB61.evel:BeginningIncludeCodeforDebuggingVBsupportsconditionalcompilation,justlikeVisualC+.However,VisualC+hasapredefinedconstantnamed_DEBUGthatmakesiteasytoincludecodeonlywhiledebugging,asinthiscode:#ifdef_DEBUGMessageBox(NU1.1.,BeginP

31、rocedure,_DebugMessage,MB_OK);#endifInVB,youcandothesamething,butyouneedtodeclarethevariableintheConditionalCompilationArgumentsfieldsintheMaketaboftheProjectpropertiesdialog,thenremembertoremoveitbeforeshippingtheexecutable.UsingtheDebug.Assertcommandisaneasierwaytohavestatementsexecutedonlywhilede

32、buggingandnotwhenrunningacompiledprogram.Forexample,thislinedisplaysamessageboxonlywhenrunningtheprograminthedesignenvironmentandnotinacompiledprogram:Debug-AssertMsgBox(BeginForm_1.oad)ThishappensbecauseDebug.Assertworksonlyinthedesignenvironment.Toevaluatetheassertion,VBexecutesthestatement.Howeve

33、r,whenyoucompiletheprogram,thecompilerremovesthislinefromtheexecutable.JoseMojica,Davie,FloridaVB432,VB5,VB61.evel:IntermediateNlXTHEXSometimes,youwanttoshowaformthatyoudon,twantuserstobeabletocancelbyclickingontheXitmightnotmakesenseforyourapp.ThebestVBsolutionistocanceltheunloadinthefbrm,sQucryUnl

34、oadevent.However,thisallowsuserstodosomethingwrong,forwhichyouthenhavetohandleandscoldthem.Ifyoudonothing,itlooksasiftheformhasabugandwon,tcancel.AddthisroutinetoastandardBASmodule:PrivateDeclareFunctionGetSystemMenu1.ibuser32_(ByVaIhWndAs1.ong,ByVaIbRevertAs1.ong)As1.ongPrivateDeclareFunctionRemove

35、Menu1.ibuser32_(ByVaIhMenuAs1.ong,ByVaInPositionAs1.ong,_ByVaIwFlagsAs1.ong)As1.ongPrivateConstMF_BYPOSITION=&H400&PublicSubRemoveCancelMenultem(frmAsForm)DimhSysMenuAs1.ong,getthesystemmenuforthisformhSysMenu=GetSyStemMenU(frm.hWnd,O)removethecloseitemCallRemOVeMenU(hSysMenu,6,MF-BYPOSITION)removet

36、heseparatorthatwasoverthecloseitemCallRemoveMenu(hSysMenu,5,MF_BYPOSITION)EndSubThencalltheroutinefromanyformasitloads:PrivateSubForm_1.oad()RemoveCanceIMenuItemMeEndSubAfterthiscall,theClosemenuiteminthesystemmenuandtheoptiontocancelXwillbedisabled.Notethatifyou,redoingotherthingswiththesystemmenu,

37、youmighthavetoadjustthepositionnumberintheRemoveMenucall.JoshFrank9Parsippany9NewJersey*辛*余*辛*零*辛*零*辛*余*率*拿*辛*辛*零*率卒*余*辛*辛*拿*率*余辛*拿*卒*辛*小*余辛*VB3,VB416/32,VB5,VB61.evel:BeginningComputeCreditCardCheckDigitsThelastdigitinyourcreditcardnumberisacheckdigitderivedfromtheotherdigitsusingthe1.uhnFormulaasd

38、escribedinISO/IEC7812-1:1993.Itsprimarypurposeistoensureaccurateentriesofthecreditcardnumberduringtransactions.Youcanapplythesametechniquetootherapplicationssuchasemployeenumbersorpatientnumbers.Usingcheckdigitsforthesenumbersalsoensuresmoreaccuratedataentries:PublicFunctionCheckDigit(strNumAsString

39、)AsIntegerDimiAsIntegerDimiEvenAsIntegerDimiddAsIntegerDimiTotalAsIntegerDimStrOneCharAsStringDimiTempAsInteger,AdddigitsinevenordinalpositionsstartingfromrightmostFori三1.en(StrNum)-1To2Step-2StrOneChar=Mid$(StrNUm,i,1)IfIsNumeric(StrOneChar)TheniEven=iEven+CInt(StrOneChar)EndIfNextiProcessdigitsino

40、ddordinalpositions,startingfromrightmostFori=1.en(StrNum)To1Step-2StrOneChar=Mid$(StrNUm,i,1)IfIsNumeric(StrOneChar)Then,DoubleitiTemp=CInt(StrOneChar)*2IfiTemp9Then,Breakthedigits(e.g.,19becomes1+9)idd=idd+(iTemp10)+(iTemp-10)Elseidd=idd+iTempEndIfEndIfNexti,AddevenandoddiTotal=iEven+idd,Returnthe1

41、0scomplementCheckDigit=10-(iTotalMod10)EndFunctionTotest,passyourcreditcardnumber,excludingthelastdigit,asastringparameter.Theresultshouldbethelastdigitofyourcreditcardnumber.ArnelJ.Domingo,HongKong*率*弥*京京*拿*VB432,VB5,VB61.evel:AdvancedUseThisHigher-ResolutionSTOPWATCHUsethiscodetocreateaclasscalled

42、HiResTimer:ThenumberiscodifiedasHighPart*232+1.owPartPrivateType1.ARGEJNTEGER1.owPartAs1.ongHighPartAs1.ongEndTypePrivateDeclareFunctionQueryPerformanceCounter1.ib_kernel32(IpPerformanceCountAs1.ARGEJNTEGER)_As1.ongPrivateDeclareFunctionQueryPerformanceFrequency1.ib_,kernel32(IpFrequencyAs1.ARGEJNTE

43、GER)As1.ongPrivatemTicksPerSecondAsDoublePrivatem_1.IOAs1.ARGE_INTEGERPrivatem_1.llAs1.ARGEJNTEGERFriendSubClass_lnitialize()Dim1.IAs1.ARGEJNTEGERIfQueryPerformanceFrequency(1.I)0Thenm-TicksPerSecond=1.I2Double(1.I)Elsem-TicksPerSecond=-1EndIfEndSubFriendPropertyGetResolution()AsDoubleResolution=1#/

44、m_TicksPerSecondEndPropertyFriendSubEnterBIockOQueryPerformanceCounterm_1.I0EndSubFriendSubEitBlock()QueryPerformanceCounterm_1.llEndSubFriendPropertyGetEIapsedTimeOAsDoubleDimEnterTimeAsDouble,ExitTimeAsDoubleEnterTime=1.I2Double(m-1.I0)/m_TicksPerSecondExitTime=1.I2Double(m_1.ll)/m_TicksPerSecondEIapsedTime=ExitTime-EnterTimeEndPropertyFriendFunction1.I2Double(1.IAs1.ARGEJNTEGER)AsDoubleDim1.owAsDoubleConstTWO_32=4#*1024#*1024#*1024#1.ow=1.1.1.owPartIf1.owOThen1.ow=1.ow+TWO_32Now1.owisint

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号