《tips_7 面向VisualBasic程序员的杂志 第7版.docx》由会员分享,可在线阅读,更多相关《tips_7 面向VisualBasic程序员的杂志 第7版.docx(41页珍藏版)》请在第壹文秘上搜索。
1、WelcometotheSeventhEditionoftheVBPJTechnicalTipsSupplement!VB3,VB416/32,VB51.evel:BeginningDeletinganArrayElementConventionalwisdomsuggeststhattodeleteanarrayelement,youmustmoveupallthesubsequentelementstoclosethegapleftbythedeleteditem.However,ifthesequenceoftheelementsisntsignificant(asinanunsorte
2、darray),thisalgorithmquicklydeletesanitem:,Elementtodelete(Delete=5NumberofelementsbeforedeletionnElements三UBound(Array)ReplaceiDeletewithlastiteminarrayArray(IDeIete)=Array(nElements)*UseReDimPreservetoshrinkarraybyoneReDimPreserveArray(1.Bound(Array)_TonElements-1)一BasilHubbard,Hammon,Omario,Canad
3、aVB432,VB5,VBA1.evel:IntermediateInvokehOpenWith.,DialogBoxWhenlaunchingadatafilewiththeSheIIExecuteOfunction,Windowstriestofindtheassociatedapplicationandopenthedatafilewiththisapplication.Butwhathappensifnoassociationexists?SheIIExecuteOsimplyreturnserrorde31(noassociation)andnothinghappens.Wouldn
4、titbeniceifyourprograminvokedthe41Openwith.dialogboxsoyoucanchsewhichapplicationyouwanttoassociatewithyourdatafile?Heresasolution-calltheSheIIDocroutineandpassafullyqualifiedpath/filenameofthedatafileyouwishtoopen:OptionExplicitDeclareFunctionGetDesktopWindow1.ib,tuser32,()As1.ongDeclareFunctionSheI
5、IExecute1.ib_,shell32.drAliasSheIIExecuteA.(ByVaIhW11dAs1.ong,ByVaIIpOperation_AsString,ByVaIIpFiIeAsString,_ByVaIIpParametersAsString,_ByVaIIpDirectoryAsString,_ByVaInShowCmdAs1.ong)As1.ongDeclareFunctionGetSystemDIrectoryUb_“kemel32AliasGetSystemDirectoryA_(ByVaIIpBufferAsString,ByVaInSize_As1.ong
6、)As1.ongPrivateConstSE-ERR-NOASSOC=31PublicSubShellDoc(strFileAsString)DimIngRetAs1.ongDimStrDirAsStringIngRet=SheHEXeCUte(GetDeSktoPWindOw,_open”,StrFiIe,_VbNuIIString1VbNuIIString.VbNormaIFocus)IfIngRet=SE_ERR_NOASSOCThennoassociationexistsStrDir三Space(260)IngRet=GetSyStemDireCtOry(StrDir,_1.en(St
7、rDIr)StrDir三1.eft(strDir,IngRet)showtheOpenwithdialogboxCallShellExecutefGetDesktopWindow,_VbNuIIString,RUND1.1.32.EXE-,Mshell32.dll,OpenAs.RunD1.1.”&_StrFite1StrDir1VbNormaIFocus)EndIfEndSub-ThomaSWeidmannjeceivedbyemailVB432,VB51.evel:BeginningSSTABVs.OptionButtonsAlthoughVB,sSSTabntrolbehavesasif
8、eachtabpageisacontainer,itactuallyusesasinglecontainerforalltabpages.Thiscancauseunexpectedbehaviorifyouhavegroupsofoptionbuttonsondifferentpages.Clickingonanoptionbuttonononepageclearsalltheuncontainedoptionbuttonsontheother,seeminglyunrelated,pages.Solvethisproblembyaddingyourowncontainersframesor
9、pictureboxes)foreachgroupofoptionsyouwanttobemutuallyexclusive.SteveCiSCoandROIandSOUtharaFranklin,TenneSSeeVB432,VB51.evel:BeginningChangetheAppearancePropertyofaTextBoxatRunTimeSorry,youcantchangetheAppearancepropertyofatextboxatruntimebutyoucanmakeitlooklikeyouhave!Ifsettonone,a3-Dpictureboxhasaf
10、latBorderStyIeproperty.Putyourtextbox(withaflatappearance)insideapicturebox(witha3-Dappearance)andchangethepictureboxsborderstyle.UsethiscompletecodebesureyouplaceText1insidePicturel:Privatem_TextVAppearanceAs1.ongPrivateSubForm_1.oad()WithText1Picturel.Width=.WidthPicturel-Height三.Height.MoveO,OEnd
11、WithText1_Appearance1,3DEndSubPublicProperty1.et_Text1_Appearance(nAppearanceAs1.ong)WithPicturelSelectCasenAppearanceCaseOFlat.BorderStyIe三nAppearanceCase1,3D.BorderStyIe=nAppearanceEndSelectm_Text1_Appearance三.BorderStyIeEndWitfCEndPropertyPublicPropertyGetText1_AppearanceOAs1.ongTeXt1.APPearanCe三
12、m_Text1_AppearanceEndPropertyJimDeutch,CaZenOvia,NeWYorkVB432,VB51.evel:BeginningDealingWithNullValuesReturnedFromRDOResultsetsIfyou,rassigningthevaluesoflumnsyoureturnfromRDOqueriesintostringvariables,you,llgetanInvaliduseofNulerrorifoneofthecolumnshasaNullvalue.Formostpurposes,dratherhavethevaluea
13、sanemptystringanyway.RatherthancodeforthateachtimeIaccessacolumn,l,vewrittenafunctioncalledCleanthatturnsNullvaluesintoemptystrings.Icallitlikethis:strMyString=Clean(rdoResultset(MMyVarCharColumnM)IalsoconvertEmptyvaluesaswell,forusewithVariants:PublicFunctionClean(ByValvarDataAsVariant)AsStringIfIs
14、NuII(VarData)ThenClean三ElselfIsEmpty(VarData)ThenClean=ElseClean=CStr(VarData)EndlfEndFunction-JameST.Stanley,MuncieJndianaVB3,VB416/32,VB51.evel:BeginningInSearchofSampleCodemalwayslookingforsamplecode,andthesetup1.vbpfileisanexcellentsourceofreusablecode.ItcomeswithVBandispartoftheVBsetupkit.Theco
15、ntentsvary,dependingonwhatversionofVByouhave,butyoullfindusefulexamplesineachversion.Forexample,theVB5filesamplecodedoesthesethings: GetstheWindowsdirectory. GetstheWindowsSystemdirectory. Determinesifafileordirectoryexists. DeterminesifyourerunningWinNTorWin95.,Determinesdrivetype. Checksdiskspace. Createsanewpath. ReadsfromanINIfile. Parsesdateandtime. Retrievestheshortpathnameofafilentaininglongfilenames.Plus,awholemoduleworkstologerrorstoanerrorfile.Thisdeiswell-commentedandcaneasilybecutandpastedintoyourproject.