《tips_6 面向VisualBasic程序员的杂志 第6版.docx》由会员分享,可在线阅读,更多相关《tips_6 面向VisualBasic程序员的杂志 第6版.docx(56页珍藏版)》请在第壹文秘上搜索。
1、WE1.COMETOTHESIXTHEDITIONOFTHEVBPJTECHNICA1.TIPSSUPP1.EMENT!ThesetipsandtricksweresubmittedbyprofessionaldevelopersusingVisualBasic3.0,VisualBasic4.0,VisualBasic5.0,VisualBasicforApplications(VBA),andVisualBasicScript(VBS).VB416/321.evel:IntermediateUSEBOO1.EANVARIAB1.ESFORCHECK-BOXVA1.UESVisualBasi
2、cspecifiesaBooleansdefaultvaluesaszeroforFalseand-1forTrue.Youmaysaveandsetthevalueofacheckbox,basedontheabsolutevalueofaBooleanvariable,asthesecorrespondtotheintrinsicconstantsVbUncheckedandVbChecked:DimbBoolasBooleanbBool=True/IfbBool=0thecheckboxwillbe/unchecked,ifitisanythingelseitwill,/bechecke
3、d.Checkl.Value=Abs(bBool)-JeremyBoschen,Branchburg,NewJerseyVB416/321.evel:IntermediateDISP1.AYHORIZONTA1.SCRO1.1.BARUnliketheWindows95commoncontrols,thestandardlistboxdoesnthaveahorizontalscrollbarwhenlistitemsaretoowidetofitwithinthelistbox.Fortunately,itsnothardtodirectalist-boxcontroltodisplayah
4、orizontalscrollbar.Addthiscodetoaforms1.oadevent.Itfillsalistboxwith100longstringsandcallsSetHScroIItoshowahorizontalscrollbarinthelistbox:PrivateSubForm_1.oad()DimiAsIntegerFori=1To1001.istl.AddItemCStr(i)&_ubottle(三)ofbeeronthewall.uNextiSetHScrollMe,1.istl,1.istl.1.ist(0)EndSubAddthiscode,whichin
5、cludestherequiredAPIdeclarationsandtheSetHScroIIroutine,toaBASmodule.TheSetHScroIIroutineusestheSendMessageAPIfunctiontosendthe1.B_SETHORIZONTA1.EXTENTmessagetoalistbox.Thelastargumentisanitemfromthelist,preferablyoneofthelongestitems.SetHScroIIdeterminesthestringswidthinpixelsandpassesthisvaluetoth
6、elistboxalongwiththe1.B_SETHORIZONTA1.EXTENTmessage.Thelistboxsetsitshorizontalextenttothisvalue,andifitiswiderthanthelist-boxcontrol,thelistboxdisplaysahorizontalscrollbar:#IfWin32ThenDeclareFunctionSendMessage1.ib11user3211_AliasnSendMessageAn(_ByValhwndAs1.ong,ByValwMsgAs1.ong,ByValwParamAs1.ong,
7、IParamAs1.ong)As1.ong#ElseDeclareFunctionSendMessage1.ib,user3211_AliasSendMessageAn(_ByValhwndAsInteger,ByValwMsgAsInteger,ByValwParamAsInteger,IParamAs1.ong)As1.ong#EndIf,Defineconstantformessagetolist-boxcontrolConst1.B_SETHORIZONTA1.EXTENT=&H194PublicSubSetHScroll(FrmAsForm,CtrlAs_Control,StrTex
8、tAsString)DimnScaleModeAsIntegerDimnTextWidthAsInteger,ScaleinpixelsforwindowmessagenScaleMode=Frm.ScaleModeFrm-ScaleMode=3,Getthewidth,inpixels,ofthetextstringnTextWidth=Frm.TextWidth(StrText),SendamessagetothelistboxSendMessageCtrl.hwnd,1.B_SETHORIZONTA1.EXTENT,nTextWidth,O,Restorepreviousscalemod
9、eFrm.ScaleMode=nScaleModeEndSub-PeterGomis,Shelton,ConnecticutVB416/32,VB51.evel:BeginningGETTHETRUEMEMBEROFANOPTIONARRAYSettinganelementinanarrayofoptionbuttonstoTrueiseasy.Clickontheoptionbutton,orusethiscode:OptionArray(ThisOne)=TrueThisOneistheIndexofthememberyouwanttobeselected.GettingtheTrueme
10、mberofanoptionarrayisnotsosimple.Becauseyouneedtoperformthiskindoftaskinalmostanyreasonablycomplexprogram,addingthisfunctiontoyourcodelibraryorgenericmodulesimplifiesthetask.Youdon,tneedtoknowthearraysizeinadvance:FunctionOptionTrueIs(OptionArrayNameAs_Variant)AsInteger,ReturnstheindexoftheTrue,memb
11、erofanoptionarrayDimCtlasControlForEachCtlinOptionArrayNameIfCtl.Value=TrueThenOptionTruels=Ctl.IndexExitForEndIfNextEndFunctionYoucanusetheroutinetosetaPublicvariablefromaPropertiessheetusingthisformat:SomePublicVariable=OptionTrueIs(SomeOptionArray()Orusethiscodetosaveaprogramvariablebetweenruns:S
12、aveSetting(uMyAppu,uOptionSettingsu,_OptionKeyn,OptionTrueIs(SomeOptionArray()1.oadtheroutineusingthiscode:SomeOptionArray(GetSetting(uMyAppu,uuzOptionsettings,OptionKeyn,O)j=TrueOryoucanloadtheroutineusingthiscode:SomePublicVariable=GetSetting(uMyAppu,11zOptionSettings,nOptionKeyn,O)Usethiscodetoco
13、ntrolaSelectCasestructure:SelectCaseOptionTrueIs(SomeOptionArray()CaseO:,ThiscodewillexecuteifelementO,ofSomeOptionArray()isselected.EndSelectUsethiscodetotestacondition:IfOptionTrueIs(SomeOptionArray()=SomeValueThen,Thiscodewillexecuteifelement,SomeValueofSomeOptionArray()is,selected.EndIf-RogerGil
14、christ,Ourimbah,NewSouthWales,AustraliaVB416/32,VB51.evel:IntermediateDEBUGERRORHAND1.ERSVisualBasicgivesyoutheoptiontoturnofferrortrappinginyourprojects.Thisisusefulwhenyoususpectthatyourerrorhandlerscontainerrorsthemselves.Toturnofferrorhandlingglobally,chooseOptionsfromtheToolsmenu.SelecttheGener
15、altab,andselectBreakonAllErrors.Thenexttimeyourunyourprojectinthedevelopmentenvironment,VisualBasicwillbreakwheneveranerroroccursinyourcode,whethertheerroristrappedornot.-JeffreyP.McManus,SanFrancisco,CaliforniaVB3,VB416/32,VB51.evel:BeginningPROTECTDATAWITHINMODU1.ESUsePrivatevariablesinBASmodulest
16、oprotectdatathatthemodulesroutinesmustaccess,butwhichshouldbehiddenfromtherestoftheapplication:Dimhidden_dataAsIntegerFunctionGetData()AsIntegerGetData=hidden_data*2EndFunction-RodStephens,Boulder,ColoradoVB3,VB416/32,VB5,VBA1.evel:IntermediateCONVERTATEXTFI1.EINTOACCESSMDBItcanbetroublesometoconvertatext