Escolar Documentos
Profissional Documentos
Cultura Documentos
6Notes,page1
CIS230,VisualBasic
Ch.6Notes,page1
Objective#1:Createaprojectwithmultipleforms.
Practicallyspeaking,youmustusemultipleformsinaVBprojecttocreateanapplicationthatisnontrivial.Whileyoucouldmakeextensive
useofframestogrouprelatedobjectsandbeverycreativewiththeVisibleproperty,limitingyourselftooneformwouldmakecoding
unnecessarilycomplicated.Creatingapleasinguserinterfacerequiresmultipleformsinaproject.
Youcancreateanewform(moreformallyknownasaformmodule)simplybychoosingthemenucommandProject/AddFormandthen
selectingFormfromtheresultingwindow.
YoucanaddapreexistingformtoaprojectbychoosingthemenucommandProject/AddFormandthenselectingtheExistingtab.Thisdoes
notduplicatetheaddedformwhichmayexistaspartofanotherVBproject.Itsimplylinksthatform(.frmfile)toyourcurrentproject.Ifthe
formisrelocatedinthefuture,theprojectwillnotbeabletoaccesstheform.
Youcanaddasmanyformsasyouwishtoaprojectbutaddingtoomanymaymaketheprojectunmanageable.
Thefirstformthatisdisplayedoractivatedinaprojectiscalledthestartupform.
Objective#2:UsetheShowandHidemethodstodisplayandhideforms.
TheHidemethodofaformmakesitdisappear.Forexample,iftheformfrmLevel2wereplaceddirectlybehindformfrmLevel1,thenthe
statementfrmLevel1.HidewouldcausefrmLevel1todisappearandrevealfrmLevel2.
TheShowmethodofaformmakesitappearonthescreen.
TheShowmethodcanbesetto0formodelessor1formodal.(TheVBconstantsVBModelessandVBModalalsocanbeusedas
constantvalues.)Forexample,thestatement
frmLevel1Show1displaysfrmLevel1inamodalstate.ThedefaultismodelessifthestatementfrmLevel1Showisused
withoutspecifyingavalue.
Displayingaforminmodelessstateallowstheusertoclosethatformorsimplyignorebymakinganotherformactive.
Displayingaformasmodal,yourequiretheusertorespondtotheforminsomeway.Thiscanbemoreeffectivethanusingamessage
box,especiallysinceyoucandomuchmorewithaformthanasimplemessagebox.
Twospecialeventsoccurbehindthesceneseverytimethataformfirstdisplaysinaproject,theFormLoadandFormActivateevents,which
arenotusergenerated.
TheForm_Loadcallstheformmoduleintomemory(RAM).
AsplitsecondlatertheForm_Activateeventoccursandactuallyactivatestheform,passingcontroltoit.IftheformishidwiththeHide
method,onlytheForm_Activatemethodwillbeinvokedwhenitisshownagain.Iftherearetasksorinitializingstepsthatmustbe
performedeverytimetheformisshown,youshouldplacethosetasksandstatementsintheform'sForm_Activateprocedure.
Ifyounolongerneedtouseaformthroughouttheremainderoftheexecutingproject,youshouldconservememorybyunloadingtheform.
ThiscanbedonewiththestatementUnloadfrmLevel1.Simplyhidingaformdoesnotclearthememorythatituseswhichcouldbe
significantonoldercomputersinprojectsthatusemanyforms.
WhiletheShowmethodautomaticallyloadsaform,youcanintentionallyloadaformbeforeyouplantouseitinordertosavetimeandmake
theformappearfaster.ThiscanbedonewiththestatementLoadfrmLevel1
http://www.minich.com/education/racc/visualbasic/cis230ch6/ch6notes1.htm 1/3
1/29/2017 VisualBasicCh.6Notes,page1
TheMekeywordcanbeusedanywhereinyourcodethatyouwouldusetheformnameofthecurrentlyactiveform.IftheformfrmLevel1was
theactiveformthenthestatementUnloadMewouldcleartheformfromthememory.
Objective#3:Createproceduresthatareaccessiblefrommultipleformmodules.
Whenyoucreateanewgeneralprocedureyoumustcarefullydeterminewhetheritwouldbesomethingthatisusefultoanumberofform
modulesoronlyoneformmodule.Ifisisonlyappropriateforoneformmodulethenyoushouldplaceitinthatformmoduleandusethe
keywordPrivateinitsheader.Ifyouthinkthattheprocedurecouldbeusedinnumerousformsthenyoushouldplaceitinastandardcode
moduleandusethekeywordPublicinitsheader.
Astandardcodemodule(.basfile)isnotassociatedwithaparticularformbutispresentwithintheproject.Infact,youshould(butarenot
requiredto)namethecodemodulewiththesamenameastheprojectitself(theextensionwillbe.basinsteadof.vbpthough).
Toaddacodemodule,choosethemenucommandProject/AddModuleandthenselectModuleontheNewtab.Ofcourseyoucanreuse
standardcodemodulesthatbelongtootherprojectsaswell.
Onceyouhavecreatedacodemodule,itwillappearintheProjectExplorerwindow.
Byplacinggeneralprocedureswithinacodemoduleyoucaneasilymakethemaccessibletomultipleformmodules.Anyprocedureina
standardcodemodulecanbecalledbyanyprocedureinanymoduleoftheproject.However,ifyouusethePublickeywordintheheaderofa
procedureinaformmodule,thenthatprocedurecanalsobecalledbyproceduresinothermodules.
Objective#4:Differentiatebetweenvariablesthatareglobaltoaprojectandthosevisibleonlytoaform.
Variablesandconstantscanhavethreedifferentlevelsofscope:local,modulelevel,andglobal.Itiswisetokeepthescopeofavariableas
narrowaspossible.Thatis,youshouldtrytorefrainfromusingmodulelevelandglobalvariables.Youmayhavedifficulttodebugsideeffects
inyourlogicifyouuseglobals.
Ifyoudowishavariabletobeaccessibletomorethanoneformmodule,youmustmakeitaglobal.SimplyusethekeywordPublicinsteadof
Diminitsdeclarationstatement.However,itmayconfusethosewhoreadyourprogramifyou"hide"globalvariablesinformmodulesinthis
way.Youshouldalwaysdeclaresuchglobalvariablesinthegeneraldeclarationssectionoftheproject'sstandardcodemodulesothattheyare
easytofindandidentify.YoustillhavetousethekeywordPublictomakethevariableglobalevenifitisdeclaredinthegeneraldeclarations
sectionofthecodemodule.
Youshouldusethethesingleletterprefixesofgandmwithinidentifiers(i.e.names)ofglobalandmodulelevelvariablesrespectively.
Therefore,thevariablenameforaglobalmightbegcurSalary.Localvariablesdonotneedaprefixsinceitisassumedthatmostofyour
variableswillhavelocalscope.
Besidestheaspectofavariable'sscope,itisimportanttomanageavariable'slifetimeaswell.Localvariablesareonlystoredinmemorywhile
aprocedureisexecuting.Assoonastheprocedureends,itslocalvariablesresettozero(orareactuallyabandonedbythecomputer'smemory.)
Ifyouwishtokeeptrackofarunningtotalforexample,throughouttheexecutionofaform,youshoulddeclarethevariableasStatic.Simply
usethekeywordStaticinsteadofDim.Forexample,StaticintRunningTotalAsIntegerdeclaresintRunningTotalinsuchaway.
ReadtheGuidelinesforDeclaringVariablesandConstantssectiononp.216veryclosely.Youwillberesponsibleforfollowingthoserules
andconventions.
Objective#5:CreateanAboutboxusingaform.
http://www.minich.com/education/racc/visualbasic/cis230ch6/ch6notes1.htm 2/3
1/29/2017 VisualBasicCh.6Notes,page1
MostWindowsapplicationsgivetheusertheopportunitytoseethenameandversionofthesoftwareinanAboutbox.Often"About..."isa
menucommandundertheHelpmenu.Youcaneasilyplacesuchinformationonaformwithlabelsandaddamenuoptionorcommandbutton
thatinvitestheusertoseethisinfo.
VBalsooffersatemplateAboutDialogformthatcanbefoundbychoosingthemenucommandProject/AddForm.
Objective#6:Addasplashscreentoyourproject.
AsplashscreencanbeaddedtoaVBprojecttomakeitlookprofessionalaswell.Asplashscreenloadsassoonasauserexecutesaprogram.
Itoftenmakesitappearasiftheapplicationisloadingfasterthanitreallyis.
YoucancreateyourownsplashscreenoryoucanaddaVBtemplatefoundundertheProject/AddFormmenucommand.Ifyouaddyourown,
youshouldmakesurethatprogramexecutionbeginswiththespecialsubprocedureMainthatislocatedinyour.basstandardcodemodule.
Objective#7:SetthestartupformorSubMaintostartprojectexecution.
YoucanchoosetohaveoneoftheformsinaVBprojectchosenasthestartupform.Thestartupformissimplythefirstformtoloadand
execute.YoucanselectanyforminyourprojectasthestartupformfromtheProject/ProjectProperties...menucommand.Underthegeneral
tabyoucanselectthe"startupobject."
YoucanalsochoosetheMainsubproceduretobethestartupobjectofaproject.Inthiscase,anycodewithintheMainsubprocedurewill
executeimmediately.Inthiscaseifyouwishtoshowasplashscreen,youmustshowthesplashscreenformwithacommandlike
frmSplash.Showsincethatwillactivatethesplashscreen.
CIS230HomePage|Mr.Minich'sEducationHomePage|Minich.comWebDesign
http://www.minich.com/education/racc/visualbasic/cis230ch6/ch6notes1.htm 3/3