tips_6 面向VisualBasic程序员的杂志 第6版.docx
《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.EINTOACCESSMDBItcanbetroublesometoconver
17、tatextfileintoanAccessdatabase.IttakesalotoftimetoopenthefileforsequentialaccessandcreatenewrecordsusingtheAddNewmethod.Instead,usetheTextISAMdriverandSQ1.todothejobforyou.First,createaSCHEMA.INIfileforthetextfileandplaceitinthesamedirectoryasthetextfile.Usethiscodetoconvertthedatabase:DimdbAsDataba
18、se,tblasTableDefSetdb=DBEngine.CreateDatabase(App.Path&_mymdb.mdb,Cib1.angGeneral,dbVersion_0)Settbl=db.CreateTableDef(11Tem11)tbl.Connect=uTextzdatabase=C:vbpjdatautbl.SourceTableName=HCustomer#txtndb.TableDefs.Appendtbldb.ExecuteSelectTemp.*intoNewTablefromTempudb.TableDefs.Deletetbl.Namedb.CloseS
19、ettbl=NothingSetdb=NothingNow,youonlyneedtocreateindexes.Youcanusethismethodtoconverttextfilesinexcessof100,000recordsinafewseconds.-RichardMageau,Warren,RhodeIslandVB3,VB416/32,VB5,VBA1.evel:IntermediateQUICKANDEASYSCHEMAJNiTocreateaSCHEMA.INIfileforatextfilequicklyandeasily,lettheODBCsetupanddrive
20、rsdoitforyou.GototheControlPanelandstartODBC.ClickontheAddbuttonandselecttheTextdriver.ClickontheOptionsbuttonanddescribehowtoarrangethetextfile.Ifyourtextfilehasaheaderrecord,clickontheGuessbutton,andtheCoIumnNameswillbefilledoutforyou.Onthelastscreen,chooseCanceltoavoidsettingupthedatasource.Check
21、thedirectorywherethetextfileresides,andyoullfindanewSCHEMA.INIfile.-RichardMageau,Warren,RhodeIslandVB416/32,VB51.evel:IntermediateCONTRO1.ARRAYSARECO1.1.ECTIONSMostVB4programmersknowtheycanaccessaformscontrolcollectionusingaForEachloop.Many,however,arenotawarethatindividualcontrolarraysareactuallyc
22、ollections.Thisknowledgecanbehelpfulwhencheckingthepropertiesofseveralcontrols,especiallywhennewcontrolsareloadedatruntime.Youmightwishtocreateaforminwhichalltextboxesmustbefilledout.Insteadofassigningauniquenametoeachtextbox,createacontrolarraywitheachtextboxnamedttFields.Youcanusethisnameasyouuset
23、henameofanycollectioninaForEachloop:DimvForEachvIntxtFieldsIfv.Text=ThenMsgBoxuAllfieldsmustbe_,filledout.,VbExclamation+_VbOKOnlyExitSubEndIfNextBecausetxtFieldsisacollection,youcanalsodeterminehowmanytextboxesareinthearraybycheckingthevalueoftxtFields.Count.Formoreinformation,seeKarlE.Peterson1SQ&
24、Acolumn,TheKeystoYourControlsVBPJAugust1997.-DanielBuskirk,TheBronx,NewYorkVB3,VB416/32,VB5,VBA1.evel:BeginningSETAF1.AGTOTRUE/FA1.SEWhenyouwanttosetaflagtoTrueorFalse,dependingontheresultsofacomparison,youdon,tneedtouseanIfstatementlikethis:Ifx3thenGreater=TrueElseGreater=FalseEndIf1.ogicaloperator
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- tips_6 面向VisualBasic程序员的杂志 第6版 面向 VisualBasic 程序员 杂志
链接地址:https://www.31ppt.com/p-7145887.html