techtips8 面向VisualBasic程序员的杂志 第8版.docx
《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
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- techtips8 面向VisualBasic程序员的杂志 第8版 面向 VisualBasic 程序员 杂志
链接地址:https://www.31ppt.com/p-7145349.html