B^Xgdhd[i

œ
J]giU`6Ug]W
œ
'%%*
:megZhh:Y^i^dc
7j^aY V Egd\gVb
X1146480A01.indd 1 10/18/05 9:07:32 PM
PUBLISHED BY
Microsoft Press
A Division of Microsoft Corporation
One Microsoft Way
Redmond, Washington 98052-6399
Copyright © 2006 by Microsoft Corporation
Information in this document, including URL and other Internet Web site references, is subject to change without notice. Unless other-
wise noted, the example companies, organizations, products, domain names, e-mail addresses, logos, people, places, and events depicted
herein are f ctitious, and no association with any real company, organization, product, domain named, e-mail address, logo, person,
place, or event is intended or should be inferred. Complying with all applicable copyright laws is the responsibility of the user. Without
limiting the rights under copyright, no part of this document may be reproduced, stored in or introduced into a retrieval system, or trans-
mitted in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or for any purpose, without express
written permission of Microsoft Corporation.
Microsoft, Direct X, Excel, Hotmail, IntelliSense, JScript, Microsoft Press, MSDN, MSN, Outlook, Tahoma, Visual Basic, Visual C#,
Visual Studio, Visual Web Developer, Win32, Windows, Windows Server, and Xbox are either registered trademarks or trademarks of
Microsoft Corporation in the United States and/or other countries.
This book expresses the author`s views and opinions. The information contained in this book is provided without any express, statutory,
or implied warranties. Neither the authors, Microsoft Corporation, nor its resellers or distributors will be held liable for any damages
caused or alleged to be caused either directly or indirectly by this book.
Body Part No. X11-75551
X1146480A01.indd 2 10/18/05 9:07:32 PM
6[TcgXe$
<agebWhV\aZ@\VebfbYgšI\fhT_5Tf\Vš
%##(8kceXff $
what ls .N£T? 2
what ls VisuaI Basic 2005? 4
ls visual 8asic 2005 an ObjecL-OrienLed Programming
(OOP) Language¯ 4
what ls VisuaI Basic 2005 £xpress £dition? 9
WhaL Kinds oí ApplicaLions Can ¥ou 8uild wiLh
visual 8asic 2005 Fxpress FdiLion¯ í0
WhaL Are Lhe Key FeaLures ¥ou Need Lo Know AbouL¯ í0
6[TcgXe%
<afgT__\aZI\fhT_5Tf\V%##(8kceXff8W\g\ba $(
Preparing to lnstaII VisuaI Basic 2005 £xpress
£dition l6
lnstaIIing VisuaI Basic 2005 £xpress £dition l7
6[TcgXe&
6eXTg\aZLbhe9\efg4cc_\VTg\ba %(
Two Types of AppIications. what's the Difference? 26
Getting Started 27
lDF ComponenLs 80
On to the Projects 3l
8uilding a Console ApplicaLion 8í
6bagXagf
iii
CeLLing Lo Know SoluLion Fxplorer 82
CeLLing Help. MicrosoíL visual SLudio 2005 Fxpress FdiLion
DocumenLaLion 88
Coding ¥our Console ApplicaLion 87
CusLomizing Lhe lDF 89
CreaLing a Windows ApplicaLion 4í
6[TcgXe'
6eXTgXLbheBjaJXU5ebjfXe\a?XffG[Ta
9\iX@\ahgXf '(
what ls a Project? 46
what ls the Design Layout? 47
Putting lt AII Together 54
6[TcgXe(
6eXTg\aZLbhe9\efg9h__J\aWbjfš4cc_\VTg\ba (*
Snap and AIign Those ControIs Using Snap Lines 58
Using lnteIIiSense-Your New Best lriend! 59
lnLelliSense and CLrl-Spacebar 60
lnLelliSense and Period/LeíL ParenLhesis 60
lnLelliSense FilLering. Removing Lhe "Uncommon" 62
lnLelliSense Code SnippeLs. 1he 1ime Saver 62
How Lo lnvoke Code SnippeLs 68
keaI-Time £rror Detection and Correction 64
Oh My, My . . . My ls Great 66
X1146480A02.indd 3 10/19/05 2:21:31 PM
iv Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
WhaL 1ypes oí 1asks Can Bn Help Me WiLh¯ 68
kenaming 68
WhaL Can ¥ou Rename¯ 68
How and Where Lo Use Lhe Rename FeaLure 69
Common windows ControIs 72
what Happens when an £vent ls Triggered? 75
6[TcgXe)
@bW\YlLbheJXU5ebjfXeAbj +$
How to Bring Up Your AppIication 82
lnteracting Through DiaIog Boxes 88
Adding an AbouL 8ox 88
Adding a NavigaLe Dialog 8ox 9í
A ProfessionaI Look and leeI at Your lingertips 94
Adding a 1ool SLrip ConLainer and Some 1ools 94
Adding a SLaLus 8ar Lo ¥our 8rowser 96
Personalize ¥our ApplicaLion wiLh Windows lcons í00
6[TcgXe*
9\k\aZg[X5eb^Xa5_bV^f $#,
Debugging an AppIication ll0
Using a DLL in an ApplicaLion íí0
Adding a Reíerence Lo ¥our ApplicaLion ííí
8reakpoinLs, Locals, FdiL and ConLinue, and visualizers íí2
6[TcgXe+
@TaTZ\aZg[X7TgT $%(
what ls a Database? l26
WhaL's ln a DaLabase¯ í26
DaLa NormalizaLion and DaLa lnLegriLy í27
WhaL ls Null¯ í80
WhaL Are Primary Keys and Foreign Keys¯ í80
How Do ¥ou lnLeracL wiLh a RelaLional DaLabase¯ í88
SOL Server 2005 £xpress in VisuaI Basic 2005
£xpress £dition l34
CreaLing a DaLabase Using visual 8asic 2005
Fxpress FdiLion í85
CreaLing 1ables in ¥our DaLabase í87
CreaLing RelaLionships 8eLween Lhe 1ables í89
FnLering DaLa in SOL Server 1ables Using
visual SLudio í48
what Are ADO.N£T and Databinding? l46
1he Car 1racker ApplicaLion DevelopmenL í48
ComponenL 1ray í58
How Do l CeL More Meaningíul lníormaLion
On My Form¯ í54
6[TcgXe,
5h\_WLbheBjaJXTg[XeGeTV^Xe
4cc_\VTg\baAbj $)&
leatures of the weather Tracker AppIication l64
1he High-Level Plan í64
Creating the AppIication User lnterface l65
Adding NoLiíicaLion Area CapabiliLies í67
Adding Lhe Splash Screen and AbouL Dialog 8ox í78
Adding Lhe OpLions Dialog 8ox í75
Using web Services l77
User SeLLings í8í
Working in Lhe 8ackground í82
CompleLing Lhe Core WeaLher 1racker FuncLionaliLy í87
1esLing WeaLher 1racker í98
Working wiLh Lhe OpLions Dialog 8ox í94
1esLing WeaLher 1racker í97
And Now, Just CIickOnce! l98
X1146480A02.indd 4 10/19/05 2:21:40 PM
lntroduction v
<agebWhVg\ba
visual 8asic 2005 Fxpress and Lhe oLher visual SLudio 2005
Fxpress FdiLion producLs are, in my opinion, one oí Lhe besL
and mosL inLelligenL ideas Lo come ouL írom Developer
Division here aL MicrosoíL. l'm applauding and cheering íor
Lhe people who had Lhis brillianL idea because l believe Lhere
is a real need and demand íor a world-class and poweríul
producL íor Lhe hobbyisL programmers, sLudenLs, and pro-
íessional developers. And visual 8asic 2005 Fxpress FdiLion
answers all oí LhaL and more.
visual 8asic 2005 Fxpress FdiLion is a íully íuncLional
subseL oí visual SLudio 2005, suiLable íor creaLing and main-
Laining Windows applicaLions and libraries. lL's noL a Limed-
bomb ediLion, a demo, or a íeaLure limiLed version, no, iL's a
key MicrosoíL iniLiaLive Lo reach more people and give Lhem
Lhe abiliLy Lo have íun while creaLing cool soíLware.
who ls This Book lor?
1his book is íor everybody. sLudenLs, hobbyisL programmers,
and also íor people who always LhoughL programming was a
Lough Lask. lL's íor people who had ideas like. l wish l could
build a Lool Lo sLore all my recipes, l wish l could prinL Lhem
and send Lhem Lo my íriends OR l wish l could build Lhis
cool card game LhaL l have never íound elsewhere OR l wish
l could build Lhis cool soíLware Lo sLore my DvD and CD col-
lecLion OR l wish l could build Lhis soíLware Lo help me work
wiLh maLrices and ploL graphics íor my maLh class and many
more projecLs LhaL one can Lhink oí!
1his book is íor people who have ideas buL don'L know
how Lo bring Lhem Lo realiLy. lL's a good inLroducLion Lo Lhis
arL and science LhaL is developing soíLware.
How This Book ls Organized
1his book consisLs oí nine chapLers, each covering a parLicu-
lar íeaLure or Lechnology abouL visual 8asic 2005 Fxpress
FdiLion. MosL chapLers build on previous chapLers, so you
should plan on reading Lhe maLerial sequenLially.
Conventions and leatures in This Book
1his book presenLs iníormaLion using convenLions designed
Lo make Lhe iníormaLion readable and easy Lo íollow. 8eíore
you sLarL Lhe book, read Lhe íollowing lisL, which explains
convenLions you'll see LhroughouL Lhe book and poinLs ouL
helpíul íeaLures in Lhe book LhaL you mighL wanL Lo use.
7cbjYbh]cbg
Q Fach exercise is a series oí Lasks. Fach Lask is presenLed as
a series oí numbered sLeps (í, 2, and so on). Fach exer-
cise is preceded by a procedural heading LhaL leLs you
know whaL you will accomplish in Lhe exercise.
Q NoLes labeled "1ip" provide addiLional iníormaLion or
alLernaLive meLhods íor compleLing a sLep successíully.
X1146480A02.indd 5 10/19/05 2:21:48 PM
vi Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
Q NoLes labeled "CauLion" alerL you Lo iníormaLion you
need Lo check beíore conLinuing.
Q 1exL LhaL you Lype or iLems you selecL or click appear in
bold.
Q Menu commands, dialog box LiLles, and oLher user inLer-
íace elemenLs appear wiLh each word capiLalized.
Q A plus sign (-) beLween Lwo key names means LhaL you
musL press Lhose keys aL Lhe same Lime. For example,
"Press AlL-1ab" means LhaL you hold down Lhe AlL key
while you press Lhe 1ab key.
Ch\Yf:YUhifYg
Q Shaded sidebars LhroughouL Lhe book provide more in-
depLh iníormaLion abouL Lhe conLenL. 1he sidebars mighL
conLain background iníormaLion, design Lips, or íeaLures
relaLed Lo Lhe iníormaLion being discussed.
Q Fach chapLer ends wiLh an ln Summary. secLion LhaL
brieíly reviews whaL you learned in Lhe currenL chapLer
and previews whaL Lhe nexL chapLer will presenL.
System kequirements
¥ou'll need Lhe íollowing hardware and soíLware Lo compleLe
Lhe exercises in Lhis book.
Q MicrosoíL Windows XP wiLh Service Pack 2, MicrosoíL
Windows Server 2008 wiLh Service Pack í, or MicrosoíL
Windows 2000 wiLh Service Pack 4
Q MicrosoíL visual 8asic 2005 Fxpress FdiLion
Q PC wiLh a PenLium lll-class processor, 600 MHz
Recommended. í CHz
Q í28 M8 RAM (256 M8 or more recommended)
Q video (800 x 600 or higher resoluLion) moniLor wiLh aL
leasL 256 colors (í024 x 768 High Color í6-biL recom-
mended)
Q CD-ROM or DvD-ROM drive
Q MicrosoíL Mouse or compaLible poinLing device
¥ou'll also need adminisLraLor access Lo your compuLer
Lo coníigure SOL Server 2005 Fxpress.
Code SampIes
1he code samples íor Lhis book can be downloaded írom Lhe
book's companion conLenL page aL Lhe íollowing address.
]iie/$$lll#b^Xgdhd[i#Xdb$bhegZhh$XdbeVc^dc$%",(*+"''&("'$
¥ou'll use Lhe code samples and sLarLer soluLions as you
períorm Lhe exercises in Lhe book. 8y using Lhe code sam-
ples, you won'L wasLe Lime creaLing íiles LhaL aren'L relevanL
Lo Lhe exercise. 1he íiles and Lhe sLep-by-sLep insLrucLions in
Lhe lessons also leL you learn by doing, which is an easy and
eííecLive way Lo acquire and remember new skills. ¥ou'll also
íind Lhe compleLe soluLions ií you wanL Lo veriíy your work
or ií you simply wanL Lo look aL iL.
The CD-kOM packaged in the
back of this book contains the
VisuaI Basic 2005 £xpress £dition
software needed to compIete the
exercises in this book.
ABG 8
X1146480A02.indd 6 10/19/05 2:21:48 PM
lnstaIIing the Code SampIes
Follow Lhese sLeps Lo insLall Lhe code samples on your compuLer.
$ 8ckb`cUXh\YWcXYgUad`YgZfca]iie/$$lll#b^Xgdhd[i#Xdb$bhegZhh$XdbeVc^dc$%",(*+"''&("'$"
% 5ZhYfmciXckb`cUXh\YWcXYgUad`YgZ]`Yfibh\Y]bghU``Yf"
& :c``ckh\Y]bghfiWh]cbgh\UhUddYUf"
1he code samples are insLalled Lo Lhe íollowing locaLion on your compuLer.
My DocumenLs\MicrosoíL Press\v8 2005 Fxpress
Using the Code SampIes
Fach chapLer in Lhis book explains when and how Lo use any code samples íor LhaL chapLer.
When iL's Lime Lo use a code sample, Lhe book will lisL Lhe insLrucLions íor how Lo open Lhe
íiles. 1he chapLers are builL around scenarios LhaL simulaLe real programming projecLs, so
you can easily apply Lhe skills you learn Lo your own work.
For Lhose oí you who like Lo know all Lhe deLails, a lisL oí Lhe code sample projecLs
appears on Lhe nexL page. AlmosL all projecLs have soluLions available íor Lhe pracLice exer-
cises. 1he soluLions íor each projecL are included in Lhe íolder íor each chapLer and are
labeled CompIete.
UninstaIIing the Code SampIes
Follow Lhese sLeps Lo remove Lhe code samples írom your compuLer.
$ =b7cbhfc`DUbY`cdYb5XXCfFYacjYDfc[fUag"
%
:fcah\Y`]ghcZ7iffYbh`m=bghU``YXDfc[fUaggY`YWhA]WfcgcZhJ]giU`6Ug]W&$$)9ldfYgg9X]h]cb.
6i]`XUDfc[fUaBckUbXW`]W_FYacjY"
& :c``ckh\Y]bghfiWh]cbgh\UhUddYUfhcfYacjYh\YWcXYgUad`Yg"
lntroduction vii
X1146480A02.indd 7 10/19/05 2:21:49 PM
viii Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
Project Description
ChapLer í & 2 No sample projecLs
ChapLer 8
MyFirsLConsoleApplicaLion ApplicaLion LhaL Lakes Lwo numbers and adds Lhem
LogeLher, Lhen displays Lhe sum in a console window.
MyFirsLWindowsApplicaLion Same applicaLion buL displays Lhe resulL in a
message box.
ChapLer 4
MyOwn8rowser Simple Web browser applicaLion LhaL enables Lhe
user Lo browse on Lhe lnLerneL.
ChapLer 5
1esLProjecL ApplicaLion LhaL enables you Lo use Lhe mosL imporLanL
íeaLures in visual 8asic 2005 Fxpress FdiLion.
ChapLer 6
MyOwn8rowser 1his is Lhe conLinuaLion oí Lhe applicaLion írom
ChapLer 4. lL is Lhe Web browser Lo which you'll add
menus, Loolbars, a sLaLus and progress bar, and a
navigaLion window wiLh auLocompleLe.
ChapLer 7
Debugger An applicaLion íull oí problems Lo help you learn how
Lo debug using íeaLures oí visual 8asic 2005
Fxpress FdiLion.
ChapLer 8
Car1racker An applicaLion enabling Lhe user Lo Lrack car ads írom
Lhe lnLerneL using a SOL Server 2005 Fxpress daLabase
Lo sLore Lhe iníormaLion.
ChapLer 9
WeaLher1racker An applicaLion LhaL runs in Lhe sysLem-Lray and has
a nice Ul Lo display weaLher daLa collecLed by your
applicaLion írom diverse Web services. A deploymenL
package is also creaLed íor Lhe disLribuLion oí your
applicaLion.
X1146480A02.indd 8 10/19/05 2:21:49 PM
PrereIease Software
1his book was reviewed and LesLed againsL Lhe AugusL 2005
release candidaLe. 1his book is expecLed Lo be íully compaL-
ible wiLh Lhe íinal release oí visual SLudio 2005. lí Lhere are
any changes or correcLions íor Lhis book, Lhey'll be collecLed
and added Lo a MicrosoíL Knowledge 8ase arLicle. See Lhe
"SupporL íor 1his 8ook" secLion in Lhis lnLroducLion íor more
iníormaLion.
TechnoIogy Updates
As Lechnologies relaLed Lo Lhis book are updaLed, links Lo
addiLional iníormaLion will be added Lo Lhe MicrosoíL Press
1echnology UpdaLes Web page. visiL Lhis page periodically
íor updaLes on visual SLudio 2005 and oLher Lechnologies.
]iie/$$lll#b^Xgdhd[i#Xdb$bhegZhh$jeYViZh$
Support for This Book
Fvery eííorL has been made Lo ensure Lhe accuracy oí Lhis
book and Lhe companion conLenL. As correcLions or changes
are collecLed, Lhey'll be added Lo a MicrosoíL Knowledge
8ase arLicle. 1o view Lhe lisL oí known correcLions íor Lhis
book, visiL Lhe íollowing arLicle.
]iie/$$hjeedgi#b^Xgdhd[i#Xdb$`W$.%*%()
MicrosoíL Press provides supporL íor books and compan-
ion conLenL aL Lhe íollowing Web siLe.
]iie/$$lll#b^Xgdhd[i#Xdb$aZVgc^c\$hjeedgi$Wdd`h$
Ouestions and Comments
lí you have commenLs, quesLions, or ideas regarding Lhe
book or Lhe companion conLenL or have quesLions LhaL are
noL answered by visiLing Lhe siLes above, please send Lhem
Lo MicrosoíL Press via e-mail Lo
bhe^ceji5b^Xgdhd[i#Xdb
or via posLal mail Lo
MicrosoíL Press
ALLn. visual 8asic 2005 Fxpress FdiLion. 8uild a Program
Now! FdiLor
One MicrosoíL Way
Redmond, WA 98052-6899
Please noLe LhaL MicrosoíL oííers no soíLware producL
supporL Lhrough Lhese addresses.
lntroduction ix
X1146480A02.indd 9 10/19/05 2:21:50 PM
4Ubhgg[X4hg[be
Patrice PeIIand
PaLrice Pelland is a Lechnical lead aL MicrosoíL working in
Lhe Developer Division. He loves .NF1 and Web services
and, íor Lhe pasL íour years, he has been working, Leaching,
evangelizing, and Lalking abouL Lhem Lo everyone.
For Lhe pasL Lwelve years, he has been working in
soíLware developmenL in various roles. developer, projecL
lead, manager and menLor, and soíLware engineer in OA
organizaLions. He has vasL experience spanning mulLiple
Lechnologies and íields. developer Lools, íiber opLics Lele-
communicaLion, aviaLion, coííee and dairy companies, and
also Lhree years Leaching compuLer science and soíLware
developmenL aL a college in Canada.
When noL developing greaL Lools íor developers and
helping cusLomers LhroughouL Lhe world, he enjoys spend-
ing Lime wiLh his íamily and íriends, playing games on
X8OX and PC, reading books, reading abouL Porsche and
dreaming abouL driving one, playing hockey, waLching
NHL hockey and NFL íooLball, and having greaL dinners
wiLh good íood and íine drinks wiLh íriends and íamily. He
resides wiLh his íamily in Sammamish, WA.
Dedication
1his book is dedicaLed Lo my wiíe, Hélène. My wiíe is a breasL
cancer survivor, and her courage and sLrengLh push me Lo
do beLLer Lhings and Lo íace more complex challenges. She's
beauLiíul, she's my idol, my inspiraLion, my sunshine, my
besL íriend, my love, and an awesome moLher! Mon amour,
Lhanks íor being who you are and íor being Lhere íor me! l
love you!
Thanks
FirsL oí all, Lhanks Lo my parenLs. Mom, Dad, you gave me
all Lhe chances Lo be whaL l am in liíe and you gave me Lhe
values Lo be Lhe man l am. 1hanks and l love you!
A book is a huge advenLure in somebody's liíe, and iL
would noL be possible wiLhouL Lhe help oí many people. l've
always read Lhe Lhank you secLions in oLher people's books
and l was always amazed aL how many people are needed
Lo make a book whaL iL is. Now l really undersLand why!!!
While wriLing a book is Lough, real Lough, iL's really saL-
isíying aL Lhe same Lime. During Lhe wriLing process, you
someLimes have doubLs, and l had my share oí--especially
Lhose nighLs aL 8.00 a.m. when all oLher souls in Lhe house
are asleep, even my dog, and Lhe producL had a bug pre-
venLing me írom LesLing someLhing, when l was in íronL oí
my lapLop wiLh an excepLion and a whiLe page in Word,
when everybody was on vacaLion Lhis summer while l was
working aL Lhe library in Sammamish. l can'L remember
how many Limes l've said Lo my íriends, "No, l won'L be
able Lo be Lhere. l need Lo work on my book!" 8uL iL's an
awesome experience Lo wriLe a book, everybody who has
Lhe chance should Lake Lhe challenge!
x Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
X1146480A02.indd 10 10/19/05 2:21:57 PM
Chapter #. Chapter TitIe xi
WiLh LhaL said, l íirsL need Lo Lhank my lovely íamily. My
kids (Laura, íí, and AnLoine, 9) and my wiíe, Hélène, were
so greaL and PA1lFN1. How many Limes l heard Lhem say,
"Daddy, iL's movie nighL.oh yeah, iL's Lrue, you're work-
ing on your book again!" 8uL aL Lhe same Lime, Lhey were
respecLing Lhe space l needed and Lhe Lime alone!!! ¥ou
guys are greaL and l love you!
l have Lo Lhank all Lhe people aL MS Learning and Lhe
publishing Leam. l would especially like Lo Lhank 8en íor
helping me geL in Lhe wriLing world, Sandra íor her consLanL
moLivaLion, help, suggesLions, and also íor helping me Lo go
Lhrough all Lhe hurdles oí wriLing a book, and Megan íor all
your help geLLing Lhe job done and a real, Langible producL.
¥ou guys have my respecL íor working day in, day ouL in Lhe
crazy world oí publishing.
l would also like Lo Lhank all Lhe people in Lhe v8, C#,
Windows Forms, MSDN and seLup Leams who helped me
by answering all my quesLions in a dynamic and consLanLly
changing producL liíecycle. l would like Lo Lhank more
speciíically Dan Fernandez, Joe 8inder, 8rian Keller, 8rian
Johnson, Hong Cao, Jay Roxe, KaviLha Radhakrishnan, KenL
Sharkey, Lisa Feigenbaum, Shamez Rajan, SLeve Lasker, Aaron
SLebner, and Habib Heydarian.
A special Lhanks Lo Jeíí Prosise íor being such a good
wriLer, an awesome Lrainer, and íor wriLing a blog abouL Lhe
wriLing process. ¥ou were Lhe spark LhaL igniLed my ílame Lo
wriLe a Lechnical book.
1hanks also Lo my colleagues írom Lhe DDCPX Leam íor
always giving me good words oí encouragemenL, Lo 8oris
Feldman íor sharing his experiences as a wriLer and, íinally,
Lhanks Lo my good íriend, John Cross, íor his consLrucLive
íeedback.
1hanks Lo my good íriends here in Lhe PugeL Sound area
íor Lhe kind words oí encouragemenL and Lo my íamily and
íriends in Canada íor undersLanding why l'm noL calling or
giving any news. Sorry, Mom and Dad!!!
And Lhanks Lo my good neighbors and íriends, Mike and
FlizabeLh, íor Lheir consLanL encouragemenL and íor leLLing
me use Lheir dog's name, Molly, in my OOP inLroducLion.
1hanks Lo everybody l mighL have íorgoLLen!
PaLrice Pelland
SepLember 2005
Sammamish, WA
X1146480A02.indd 11 10/19/05 2:22:05 PM
X1146480A02.indd 12 10/19/05 2:22:06 PM
l
Whqt ls .NLT?, 2
Whqt ls Visuql 8qsic
2005?, 4
Whqt ls Visuql 8qsic 2005
Lxpress Ldition?, 9
6[TcgXe$
lntroducing Microsoft
®

Visuql 8qsic
®
2005
Lxpress Ldition
&
So maybe you've decided you wanL Lo Lry programming and you íound
yourselí wiLh Lhis book. Well, ií LhaL's Lhe case, you've come Lo Lhe righL
place. 1his book is all abouL inLroducing you Lo Lhe arL, science, and joys
oí creaLing soíLware íor MicrosoíL Windows®, yes, Lhe same MicrosoíL
Windows you probably use every day. 1hroughouL Lhe book, l'll show you
how Lo build applicaLions LhaL are very similar Lo many oí Lhe applicaLions
you use on a regular basis, such as your lnLerneL browser, your word proces-
sor, your e-mail soíLware, and your personal íinance applicaLion. ¥ou're
probably wondering how you could possibly do Lhis wiLh no programming
experience. 8y Lhe Lime you íinish Lhis book, you'll believe iL. Don'L worry.
We'll have a blasL, and because you'll acLually be building Lhe applicaLions as
you íollow along wiLh each exercise, you'll see íor yourselí jusL how easy iL
can be.
C01622132.indd 1 10/18/05 6:21:43 PM
2 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
what ls .N£T?
;^\jgZ&"&
Fb`XXkT`c_XfbYj[XeXlbh`\Z[g[TiXVb`XTVebffT
eXYXeXaVXgb!A8G
So now, whaL is LhaL .N£T Lhing LhaL everybody is Lalking abouL¯ Maybe you've seen iL
somewhere online or have come across Lhe Lerm in Lhe jobs secLion in your Sunday newspa-
per. For insLance, Lhe Lerm mighL have appeared when you were logging on Lo HoLmail® or
in an online ad where a company is looking íor a developer wiLh .NF1 skills. Look aL
Figure í-í íor some examples oí where you mighL have come across a reíerence Lo .NF1.
1he Lerm #C:I by iLselí does noL mean much. ¥ou could ask í0 diííerenL people in Lhe
indusLry, and you would geL í0 diííerenL answers. 1he Lerm is widely used and wiLh a loL oí
diííerenL meanings. ln íacL, .NF1 has been used wiLh a loL oí markeL hype aLLached Lo iL, a liL-
Lle biL like Lhe Lerm BE(. So in realiLy, when you hear or read #C:I, you really should be
Lhinking abouL Lhe .NF1 Framework.
Here is a íormal deíiniLion oí Lhe .NF1 Framework.
I]Z#C:I;gVbZldg`^hVeaVi[dgbi]ViVaadlhndjidYZkZadehd[ilVgZVeea^XVi^dch
VcYa^WgVg^ZhXVaaZYÆbVcV\ZYVeea^XVi^dchÇ0^iegdk^YZhndjl^i]i]ZXdbe^aZgVcYiddahid
WZVWaZidWj^aY!YZWj\!VcYZmZXjiZbVcV\ZYVeea^XVi^dch#
For our purposes, you could say .NF1 is Lhe plaLíorm LhaL gives you everyLhing you need
Lo develop and run managed applicaLions LhaL run on Windows.
H\YD`UhZcfagG8?hYUa]g`cc_]b[ZcfUghfcb[XYjY`cdYfhckcf_cbcif]bhYfbU`
Hcc`ghYUa"H\YHcc`ghYUadfcXiWYghcc`gUbXKYVg]hYgh\UhhfUW_K]b:L5D=
XYjY`cdaYbhfYdcfhaYhf]WgcbcifG8?gUbXhfUW_h\YhYbgcZh\cigUbXgcZZ]`Yg
h\UhUfYgiVa]hhYXhccifG8?gZcfK]b:Lh\Y"B9H:fUaYkcf_UbXh\YA]WfcgcZh
K]bXckgcdYfUh]b[gmghYag"
C01622132.indd 2 10/18/05 6:21:44 PM
Chapter l. lntroducing Microsoft
®
VisuaI Basic
®
2005 £xpress £dition 3
We say LhaL applicaLions are managed because Lheir execuLion is managed by Lhe
.NF1 Framework. ln íacL, Lhe .NF1 Framework is bVcV\^c\Lhe execuLion by providing
a conLrolled runLime environmenL oííering a wide varieLy oí services like loading your
applicaLions, managing Lhe memory, and íinally moniLoring and mainLaining Lhe securiLy
and inLegriLy while Lhe applicaLion is execuLed. 8eíore .NF1 (and Java), applicaLions were
unmanaged because Lhey were noL execuLed by a conLrolled runLime environmenL. No
oLher componenL oí Lhe sysLem provided Lhe services .NF1 oííers. 1he applicaLions had Lo
manage Lheir own services, which someLimes led Lo erroneous code, securiLy holes, and daLa
corrupLion. 8ecause oí Lhese problems, applicaLions were Lough Lo mainLain and debug.
1he .NF1 Framework provides you wiLh a wide varieLy oí Lools such as compiIers,
debuggers, programming Ianguages, an execution engine (named CLR - Common
Language RunLime), developer Lools, and a large number oí predeíined "building blocks"
libraries. 1hose libraries are named lCL {lramework CIass Libraries). ¥ou can Lhink oí each
.NF1 componenL as a building block in a house, as illusLraLed in Lhis image.
l won'L puL you Lo sleep wiLh all Lhe
deíiniLions íor each block oí Lhis house, because
we're going Lo use or Lalk abouL mosL oí Lhem
in our projecLs, l'll simply inLroduce Lhe blocks
as appropriaLe. JusL consider Lhis illusLraLion and
come back Lo iL as needed.
1wo noLes abouL Lhis special house are
worLh menLioning.
FirsL, look aL Lhe beige componenL on Lhe
righL side oí Lhe house. lL is noL parL oí Lhe .NF1
Framework, buL iL Louches Lhe .NF1 Framework
aL all levels. 1he doorknob on Lhis componenL
indicaLes LhaL Lhrough Lhis applicaLion, you can
develop applicaLions LhaL will allow you Louch
all Lhe componenLs oí Lhe .NF1 Framework.
Throughout this book, l'II be using
the terms [gVbZldg` and #C:I
;gVbZldg` as synonyms.
ABG 8
lt's not necessary to have Microsoft VisuaI Studio
®
to
deveIop .N£T appIications, but using it offers many
advantages.
< @C BEG4AG
C01622132.indd 3 10/18/05 6:21:48 PM
4 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
Second, noLice LhaL Common Language RunLime (CLR) is Lhe primary parL oí Lhe house's
íoundaLion. lL's a crucial parL oí Lhe íoundaLion because iL's Lhe engine LhaL loads and
manages Lhe execuLion oí source code. All oLher services you need Lo develop applicaLions
are on Lop oí Lhe CLR.
what ls VisuaI Basic 2005?
visual 8asic 2005 is one oí Lhe available programming languages LhaL LargeL Lhe .NF1
Framework. Like any spoken/wriLLen language, visual 8asic has synLax rules and a series oí
valid words you can use Lo creaLe your applicaLions. visual 8asic is a popular choice íor
beginners because some people íind Lhe synLax simpler Lhan Lhe synLax oí many oLher pro-
gramming languages. lí you have already used an older version oí visual 8asic (such as
visual 8asic 4.x, 5.x, or visual 8asic 6.0) Lo program, you'll íind some íamiliar consLrucLs and
a íamiliar user inLeríace (Ul) in visual 8asic 2005.
ls VisuaI Basic 2005 an Object-Oriented Programming {OOP) Language?
visual 8asic 2005 is a íully íledged objecL-orienLed programming language. LeL's Lalk abouL
whaL Lhis means.
ObjecL-orienLed programming (OOP) is a programming sLyle (or programming
paradigm). 1here are oLher programming paradigms, such as íuncLional or procedural
programming. Languages like C, ForLran, Pascal, and previous versions oí visual 8asic are all
programming paradigms. 8uL Lhese paradigms íocus more on Lhe acLions while OOP íocuses
more on Lhe daLa iLselí.
ApplicaLions LhaL use Lhe OOP paradigm are developed using OOP languages (OOPL).
1he íirsL OOPL were inLroduced in Lhe í960s, buL Lhey really became popular in Lhe laLe
í970s. 1hey are widely used Loday because mosL people agree LhaL Lhey're easy Lo learn,
use, debug, and mainLain. For insLance, OOPL easily represenL real-world objecLs. visual 8asic
2005 is an OOP language as are C#, C--, Java, Small1alk, and Lisp.
with C++ you can deveIop pro-
ceduraI appIications, pure object-
oriented appIications, or a mix of
both.
@BE8 < A9 B
C01622132.indd 4 10/18/05 6:21:48 PM
Chapter l. lntroducing Microsoft
®
VisuaI Basic
®
2005 £xpress £dition 5
Programmers use OOP Lo wriLe programs LhaL represenL Lhe decomposiLion oí real-
world problems inLo modules. 1hose modules represenL real-world objecLs and are named
classes or Lypes. ¥ou can Lhink oí an OOP program as a collecLion oí objecLs inLeracLing wiLh
each oLher. Using OOP, a programmer deíines new Lypes Lo represenL real-world objecLs,
such as a plane, a person, a cusLomer, a dog, or a car. 1hose Lypes or classes creaLe objecLs
or insLances. An objecL is a uniL LhaL represenLs one insLance oí Lhe real world. lL's a selí-
conLained uniL because iL includes all Lhe daLa and íuncLionaliLy associaLed wiLh LhaL objecL.
1his means LhaL each objecL creaLed in an applicaLion conLains all Lhe iníormaLion LhaL
characLerizes iL (data members) and all Lhe acLions (methods) LhaL can access or modiíy
LhaL iníormaLion.
Here is a simple example in visual 8asic 2005 LhaL deíines a person's class.
1 Pub]1c C]ass Pe¡son
2 `Dafa membe¡s
3 Pub]1c Name As 5f¡1ng
4 Pub]1c Add¡ess As 5f¡1ng
S Pub]1c C1fy As 5f¡1ng
6 Pub]1c 5fafe As 5f¡1ng
7 Pub]1c ZlP As 5f¡1ng
ß Pub]1c Counf¡y As 5f¡1ng
9
10 ` Mefhods
11 Ove¡¡1dab]e 5ub D1sp]ay1}
12 Conso]e.W¡1feL1ne1Name}
13 Conso]e.W¡1feL1ne1Add¡ess}
14 Conso]e.W¡1feL1ne1C1fy}
1S Conso]e.W¡1feL1ne15fafe}
16 Conso]e.W¡1feL1ne1ZlP}
17 Conso]e.W¡1feL1ne1Counf¡y}
1ß Lnd 5ub
19 Lnd C]ass
1his class includes public daLa members and a display meLhod Lo prinL Lhe objecL's
conLenL Lo Lhe console. 1he sub meLhod is by deíaulL public. 1he overridable keyword means
LhaL a new class derived írom Lhis class will be able Lo wriLe iLs own implemenLaLion oí Lhe
display meLhod.
C01622132.indd 5 10/18/05 6:21:49 PM
6 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
LeL's use a diííerenL example Lo go over Lhese concepLs. My dog, Chopin, is an insLance
oí Lhe class Dog and Lhe class Dog is a subclass oí Lhe Animal class. 8ecause Chopin is a
Dog, he has some behaviors and daLa LhaL are proper íor a dog. 8uL because Dog is also an
Animal, Chopin also inheriLs some daLa and behaviors írom Lhe Animal class.
1his means LhaL Lhe insLance Chopin oí Lhe Dog class has daLa members LhaL characLerize
him and meLhods LhaL l can call on LhaL liLLle íurry ball. For example, here is Lhe insLance
iníormaLion íor Lhe Chopin objecL.
DaLa
N 8reed. He's a MalLese.
N Cender. He's male.
N WeighL. His weighL is 5.5 pounds.
N Color. He's whiLe.
N Name. His name is Chopin Chabispel.
N Age. He's í.5 years old.
AcLions
N He speaks (barks).
N He eaLs.
N He moves.
N He sleeps.
All oí Lhese daLa iLems (breed, gender, weighL, color, name, and age) and acLions (speak,
eaL, move, and sleep) characLerize him, buL Lhey can also characLerize any oLher dog, like my
neighbor's dog, Molly. And ií you Lhink abouL iL, Lhose iLems can characLerize any animal.
1his means LhaL Lhe class Dog inherits daLa members and meLhods írom Lhe class Animal.
LeL's say you wanL Lo develop an applicaLion íor a veLerinary clinic. 1o cover Lhe caLs
LhaL come Lo your clinic, all you musL do is creaLe a CaL class LhaL also inheriLs írom Lhe
class Animal. 1hen each class (CaL or Dog) could override Lhe íuncLionaliLy írom Lhe
Animal class as needed. For insLance, íor Lhe CaL class Lhe speak meLhod would be "meows"
insLead oí barks.
C01622132.indd 6 10/18/05 6:21:49 PM
Chapter l. lntroducing Microsoft
®
VisuaI Basic
®
2005 £xpress £dition 7
LeL's look aL Lhe Person class example again. 1his Lime, an Fmployee class LhaL derives
írom Lhe Person class is added. 1he Fmployee class derives írom Lhe Person class using Lhe
keyword lnheritsíollowed by Lhe Person elemenL. 1he keyword Overrides changes Lhe
implemenLaLion oí Lhe DispIay{) meLhod.
20 Pub]1c C]ass Lmp]oyee
21 lnhe¡1fs Pe¡son
22
23 Pub]1c Leve] As lnfege¡
24 Pub]1c 5a]a¡y As lnfege¡
2S
26 Ove¡¡1des 5ub D1sp]ay1}
27 Conso]e.W¡1feL1ne1Name + ¨ 1s af ]eve] ¨ + Leve].1o5f¡1ng1} +
¨ and has a sa]a¡y of : ¨ + 5a]a¡y.1o5f¡1ng1} + ¨$¨}
2ß Conso]e.W¡1feL1ne1¨h1s add¡ess 1s:¨}
29 Conso]e.W¡1feL1ne1Add¡ess}
30 Conso]e.W¡1feL1ne1C1fy + ¨.¨ + 5fafe + ¨ ¨ + ZlP}
31 Conso]e.W¡1feL1ne1Counf¡y}
32 Lnd 5ub
33 Lnd C]ass
ln Lhis case, Lhe Fmployee class inheriLs írom Lhe Person class and Lhereíore geLs all
Lhe daLa íields írom LhaL base class. Fmployee class doesn'L have Lo redeíine all íields in
iLs deíiniLion because iL geLs Lhem auLomaLically írom Person. So, íor Lhe Fmployee class,
you musL speciíy only whaL is diííerenL írom an insLance oí Lhe Person class. For example,
an insLance oí Lhe Fmployee class would have level and salary whereas noL all insLances oí
Lhe Person class would. Plus, Lhe Display meLhod íor Fmployee could add level and salary
iníormaLion Lo Lhe displayed message when called.
1his was jusL a brieí inLroducLion Lo OOP and some oí iLs concepLs. visual 8asic 2005
supporLs all oí Lhese concepLs and many more. 1hroughouL Lhis book you'll see more OOP
concepLs, and when you do, l'll highlighL Lhem in a reader aid iníormaLion box.
Here's Lhe compleLe lisLing used in Lhis secLion wiLh Lhe addiLion oí anoLher class. Lhe
CusLomer class.
34 lmpo¡fs 5ysfem
3S
36 Pub]1c C]ass Pe¡son
37 `Dafa membe¡s
ln .N£T, aII cIass uItimateIy derives
from the Object cIass, even when
it is not specified.
F 8 8 4? F B
ln this book, you'II notice that
some code Iistings incIude Iine
numbers. lf a Iine does not incIude
a number, it indicates that the code
is a continuation from the previous
Iine. Some code Iines can get rather
Iong and must be wrapped to be
dispIayed on the printed page. lf
you need to type in the code in
VisuaI Basic, be sure to put contin-
ued Iines on a singIe Iine.
G < C
C01622132.indd 7 10/18/05 6:21:50 PM
8 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
3ß Pub]1c Name As 5f¡1ng
39 Pub]1c Add¡ess As 5f¡1ng
40 Pub]1c C1fy As 5f¡1ng
41 Pub]1c 5fafe As 5f¡1ng
42 Pub]1c ZlP As 5f¡1ng
43 Pub]1c Counf¡y As 5f¡1ng
44
4S ` Mefhods
46 Ove¡¡1dab]e 5ub D1sp]ay1}
47 Conso]e.W¡1feL1ne1Name}
4ß Conso]e.W¡1feL1ne1Add¡ess}
49 Conso]e.W¡1feL1ne1C1fy}
S0 Conso]e.W¡1feL1ne15fafe}
S1 Conso]e.W¡1feL1ne1ZlP}
S2 Conso]e.W¡1feL1ne1Counf¡y}
S3 Lnd 5ub
S4 Lnd C]ass
SS
S6 Pub]1c C]ass Cusfome¡
S7 lnhe¡1fs Pe¡son

S9 Pub]1c lD As lnfege¡
60 Pub]1c lsPa¡fne¡ As 8oo]ean
61
62 Ove¡¡1des 5ub D1sp]ay1}
63 D1m pa¡fne¡Message As 5f¡1ng
64
6S lf lsPa¡fne¡ 1hen
66 pa¡fne¡Message = ¨ 1s a pa¡fne¡¨
67 L]se
6ß pa¡fne¡Message = ¨ 1s nof a pa¡fne¡¨
69 Lnd lf
70
71 Conso]e.W¡1feL1ne1¨Cusfome¡ lD: ¨ + lD.1o5f¡1ng1}}
72 Conso]e.W¡1feL1ne1Name + pa¡fne¡Message}
73 Conso]e.W¡1feL1ne1Add¡ess}
74 Conso]e.W¡1feL1ne1C1fy + ¨.¨ + 5fafe + ¨ ¨ + ZlP}
7S Conso]e.W¡1feL1ne1Counf¡y}
76 Lnd 5ub
77 Lnd C]ass

79 Pub]1c C]ass Lmp]oyee
C01622132.indd 8 10/18/05 6:21:50 PM
Chapter l. lntroducing Microsoft
®
VisuaI Basic
®
2005 £xpress £dition 9
ß0 lnhe¡1fs Pe¡son
ß1
ß2 Pub]1c Leve] As lnfege¡
ß3 Pub]1c 5a]a¡y As lnfege¡
ß4
ßS Ove¡¡1des 5ub D1sp]ay1}
ß6 Conso]e.W¡1feL1ne1Name + ¨ 1s af ]eve] ¨ + Leve].1o5f¡1ng1} + ¨ and has a
sa]a¡y of : ¨ + 5a]a¡y.1o5f¡1ng1} + ¨$¨}
ß7 Conso]e.W¡1feL1ne1¨h1s add¡ess 1s:¨}
ßß Conso]e.W¡1feL1ne1Add¡ess}
ß9 Conso]e.W¡1feL1ne1C1fy + ¨.¨ + 5fafe + ¨ ¨ + ZlP}
90 Conso]e.W¡1feL1ne1Counf¡y}
91 Lnd 5ub
92 Lnd C]ass
1his is a simple case, buL iL illusLraLes some oí Lhe basic concepLs oí OOP.
what ls VisuaI Basic 2005 £xpress £dition?
visual 8asic 2005 Fxpress FdiLion is Lhe Lool we will use LhroughouL Lhis book Lo learn how
Lo develop applicaLions running on Windows.
1he Fxpress FdiLions oí visual SLudio 2005 were designed Lo íocus on producLiviLy.
As wiLh Lheir high-end version counLerparLs, Lhe Fxpress FdiLions oí visual SLudio 2005 are
also whaL we call Rapid ApplicaLion DevelopmenL Lools (RAD Lools) because Lheir philosophy
is geared Loward producLiviLy. 1hese new versions oí visual SLudio are easy Lo use, easy Lo
learn, and sLreamlined because, alLhough Lhey conLain mosLly Lhe same componenLs, Lhey
lack Lhe íull breadLh oí íeaLures íound in Lhe higher-end versions oí visual SLudio. MosL íea-
Lures and componenLs in Lhe Fxpress FdiLions were simpliíied Lo make Lhe learning curve less
sLeep and Lo íiL Lhe needs oí Lhe nonproíessional developer.
1he visual SLudio 2005 Fxpress FdiLion íamily is designed wiLh beginning program-
mers in mind-people like you who are curious abouL programming and who are looking
íor an easy way Lo build Windows applicaLions while learning how Lo program. visual 8asic
2005 Fxpress FdiLion is Lhe ideal Lool Lo use Lo rapidly develop applicaLions íor Lopics you
really love or íor hobbies you enjoy. lL can also be used Lo help ease your day-Lo-day job or
school Lasks. MosL imporLanLly, you can have íun wiLh Lhe Lool while you're learning Lo program.
C01622132.indd 9 10/18/05 6:21:51 PM
l0 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
what Kinds of AppIications Can You BuiId with VisuaI Basic 2005
£xpress £dition?
WiLh Lhis version oí visual SLudio 2005, you'll be able Lo creaLe Lhe íollowing Lypes oí appli-
caLions.
N windows AppIications ApplicaLions LhaL have a graphical inLeríace wiLh buLLons, win-
dows, menus, Loolbars, and so on, as in MicrosoíL Word or lnLerneL Fxplorer.
N ConsoIe AppIications ApplicaLions LhaL have no graphical inLeríace and simply use LexL
Lo communicaLe wiLh Lhe user. (1ypically, Lhese are a command window or DOS window.)
N keusabIe Components or CIass Libraries A group oí Lools creaLed Lo help build oLher
applicaLions.
WhaL you won'L be able Lo build are Web siLes and Web services. 1o creaLe any Lype oí
Web applicaLion, you will need Lo geL MicrosoíL visual Web Developer" 2005 Fxpress FdiLion.
what Are the Key leatures You Need to Know About?
1he lisL below, alLhough noL compleLe, provides Lhe essenLial íeaLures oí visual 8asic 2005
Fxpress FdiLion. AL Lhis poinL, don'L worry ií you don'L undersLand every single íeaLure lisLed
below. l presenL Lhe íeaLures in Lhe lisL because you'll come across all oí Lhem in some way in
Lhe íun samples LhaL you will be creaLing.
MosL oí Lhe íeaLures lisLed here emphasize Lhe RADphilosophy. AlLhough Lhe idea
is Lo give you an overview oí Lhe inLeresLing íeaLures LhaL can make your liíe easier, Lhe
íeaLure names alone are noL suííicienL Lo undersLand whaL Lhey mean. So l include a brieí
descripLion giving you Lhe essenLials and how Lhey should help you Lo develop applicaLions.
N BuiIt-in Starter Kits Fully developed applicaLions wiLh besL pracLices and examples Lo
íollow. 1hese applicaLions will give you anoLher example Lo base your learning on. 1hey
will be a good complemenL Lo whaL we are doing wiLh Lhis book.
N Beginner's Targeted Documentation and TutoriaIs A íasL and easy way Lo geL Lhe
iníormaLion. 1hey also provide samples.
we wiII Iook into the detaiIs of
what types of appIications faII into
these categories in Chapter 2.
ABG 8
C01622132.indd 10 10/18/05 6:21:51 PM
Chapter l. lntroducing Microsoft
®
VisuaI Basic
®
2005 £xpress £dition ll
N lnteIIiSense
®
Provides real-liíe synLax suggesLions and even íinishes your Lyping íor
you.
N Code Snippets Provide code íor more Lhan 200 programming Lasks Lo help you com-
pleLe many common Lasks auLomaLically. Code snippeLs show Lhe recommended way oí
doing Lhings. 1hey are direcLly inLegraLed in Lhe developmenL environmenL, and Lhey are
exLensible, LhaL is, over Lime MicrosoíL will conLinue Lo supply new code snippeLs and
members oí online communiLies will conLribuLe Lheir snippeLs as well. Code snippeL
exLensibiliLy seems Lo be a really nice íeaLure LhaL will help people share useíul Lhings in
Lhe online communiLies.
N The Bn Construct 1his new visual 8asic íeaLure provides simple access Lo popular .NF1
Framework classes and common Lasks. lL enables you Lo períorm Lasks wiLhouL knowing
all Lhe íramework inLernals. 1hese Lasks are nicely wrapped and provide you wiLh an easy
and clean way oí geLLing Lhings done. AlLhough similar Lo code snippeLs, Lhe My
ConsLrucL elemenLs have Lheir source code hidden and wrapped in one line oí code.
N Data-enabIed AppIications 1hese applicaLions allow you Lo connecL Lo MicrosoíL SOL
Server" 2005 Fxpress FdiLion and add daLabases and code Lo access Lhese daLa in your
applicaLions.
N windows lorm Designer WiLh Lhese new Lools, you can easily design your Windows
applicaLion, including íeaLures such as snap lines LhaL make sure your conLrols are
aligned in your íorm and auLocorrecL Lo give you real-Lime compiler íeedback. ¥ou won'L
have Lo compile your code Lo know ií you have errors, visual 8asic 2005 will give Lhem
Lo you as you Lype and even give you poLenLial íixes.
N XML web Services 1his producL provides easy-Lo-use Lools and wizards LhaL will help
you connecL Lo published XML-based Web Services and uLilize Lheir íuncLionaliLies.
N New windows lorm ControIs 1hese comprise an impressive lisL oí conLrols-a greaLer
selecLion Lhan in any previous version oí visual 8asic. 1hey will help you creaLe user
inLeríaces LhaL have a proíessional look and íeel.
C01622132.indd 11 10/18/05 6:21:51 PM
l2 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
N Smart Tags MosL Windows Form conLrols LhaL come wiLh Lhe producL include SmarL
1ags. As in many MicrosoíL Oííice applicaLions, a SmarL 1ag is represenLed by a liLLle black
Lriangle or an icon and a liLLle black Lriangle aLLached Lo a conLrol. A SmarL 1ag gives you
access Lo Lhe mosL common acLions you can períorm on a conLrol.
N CIick-once DepIoyment 1his íeaLures allows you Lo easily publish your applicaLions on
Lhe lnLerneL, on a local area neLwork (LAN), a neLwork share, or on a CD. lL also simpliíies
publishing updaLes.
N £dit and Continue While you are debugging your applicaLion, FdiL and ConLinue leLs
you modiíy Lhe code, move back and íorLh in Lhe debugger, re-execuLe code, add íunc-
LionaliLy, or íix bugs on Lhe íly wiLhouL sLopping program execuLion.
N Debugger VisuaIizers While you are debugging your applicaLion, Lhe visualizers give
you an easy way Lo geL readable represenLaLions oí your applicaLion daLa. 1hey give you
a human-readable represenLaLion oí Lhe sLored daLa, even íor more complex Lypes íound
in ADO.NF1 or FxLensible Markup Language (XML).
N Community Access and Start Page WiLh Lhese íeaLures, you can access addiLional
iníormaLion írom online communiLies and írom diííerenL sources oí online help, including
diverse RSS (Rich SiLe Summary or Really Simple SyndicaLion) Feeds. (RSS is a íamily oí
XML íile íormaLs, iL is widely used by Lhe weblog communiLy and news Web siLes.)
N SimpIified DeveIopment £nvironment FveryLhing in Lhe developmenL environmenL
was creaLed so LhaL you can easily access key íuncLionaliLies, Lools, and objecLs.
As you can see, visual 8asic 2005 Fxpress FdiLion includes many nice íeaLures Lo help
Lhe new programmer develop applicaLions in a íasL and íun way. 1hese íeaLures will provide
guidance even when you're noL necessarily sure whaL synLax or componenLs Lo use and will
greaLly expediLe your learning oí Lhe producL.
C01622132.indd 12 10/18/05 6:21:52 PM
Chapter l. lntroducing Microsoft
®
VisuaI Basic
®
2005 £xpress £dition l3
ln Summary...
¥ou now know LhaL .NF1 is a íramework composed oí compilers, Lools, languages, debug-
gers, and an execuLion engine. 1he CLR, or Common Language RunLime, is LhaL execuLion
engine, and iL is responsible íor loading and execuLing managed applicaLions. ln essence,
.NF1 is like a house wiLh Lhe CLR as Lhe íoundaLion and all oLher services builL on Lop oí iL.
¥ou also learned LhaL visual 8asic 2005 is an objecL-orienLed programming language
LhaL has a simpler synLax Lhan oLher modern programming languages. ¥ou also sLarLed Lo
learn whaL objecL-orienLed programming is and sLarLed Lo learn Lhe basics oí OOP in visual
8asic 2005.
1his chapLer has given you Lhe opporLuniLy Lo learn Lhe íeaLures oí visual 8asic 2005
Fxpress FdiLion and how you can creaLe Lhree diííerenL Lypes oí applicaLions LhaL maLch your
hobbies, job, or school needs. ln Lhe nexL chapLer, you'll learn how Lo insLall visual 8asic
2005 Fxpress FdiLion.
C01622132.indd 13 10/18/05 6:21:52 PM
C01622132.indd 14 10/18/05 6:21:52 PM
l5
CeXcTe\aZgb<afgT__
I\fhT_5Tf\V%##(8kceXff
8W\g\ba$)
<afgT__\aZI\fhT_5Tf\V
%##(8kceXff8W\g\ba$*
6[TcgXe%
<afgT__\aZI\fhT_
5Tf\V%##(
8kceXff8W\g\ba
'
ln Lhis chapLer, you'll insLall Lhe producL and sLarL geLLing Lo know whaL
componenLs are included wiLh iL. l'll guide you Lhrough all Lhe sLeps oí Lhis
insLallaLion so LhaL you can be ready Lo sLarL building applicaLions using
MicrosoíL
®
visual 8asic
®
2005 Fxpress FdiLion righL away. l will Lalk abouL
some common insLallaLion scenarios, give you some Lips, and l will also
cover whaL Lo do ií Lhe unexpecLed happens.
1he insLallaLion process is easy and sLraighLíorward, íollowing in Lhe spiriL
oí Lhe Fxpress producLs.
C02622132.indd 15 10/18/05 6:28:35 PM
l6 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
Preparing to lnstaII VisuaI Basic 2005 £xpress £dition
1here are a couple oí opLions íor insLalling visual 8asic 2005, parLicularly ií you've had previ-
ous versions insLalled beíore or ií you insLalled an early version oí Lhe producL. 8eíore you
sLarL Lhe insLallaLion, make sure your compuLer meeLs Lhe soíLware and hardware recom-
mendaLions. ¥ou can review Lhe lnLroducLion oí Lhis book íor all necessary iníormaLion. ¥ou
will also wanL Lo ensure LhaL your compuLer has been updaLed wiLh Lhe laLesL updaLes írom
Windows UpdaLes (httµ.//winJcwsµµJcte.micrcscft.ccm} or MicrosoíL UpdaLes (httµ.//µµJcte.
micrcscft.ccm). 1his will coníirm LhaL your machine has all Lhe laLesL securiLy updaLes along
wiLh some insLallaLion prerequisiLes beíore sLarLing Lhe acLual producL insLallaLion.
lí you have an anLivirus or an anLispyware applicaLion insLalled and running, your
anLispyware program mighL prompL you Lo allow cerLain seLup Lasks Lo proceed. For
insLance, wiLh Lhe laLesL MicrosoíL Windows
®
AnLiSpyware 8eLa (httµ.//www.micrcscft.ccm/
cthcme/secµritv/sµvwcre/scftwcre/Jefcµlt.msµx), l was asked Lwo Limes Lo allow cerLain Lasks
Lo proceed and a íew oLher Limes Lhe anLispyware producL recognized Lhe source and simply
menLioned iL and conLinued. 1his was Lhe experience l had wiLh Lhe MicrosoíL producL. lí
you are using anoLher anLispyware applicaLion, your experience mighL vary slighLly buL will
bear some similariLies Lo Lhis process. 1he anLivirus and anLispyware producLs are giving you
an opporLuniLy Lo coníirm Lhe origin oí Lhe producL you're abouL Lo insLall. When you're sure
iL's írom MicrosoíL, allow Lhe seLup applicaLion Lo conLinue iLs job by allowing Lhe acLion.
During Lhe insLallaLion, ií someLhing goes wrong, you're probably noL Lhe only person
Lo encounLer Lhis problem. 1he íirsL Lhing Lo do is Lo look aL Lhe laLesL Readme iníormaLion
mainLained by Lhe SeLup Leam on MSDN and íollow Lhe sLeps provided Lo solve insLallaLion
problems. Here are Lwo links Lo Lhe MSDN
®
resources.
N Fxpress Online Readme. httµ.//çc.micrcscft.ccm/fwlinl/?linllJ=S1J22
N Fxpress Online Known lssues. httµ.//çc.micrcscft.ccm/fwlinl/?linllJ=S1J2S
Side-by-Side lnstaIIation lí you have a previous version oí MicrosoíL visual SLudio
®
,
say visual SLudio 2002 or visual SLudio 2008, insLalling visual 8asic 2005 Fxpress FdiLion, or
any visual SLudio 2005 producL, will be sLraighLíorward. 1his is considered a side-by-side
execuLion, and you can go sLraighL Lo Lhe secLion on insLalling Lhe soíLware.
C02622132.indd 16 10/18/05 6:28:36 PM
Chapter 2. lnstaIIing VisuaI Basic 2005 £xpress £dition l7
Previous Versions of VisuaI Basic 2005 £xpress £dition lí you have a pre-release-
especially a pre-8eLa2 version oí visual 8asic 2005 Fxpress FdiLion or any version oí visual
SLudio 2005 (C1P-CommuniLy 1echnology Preview or a 8eLa version)-vcµ mµst µninstcll
cll µre-relecse ccmµcnents befcre instcllinç this new versicn. 1he soíLware componenLs musL
be uninsLalled in a parLicular order íor your new insLallaLion Lo be successíul. 1he mosL up-
Lo-daLe iníormaLion íor uninsLalling any pre-release versions oí Lhe producL is available on
Lhe íollowing MSDN Help page. httµ.//çc.micrcscft.ccm/fwlinl/?linllJ=47062.
1he pre-release (pre-8eLa2) versions represenL a version oí Lhe íinal producL early in Lhe
developmenL cycle. 1hey are noL oííicially supporLed by MicrosoíL, so when you insLall early
beLa or C1P versions, you also undersLand LhaL Lhe producL is undergoing conLinual changes,
which ulLimaLely reílecLs in Lhe insLallaLion process.
When you uninsLall a pre-release version oí any soíLware, you mighL encounLer
problems. AL some poinL, you may have no choice buL Lo reíormaL your hard drive and
reinsLall your operaLing sysLem. 1his is noL an uncommon siLuaLion when you work wiLh pre-
release soíLware. 8uL Lhis siLuaLion has a soluLion. 8eíore beginning Lhe uninsLall procedure,
and as a precauLionary measure, you'll wanL Lo back up all your daLa beíore beginning. lí
possible, a good pracLice is Lo avoid insLalling any pre-release versions oí any producLs on
your main compuLer. Using a LesL machine (or virLual soíLware) will help you avoid losing
any imporLanL daLa and won'L slow your producLiviLy in Lhe evenL someLhing goes wrong. 1o
learn more abouL Lhe virLual soluLion LhaL MicrosoíL oííers, you can look aL MicrosoíL virLual
PC 2004 aL httµ.//www.micrcscft.ccm/winJcws/virtµclµc/Jefcµlt.msµx.
lnstaIIing VisuaI Basic 2005 £xpress £dition
Now LhaL we've addressed a loL oí poLenLial issues and now LhaL your machine is ready, you
can proceed wiLh Lhe insLallaLion. ¥ou should íind a CD or DvD aL Lhe end oí Lhis book LhaL
conLains a íull working ediLion oí Lhe producL. Simply inserL iL inLo any available CD or DvD
drive in your sysLem and íollow Lhe sLeps below.
PIease make sure you carefuIIy
read this section on the MSDN
HeIp page before starting the
uninstaII process!
6 4HG < BA
£ven though the pre-reIease ver-
sions of the software are not
officiaIIy supported by Microsoft,
you wiII find resources on the
Microsoft site to heIp you with
instaIIation. ln particuIar, you wiII
find information on how to unin-
staII {and in what order to unin-
staII) the products.
ABG 8
C02622132.indd 17 10/18/05 6:28:36 PM
l8 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
TO lNSTALL VlSUAL BASlC 2005 £XPk£SS £DlTlON
$
|¦ /J¦o·Jr |s erab|ed. ¦|e |rs¦a||a¦|or p·ocess s|oJ|d s¦a·¦ aJ¦ona¦|ca||y |¦ |¦ doesr'¦ s¦a·¦ aJ¦ona¦|ca||y
a¦¦e· a ¦e. secords. ¦o||o. ¦|ese s¦eps
a. C||c| ¦|e Start bJ¦¦or. ard ¦|er c||c| My Computer
b. ||g|¦c||c| ¦|e CD o· D\D d·|ve ¦|a¦ |as ¦|e p·odJc¦ ned|a ard se|ec¦ £xpIore
c. |r ¦|e ||s¦ o¦ ¦||es. |oca¦e ard doJb|ec||c| Setup.exe ¦o s¦a·¦ ¦|e |rs¦a||a¦|or \|.a·d
%
\|¦||r a ¦e. secords. yoJ s|oJ|d see ¦|a¦ ¦|e se¦Jp p·og·an |s copy|rg a|| recessa·y |rs¦a||a¦|or ¦||es
¦o a ¦enpo·a·y ¦o|de· as s|o.r |r ||gJ·e ´1
;^\jgZ'"&
6bcl\aZfXghcY\_Xf_bVT__lgbT
gX`cbeTelYb_WXe
;^\jgZ'"'
<a\g\T_\m\aZg[XfXghccebVXff
You'II have nothing to do but wait
at this point. The wait shouId not
be Iong-Iess than a minute in
most cases, depending on your
computer speed.
ABG 8
&
\|er ¦|e Se¦Jp p·og·an |s dore copy|rg ¦|e ¦||es. ¦|e se¦Jp app||ca¦|or |oads |r¦o neno·y \|||e
¦|e app||ca¦|or |s |oad|rg. yoJ'|| see ar |r|¦|a||.a¦|or p·og·ess ba· as s|o.r |r ||gJ·e ´´
C02622132.indd 18 10/18/05 6:28:37 PM
Chapter 2. lnstaIIing VisuaI Basic 2005 £xpress £dition l9
'
Ne.¦. yoJ'|| be g·ee¦ed by ¦|e \e|cone page
(||gJ·e ´`). .||c| p·ov|des sone |r¦o·na¦|or
aboJ¦ ¦|e p·odJc¦ ard ¦|e poss|b|||¦|es yoJ'|| |ave
.o·||rg .|¦| |¦ `oJ'|| a|so be ro¦|¦|ed |¦ yoJ·
nac||re does ro¦ nee¦ ary p·e·eoJ|s|¦es ¦o· |oad
|rg ¦|e so¦¦.a·e C||c| BYlh ¦o cor¦|rJe o· 7UbWY`
¦o e.|¦ ¦|e |rs¦a||a¦|or p·og·an
;^\jgZ'"(
JX_Vb`XcTZX
(
To cor¦|rJe ¦|e |rs¦a||a¦|or p·ocess. yoJ nJs¦
·ead ard accep¦ ¦|e ||cerse ag·eener¦
(||gJ·e ´4) ||ease ·ead |¦ ca·e¦J||y ¦o see .|a¦ yoJ
car ard car'¦ do .|¦| ¦||s p·odJc¦ \|er yoJ'·e
dore ard yoJ'·e ·eady ¦o accep¦ ¦|e ||cerse ag·ee
ner¦. c||c| ¦|e c|ec| bo.. ard ¦|er c||c| BYlh ¦o
cor¦|rJe
;^\jgZ'")
?\VXafXTZeXX`Xag
C02622132.indd 19 10/18/05 6:28:38 PM
20 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
)
T|e |rs¦a||a¦|or Cp¦|ors page appea·s. as
s|o.r |r ||gJ·e ´' Cr ¦||s page. be sJ·e ¦o
spec|¦y ¦|a¦ yoJ .ar¦ access ¦o ¦|e |e|p sys¦en
(\SDN |.p·ess ||b·a·y) ard ¦|e \|c·oso¦¦ SO|
Se·ve·" ´JJ' |.p·ess |d|¦|or
SO| Se·ve· ´JJ' |.p·ess |d|¦|or |s a ·e|a¦|ora| da¦a
base naragener¦ sys¦en (|D|\S) ¦|a¦ .||| erab|e
yoJ ¦o eas||y nar|pJ|a¦e da¦a |r yoJ· app||ca¦|or
T||s |s ar |npo·¦ar¦ s¦ep |o· e.anp|e. |¦ yoJ'·e c·e
a¦|rg ¦|e D\D co||ec¦|or naragener¦ app||ca¦|or.
.||c| |s ore o¦ ¦|e S¦a·¦e· ||¦s. a|| ¦|e da¦a ·e|a¦ed
¦o yoJ· D\D co||ec¦|or .||| reed ¦o be s¦o·ed |r a
da¦abase Js|rg SO| Se·ve· ´JJ' |.p·ess |d|¦|or
\|er yoJ'·e dore .|¦| yoJ· se|ec¦|ors. c||c|
BYlh ¦o cor¦|rJe
+
T|e Des¦|ra¦|or |o|de· page appea·s. as
s|o.r |r ||gJ·e ´b T||s page .||| as| yoJ
.|e·e ¦o |rs¦a|| ¦|e so¦¦.a·e or yoJ· conpJ¦e· |
·econnerd ¦|a¦ yoJ Jse ¦|e de¦aJ|¦ |oca¦|or
The onIy reason not to instaII the IocaI MSDN heIp or
SOL Server 2005 £xpress £dition is Iimited hard drive
space. Be sure you understand the consequences of your
seIections. lf you don't instaII MSDN £xpress Library,
you'II need access to the lnternet to get heIp from MSDN
OnIine. lf you don't instaII SOL Server 2005 £xpress, you
won't be abIe to create appIications that must access
other sources of data, such as Microsoft Access database
information, £xtensibIe Markup Language {XML) fiIes,
or other types of kDBMS information. ln addition, some
sampIe fiIes from this book won't work automaticaIIy,
and you'II have to perform some manipuIations or re-
instaIIations to get them to work.
ABG 8
;^\jgZ'"*
FXglbhe\afgT__T
g\babcg\baf[XeX
;^\jgZ'"+
7Xfg\aTg\ba9b_WXe
*
lf you choose to instaII the software in a foIder other
than the recommended defauIt, you might have prob-
Iems working with some of the paths and fiIes men-
tioned Iater in the book. lf you do instaII in a different
Iocation, rest assured l'II give you some cautionary
notes whenever you may run into probIems.
6 4HG < BA
C02622132.indd 20 10/18/05 6:28:40 PM
Chapter 2. lnstaIIing VisuaI Basic 2005 £xpress £dition 2l
C||c|=bghU`` ¦o s¦a·¦ ¦|e |rs¦a||a¦|or T|e |rs¦a|
|a¦|or p·og·ess ba· appea·s ard ¦|e |rs¦a||a¦|or
|s Jrde·.ay (see ||gJ·e ´¯)¦ T||s n|g|¦ be a good
¦|ne ¦o ge¦ sone¦||rg ¦o d·|r| becaJse |rs¦a||a¦|or
coJ|d ¦a|e sone ¦|ne
|e·e |s ¦|e ||s¦ o¦ conporer¦s ¦|a¦ .||| be |rs¦a||ed
Q H\Y"B9H:fUaYkcf_&"$T||s .as oJ· |oJse
|||Js¦·a¦ed |r C|ap¦e· 1
Q J]giU`6Ug]W&$$)9ldfYgg9X]h]cb T||s |s ¦|e
¦oo| |¦se|¦. .||c| .as ¦|e be|ge b|oc| ·ep·eser¦|rg
¦|e doo· o¦ oJ· |oJse |r C|ap¦e· 1
Q AG8B9ldfYgg@]VfUfm T||s .as desc·|bed ea·||e·
Q GE@GYfjYf&$$)9ldfYgg9X]h]cb T||s .as
desc·|bed ea·||e·
T|e Se¦Jp Conp|e¦e page appea·s (see ||gJ·e ´8).
ard yoJ a·e ro. ¦|r|s|ed .|¦| ¦|e |rs¦a||a¦|or No¦
¦oo pa|r¦J|. .as |¦¯ |e¦o·e yoJ c||c| ¦|e |.|¦ bJ¦¦or.
p|ease ·ead ¦|e ¦o||o.|rg ro¦es
On some computers, depending on the windows ver-
sion, when the .N£T lramework instaIIation is compIet-
ed, you might be asked to reboot your computer before
the instaIIation continues.
ABG 8
;^\jgZ'",
<afgT__Tg\baCebZeXff
;^\jgZ'"-
FXghc6b`c_XgX
The instaIIation time wiII vary based on your choices
on the previous page. On average, if you seIected both
MSDN Library and SOL Server 2005 £xpress £dition, the
instaIIation shouId be around l0 to 30 minutes depend-
ing on your computer's speed.
ABG 8
,
C02622132.indd 21 10/18/05 6:28:41 PM
22 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
Whenever you insLall a new applicaLion, iL's always a good habiL Lo go Lo MicrosoíL
UpdaLe (httµ.//µµJcte.micrcscft.ccm) or Windows UpdaLe (httµ.//winJcwsµµJcte.micrcscft.
ccm) Lo geL all Lhe high-prioriLy updaLes. Or you can click on Lhe Windows UpdaLe hyperlink
írom Lhe seLup applicaLion as shown in Figure 2-8. l preíer MicrosoíL UpdaLe because
you geL all Lhe updaLes you need íor all Lhe MicrosoíL soíLware already insLalled on your
hard drive. ¥ou'll geL your Windows, Oííice, and .NF1 Framework updaLes along wiLh your
hardware drivers updaLes, all in one sLop!
lL's also imporLanL Lo veriíy LhaL your anLivirus applicaLion and iLs signaLures are up-Lo-
daLe and LhaL you have updaLed anLispyware insLalled. One lasL Lhing. ií you have Lhe laLesL
Service Pack insLalled on your machine, you should have Lhe SecuriLy CenLer in your ConLrol
Panel. lí you don'L have iL, please updaLe your operaLing sysLem Lo Lhe laLesL Service Pack
(only available on MicrosoíL Windows Server" 2008 and Windows XP). Open Lhe SecuriLy
CenLer and make sure all lighLs íor Lhe Firewall, virus ProLecLion, and AuLomaLic UpdaLes are
green. lí noL, address Lhose issues Lo prevenL any securiLy hazards.
Click Lhe FxiL buLLon when you are done. On exiL, Lhe seLup applicaLion will send some
íeedback on your insLallaLion Lo Lhe MicrosoíL servers as shown in Figure 2-9.
;^\jgZ'".
G[XFXghcTcc_\VTg\bafXaWf\afgT__T
g\baYXXWUTV^gbg[X@\VebfbYgfXeiXef
ln the future, more products wiII
be added to this Iist.
ABG 8
C02622132.indd 22 10/18/05 6:28:42 PM
Chapter 2. lnstaIIing VisuaI Basic 2005 £xpress £dition 23
ln Summary...
1his chapLer íocused on insLalling visual 8asic 2005 Fxpress FdiLion. lL addressed mosL issues
LhaL you may encounLer during Lhe insLallaLion, diííerenL seLup scenarios, and also provided
links Lo MSDN íor more help.
AíLer working Lhrough Lhis chapLer, you should now have Lhe .NF1 Framework 2.0, visual
8asic 2005 Fxpress FdiLion, MSDN Fxpress Library, and SOL Server 2005 Fxpress FdiLion
insLalled and ready Lo go. ¥our compuLer should also be up Lo daLe wiLh all updaLes insLalled
and all seLLings relaLed Lo securiLy on green.
Whenever you're ready Lo explore Lhe lDF and wriLe your íirsL Lwo applicaLions, jusL jump
Lo Lhe nexL chapLer.
C02622132.indd 23 10/18/05 6:28:42 PM
C02622132.indd 24 10/18/05 6:28:42 PM
25
GjbGlcXfbY
4cc_\VTg\baf-J[TgÆf
g[X7\YYXeXaVX2%)
:Xgg\aZFgTegXW%*
Bagbg[XCeb]XVgf&$
6[TcgXe&
6eXTg\aZLbhe
9\efg4cc_\VTg\ba
(
¥ou've been inLroduced Lo MicrosoíL® visual 8asic
®
2005 Fxpress FdiLion,
now iL's Lime Lo creaLe your íirsL applicaLion. ¥ou'll sLarL Lhis chapLer by
learning abouL Lhe diííerences beLween a consoIe appIication and a
windows appIication. ¥ou'll Lhen look aL Lhe lntegrated DeveIopment
£nvironment {lD£). As iLs name implies, Lhe lDF is Lhe applicaLion LhaL pro-
vides all Lhe Lools you need Lo design, plan, develop, and disLribuLe your
applicaLions. We could have used any LexL ediLor, like NoLepad, íor example,
buL in Lhis chapLer and íor Lhe reminder oí Lhe book, you'll be using Lhe lDF
Lo creaLe your applicaLions.
MosL programming books usually sLarL wiLh a íairly simple applicaLion
called Hello World! ¥our íirsL applicaLion will be a simple applicaLion as well,
buL you'll be creaLing an applicaLion LhaL does a liLLle biL more Lhan jusL say
"hello" Lo Lhe world. ln Lhis chapLer, you'll learn Lo creaLe an applicaLion LhaL
adds Lwo numbers LogeLher and ouLpuLs a resulL. WiLh Lhis applicaLion you'll
also learn abouL Lhe SoluLion Fxplorer as well as Lhe documenLaLion and
Help sysLem builL inLo visual 8asic 2005 Fxpress FdiLion.
C03622132.indd 25 10/18/05 6:41:29 PM
26 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
Two Types of AppIications. what's the Difference?
As menLioned on Lhe previous page, you're going Lo creaLe Lwo versions oí Lhe same appli-
caLion. a console applicaLion and a MicrosoíL Windows
®
applicaLion. ¥ou mighL wonder why
you even have Lo worry abouL Lhis disLincLion when you're creaLing a program. Well, some-
Limes your applicaLion can'L or doesn'L need Lo have a graphical inLeríace. For insLance,
some applicaLions need Lo be execuLed in a scripL or a baLch íile or Lhey can'L have a graphi-
cal inLeríace because no user inLeracLs wiLh iL. ln some oLher cases you don'L need
a graphical inLeríace íor a Windows applicaLion. For example, a Windows service is an appli-
caLion LhaL runs on Windows in Lhe background-iL doesn'L have a user inLeríace, doesn'L
produce any visual ouLpuL, sLarLs when Windows sLarLs, and doesn'L even require
a user Lo be logged in Lo sLarL execuLing.
ln some oLher cases, you don'L wanL users Lo inLeracL wiLh a graphical inLeríace you've
creaLed. For insLance, you could wriLe an applicaLion LhaL moniLors Lhe available physical
memory on a compuLer Lhen displays LhaL iníormaLion once iL reaches a cerLain level. 1he
iníormaLion is displayed in a user inLeríace buL does noL require any inpuL írom Lhe user.
Figure 8-í shows Lhe resulL oí Lhe íamous Hello World! applicaLion as a console
applicaLion.
;^\jgZ("&
6bafb_XTcc_\VTg\ba
windows XP SP2 comes with
roughIy four dozen windows
services. lor exampIe, a windows
service vaIidates your user name
and password at start up.
ABG 8
1his Lype oí applicaLion is called a console applicaLion because everyLhing is displayed
in a sysLem console window. ¥ou may have heard Lhese Lypes oí windows reíerred Lo by
many diííerenL names. a DOS window, a command prompL window, or simply Lhe command
window. 1he mosL common ouLpuL in a console applicaLion is simple LexL.
C03622132.indd 26 10/18/05 6:41:30 PM
Chapter 3. Creating Your lirst AppIication 27
Console applicaLions can be as íeaLure rich and as complex as Windows applicaLions.
1he only diííerence is LhaL Lhey don'L have a graphical inLeríace. LeL's look aL one possible
use íor console applicaLions.
ln corporaLe daLa cenLers, many applicaLions execuLe all day and nighL, producing a
large amounL oí daLa. lL would be Lime-consuming and problemaLic Lo rely on people Lo
veriíy Lhe daLa. So daLa cenLers are usually highly auLomaLed Lo íaciliLaLe Lhis job. Console
applicaLions produce, manipulaLe, and veriíy Lhe daLa in scripLs or baLch íiles. Console
applicaLions can be wriLLen in many diííerenL programming languages (C, C--, C#, visual
8asic, and so on) and scripLing languages (Perl, PyLhon, or JScripL
®
).
ln conLrasL Lo a console applicaLion, a Windows
applicaLion has a graphical inLeríace, as shown in
Figure 8-2. (lL is also called a Windows Forms
applicaLion in .NF1).
1hese applicaLions are usually insLalled and accessi-
ble in Lhe Windows SLarL menu, and by deíaulL Lhey
share some common characLerisLics, such as a Close
buLLon, a Maximize buLLon, and a Minimize buLLon.
H^beaZIZmiVcY
6H8>>8]VgVXiZgh
Simµle text (clsc cclleJ
ASCll chcrccters} is the µsµcl
cµtµµt cf c ccnscle cµµliccticn.
bµt scme ccnscle cµµliccticns
µse ASCll çrcµhic chcrccters.
(ASCll stcnJs fcr Americcn
StcnJcrJ CcJe fcr lnfcrmcticn
lnterchcnçe.} An ASCll ccJe is
the nµmericcl reµresentcticn
cf c chcrccter (sµch cs ´0¨cr
´=¨} cr cn ccticn cf scme scrt.
(Pressinç lnter in c wcrJ µrc-
cesscr tc mcve c line cf text is
cn ccticn reµresenteJ bv cn
ASCll chcrccter. fcr instcnce.}
1he ASCll çrcµhic chcrccter set.
clsc cclleJ extenJeJ ASCll chcr-
ccters. inclµJes verticcl lines.
verticcl Jcµble lines. ccrners.
cnJ mµch mcre. ASCll chcrcc-
ters cre scmetimes µseJ tc cre-
cte bcxes crcµnJ text in ccnscle
cµµliccticns.
PIease note that consoIe appIi-
cations are stiII executed in
windows, but in a consoIe context.
ABG 8
;^\jgZ("'
J\aWbjfTcc_\VTg\ba
Getting Started
1o geL sLarLed wriLing Lhe code íor Lhe íirsL version oí Lhe applicaLion, you'll need Lo sLarL
MicrosoíL visual 8asic 2005 Fxpress FdiLion. 1o do Lhis íollow Lhese Lhree easy sLeps. click
SLarL, choose All Programs, Lhen selecL MicrosoíL visual 8asic 2005 Fxpress FdiLion.
Close 8uLLon Maximize 8uLLon Minimize 8uLLon
C03622132.indd 27 10/18/05 6:41:31 PM
28 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
1he íirsL Lime you sLarL MicrosoíL visual 8asic 2005 Fxpress FdiLion, iL will Lake some Lime
Lo load since Lhe lDF is being coníigured. ¥ou should see a screen similar Lo Figure 8-8.
8eíore we go íuLher, leL's pause and
admire Lhe lDF in all iLs glory. WiLh Lhis ver-
sion oí visual 8asic, Lhe lDF goL a nice new
makeover. Look aL Figure 8-4 and íeel Lhe
exciLemenL.
lí you're noL íeeling Lhe exciLemenL yeL,
you soon will. 1he developmenL environmenL has been designed Lo make a loL more iníor-
maLion available up íronL and Lo geL you more producLive íasLer. From Lhis screen, every-
Lhing you need Lo build an applicaLion is available in a couple oí clicks. From Lhe lDF you
;^\jgZ("(
8ai\eba`XagVbaY\ZheTg\baYbeY\efg
g\`XhfXbY@\VebfbYgI\fhT_5Tf\V
%##(8kceXff8W\g\ba
;^\jgZ(")
G[X<78\aT__\gfZ_bel
\a|r Too|ba·
So|J¦|or |.p|o·e·
\SDN \|sJa| |as|c |SS
|eed
\erJ |a·
Too|bo.
|ecer¦ |·o|ec¦s
Ce¦¦|rg S¦a·¦ed
\|sJa| |as|c
|.p·ess |ead||res
S¦a¦Js |a·
C03622132.indd 28 10/18/05 6:41:33 PM
Chapter 3. Creating Your lirst AppIication 29
Lype in your code, compile your code, launch Lhe applicaLion, íind your misLakes and íix
Lhem, geL help on Lhe synLax, and many oLher Lhings. FurLhermore, Lhe visual 8asic 2005 lDF
is designed Lo help you by generaLing a loL oí code íor you so LhaL you have less Lo Lype in.
lí you didn'L have Lhe lDF and you wanLed Lo wriLe a Windows Forms applicaLion, you would
normally have Lo wriLe a loL more code and iL would be more prone Lo errors. WiLh Lhe new
lDF, mosL oí Lhe acLions you'll períorm will auLomaLically generaLe a loL oí code behind Lhe
scenes.
As you spend more Lime wiLh Lhe program, you'll íind LhaL Lhere are many diííerenL ways
Lo períorm Lhe same acLions. For insLance, Lo compleLe a speciíic acLion you can use a series
oí menu choices, or use a keysLroke shorLcuL, or click an iconon a tooIbar, click on a
hyperIink in a page, or you can righL-click and choose an opLion írom a context-sensitive
menu. 8eíore diving inLo our íirsL applicaLion and beíore you wriLe some code, we'll go
Lhrough each big componenL oí Lhe lDF.
1he íirsL page you see is a really useíul one. Lhe SLarL Page. LeL's geL inLo whaL
iníormaLion you see on LhaL page.
N kecent Projects Here, you'll geL Lhe lisL oí projecLs or soluLions LhaL were recenLly
opened. ¥ou can also creaLe a new projecL or open an exisLing projecL LhaL is noL in Lhe
lisL.
N Getting Started l call Lhis useíul secLion Help CenLral because ií you need quick help,
Lhis is one oí Lhe besL places Lo geL answers. WheLher you need help wiLh some visual
8asic consLrucLs, or you wanL Lo see a lisL oí How 1o arLicles, or you simply wanL hyper-
links Lo communiLies oí programmers, you can oíLen íind help in Lhe CeLLing SLarLed
secLion oí Lhe lDF.
N VisuaI Basic £xpress HeadIines 1his is where you íind speciíic news abouL visual 8asic
Fxpress coming írom MicrosoíL. 1hese producL headlines deliver special messages speciíic
Lo visual 8asic 2005 Fxpress FdiLion and announce new updaLes, new releases, new code
snippeLs, or anyLhing LhaL needs special aLLenLion on your parL.
l wiII show you how and where
to Iook for this generated code in
Chapter 5.
ABG 8
£ven when the Start Page disap-
pears, you can aIways get it back
by going to the View meu and
seIecting Start Page.
The first and onIy ruIe of this book
is to not be afraid to experiment.
CIick, Iook, read, and try when-
ever possibIe. This is reaIIy the
best way to Iearn. l'II show you
some important materiaI, tips, and
tricks throughout this book, but
my advice to you is to go beyond
these exampIes and just try and
try and try . . .
G < C
C03622132.indd 29 10/18/05 6:41:33 PM
30 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
N MSDN leeds 1his secLion oí Lhe SLarL Page includes hyperlinks Lo arLicles írom one oí
Lhe MSDN kSS (MicrosoíL Developer NeLwork Really Simple SyndicaLion) íeeds. 1hese
arLicles can be coníigured Lo any valid RSS íeed írom Lhe Web. 1he deíaulL is seL Lo Lhe
MSDN
®
visual 8asic 2005 Fxpress RSS íeed. 1hese arLicles are diííerenL írom Lhe visual
8asic Fxpress Headlines, occasionally, Lhey mighL be Lhe same, buL Lhe arLicles írom MSDN
cover noL only visual 8asic Fxpress buL all Lopics including visual SLudio 1eam SysLems,
MicrosoíL SOL Server", Web Services, and so íorLh. ¥ou can
modiíy Lhe íeed by selecLing OpLions írom Lhe 1ools menu,
expanding FnvironmenL, selecLing SLarLup, and updaLing
Lhe SLarL Page News Channel íield wiLh a valid RSS íeed oí
your choice.
lD£ Components
Some imporLanL componenLs oí Lhe lDF are noL parL oí Lhe SLarL page.
N Menu Bar 1his is where you can selecL and períorm almosL all possible acLions relaLed Lo
your projecLs, íiles, and Help. 1he available opLions change based on Lhe currenL conLexL.
For example, when you don'L have an open projecL you have íewer menu choices. only
File, FdiL, view, 1ools, Window, CommuniLy, and Help. When a projecL is open, Lhe menu
choices will also include ProjecL, 8uild, Debug, and DaLa.
N Main TooIbar 1his Loolbar conLains icons LhaL are essenLially shorLcuLs Lo acLions LhaL
you can also períorm by going Lhrough Lhe menus.
N TooIbox lí you scroll over Lhe Loolbox on Lhe leíL side oí Lhe SLarL Page screen, iL will
expand. lí Lhere is no opened projecL, Lhe Loolbox should be empLy. 1he Loolbox conLains
conLrols LhaL are used in your applicaLions. AL Lhis poinL you can Lhink oí conLrols as visual
elemenLs in Windows applicaLions LhaL possess a graphical inLeríace. For insLance, once a
projecL is opened, Lhe Loolbox will include buLLons, labels, LexL box conLrols, menus, Lool-
bars, and so on. 1hese conLrols are deíined and explained in greaLer deLail in ChapLer 5.
N SoIution £xpIorer 1his íeaLure lisLs Lhe íiles and componenLs in your projecL. lí no proj-
ecL is open, iL should be empLy. ¥ou'll see more deLails abouL Lhe SoluLion Fxplorer laLer
in Lhis chapLer.
The Microsoft DeveIoper Network
{MSDN) is a set of onIine and
offIine services designed to heIp
deveIopers write appIications
using Microsoft products and
technoIogies.
ABG 8
Some hyperIinks on the Start page
require a Iive connection to the
lnternet. So, if you are unabIe to
read the hyperIinks, pIease verify
your lnternet connection status.
ABG 8
lf you don't see Startup and other
settings in the Options diaIog box,
ensure that you seIect the Show
AII Settings check box in the Iower
Ieft corner of the Options diaIog
box.
ABG 8
C03622132.indd 30 10/18/05 6:41:34 PM
Chapter 3. Creating Your lirst AppIication 3l
N Status Bar 1he sLaLus bar displays a wide varieLy oí iníormaLion corresponding Lo Lhe
sLaLe oí cerLain acLive operaLions. For insLance, when you load a projecL you'll see a
message on your screen such as "Loading projecL c.\blabla\blabla.vbproj írom your hard
drive." When you're building an applicaLion you'll see someLhing like "8uild sLarLed," and
when Lhe applicaLion has íinished, you'll see "8uild succeeded" or "8uild íailed" depend-
ing on Lhe success oí Lhe process.
On to the Projects
From Lhis poinL on, you'll íocus on whaL you really came here Lo do. build some projecLs. So
leL's sLarL wiLh your íirsL applicaLion. FirsL, you'll build Lhe console version oí Lhe applicaLion.
BuiIding a ConsoIe AppIication
We've been Lalking abouL whaL a console applicaLion can do and whaL iL will look like, so
why don'L we build one¯ ln Lhis secLion, you'll creaLe a simple maLhemaLic applicaLion.
TO BUlLD A CONSOL£ APPLlCATlON
$
|¦ \|sJa| |as|c ´JJ' |.p·ess |d|¦|or |s ro¦ ·Jrr|rg. s¦a·¦ |¦ by c||c||rg GhUfh#5``Dfc[fUag#A]WfcgcZh
J]giU`6Ug]W&$$)9ldfYgg9X]h]cb
%
`oJ car c|oose ¦o s¦a·¦ bJ||d|rg yoJ· app||ca¦|or e|¦|e· by c||c||rg ¦|e BYkDfc^YWh |cor or ¦|e ¦oo|
ba·. by se|ec¦|rg 7fYUhY.Dfc^YWh ¦·on ¦|e S¦a·¦ |age. o· by c||c||rg :]`Y#BYkDfc^YWh or ¦|e
nerJ ba·
&
|r ¦|e Ne. |·o|ec¦ d|a|og bo.. se|ec¦ 7cbgc`Y5dd`]WUh]cb |r ¦|e Tenp|a¦es sec¦|or. ard ¦ype Am:]fgh
7cbgc`Y5dd`]WUh]cb|r ¦|e Nane bo. T|e Ne. |·o|ec¦ d|a|og bo. s|oJ|d be s|n||a· ¦o ¦|e ore |r
||gJ·e `' C||c| C? .|er yoJ'·e ·eady ¦o go ¦o ¦|e re.¦ s¦ep
lor demos and sampIes, l recom-
mend that you type in aII the
source code so you can see and
better understand concepts.
However, for Ionger source code
Iistings, you can aIso downIoad the
compIeted code sampIes. {httµ.//
www.micrcscft.ccm/msµress/ccmµcn-
icn/0-7JS6-221J-2/)
G < C
C03622132.indd 31 10/18/05 6:41:34 PM
32 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
¥ou should now see Lhe lDF in an idle sLaLe waiLing íor you Lo wriLe Lhe applicaLion's
code. ¥our screen should now look like Lhe one shown here in Figure 8-6.
Getting to Know SoIution £xpIorer
8eíore you wriLe Lhe code, you need Lo learn abouL Lhe SoluLion Fxplorer. As seen on Lhe
righL side oí Lhe screen in Figure 8-6, Lhe SoluLion Fxplorer provides an organized view oí
your projecLs and all Lhe íiles LhaL are associaLed wiLh Lhem, as well as some useíul com-
mands in Lhe íorm oí a Loolbar. ¥ou'll íind all Lhe source code íiles, Lhe projecL seLLings,
resource íiles (like Lhe applicaLion icon), coníiguraLion íiles, and so on in Lhe SoluLion
Fxplorer.
By defauIt in VisuaI Basic 2005
£xpress £dition, when you cIick
OK, projects are created in a tem-
porary Iocation. when you save or
cIose the project, fiIes are saved
in My Documents\VisuaI Studio
2005\Projects. You can change
the defauIt projects Iocation by
going to TooIs/Options/Projects
and SoIutions and finding the
first text box named VisuaI Studio
Projects Iocation. You'II Iook into
what those fiIes are and what their
content is Iater in this chapter and
aIso in Chapter 5.
ABG 8
;^\jgZ("*
6eXTg\aZTVbafb_XTcc_\VTg\bahf\aZ
g[XAXjCeb]XVgW\T_bZUbk
lf the SoIution £xpIorer disappears
for any reason, you can get it back
by going to the View menu and
seIecting SoIution £xpIorer.
G < C
C03622132.indd 32 10/18/05 6:41:35 PM
Chapter 3. Creating Your lirst AppIication 33
lí you wanL more iníormaLion abouL Lhe SoluLion Fxplorer you can always do a search in
Lhe Help sysLem and producL documenLaLion. 8eíore Lrying Lo períorm a search, please read
Lhe nexL secLion, you'll learn a loL abouL all Lhe iníormaLion LhaL is aL your disposiLion.
Getting HeIp. Microsoft VisuaI Studio
2005 £xpress £dition Documentation
lí you wanL Lo read more abouL Lhe SoluLion Fxplorer, you need Lo be inLroduced Lo Lhe
Help and Lhe documenLaLion sysLems. ¥ou access Lhe documenLaLion by pressing Fí írom
;^\jgZ("+
@l9\efg6bafb_X4cc_\VTg\baj\g[bhgg[XVbWX
Note that the Start Page is stiII
avaiIabIe, it's simpIy in a separate
tab. You can go to any window by
cIicking a specific tab or by press-
ing CtrI+Tab.
G < C
C03622132.indd 33 10/18/05 6:41:36 PM
34 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
wiLhin visual 8asic 2005 Fxpress FdiLion or by going Lhrough Lhe Help menu. 1he íirsL Lime
you press Fí or go Lhrough Lhe Help menu, you'll be greeLed wiLh Lhe Online Help SeLLings
screen, as shown in Figure 8-7.
1his screen prompLs you Lo choose a primary help source, you can choose online Help as
a primary source, local Help as a primary source, or no online Help aL all. 1hink abouL your
opLions careíully. lí you don'L have a
broadband (cable, DSL, or saLelliLe)
lnLerneL connecLion, l suggesL you
choose Lhe local Help as a primary
source, oLherwise choose online Help
as your primary source since iL is Lhe
besL source íor Lhe laLesL iníormaLion.
Once you've made your selecLion,
you'll see Lhe documenLaLion graphical
inLeríace, as shown in Figure 8-8.
;^\jgZ(",
Ba_\aX;X_cFXgg\aZfV[b\VXf
C03622132.indd 34 10/18/05 6:41:37 PM
Chapter 3. Creating Your lirst AppIication 35
1he Loolbar aL Lhe Lop oí Lhe window includes several inLeresLing elemenLs LhaL will help
you Lo geL exacLly whaL you need. Figure 8-9 highlighLs Lhe mosL imporLanL iníormaLion
abouL Lhe Loolbar.
;^\jgZ("-
@\VebfbYgI\fhT_FghW\b%##(8kceXff8W\g\baWbVh`XagTg\ba
For example, leL's say you wanL Lo learn more abouL Lhe SoluLion Fxplorer. Click Lhe
Search icon in Lhe Loolbar and Lhe search page opens. FnLer your search query (in Lhis
example, "SoluLion Fxplorer"), and eiLher press FnLer or click Lhe Search buLLon. 1he help
resulLs come írom íour diííerenL sources. LeL's go over each one brieíly.
N LocaI HeIp 1his source is íed by Lhe MSDN Library (parL oí Lhe producL insLallaLion) and
is insLalled on your hard drive. (lí you selecLed iL aL insLall.)
Tas|based ¦op|cs
Ca¦e.ay ¦o ¦oJ· sea·c| soJ·ces
Save a |e|p sea·c| `oJ· oJes¦|ors or \SDN ¦o·Jns
Syrc|·or|.e TCC
;^\jgZ(".
<`cbegTagX_X`Xagfbag[Xgbb_UTe
C03622132.indd 35 10/18/05 6:41:38 PM
36 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
N MSDN OnIine 1his source conLains Lhe mosL up-Lo-daLe iníormaLion írom Lhe MSDN
Online Library.
N Codezone Community 1he Codezone CommuniLy is a seL oí Web siLes based on
MicrosoíL developer producLs. 1o see a lisL oí all Web siLes LhaL are researched click
1ools/OpLions and Lhen Online. Figure 8-í0 shows whaL you should look íor. NoLice Lhe
lisL oí Web siLes on Lhe righL LhaL are parL oí Lhe Codezone CommuniLy. ln Lhe íuLure,
Lhis lisL mighL be expanded Lo show more siLes Lo provide even beLLer coverage oí Lhe
communiLy.
On Lhe same screen, you can cusLomize diííerenL seLLings relaLed Lo Lhe Help sysLem.
On Lhe Ceneral Lab, you can seL up how Lhe Help sysLem reLrieves and presenLs
iníormaLion. ¥ou can seL up Lhe inLernaLional seLLing Lo geL local help in Lhe language oí
your operaLing sysLem, ií available, and geL online Help in a predeíined lisL oí languages
supporLed by MSDN. Look aL Lhe keyboard shorLcuL mapping íor menu commands used
LhroughouL Lhe producL (íor example, noLice LhaL Lhe shorLcuL íor Copy is CLrl-C), or Lo
assign new shorLcuLs Lo commands LhaL mighL noL have one already.
;^\jgZ("&%
Bcg\baffVeXXaj\g[g[XBa_\aX
fXgg\aZf\aV_hW\aZg[X6bWXmbaX
6b``ha\glJXUf\gXf
C03622132.indd 36 10/18/05 6:41:39 PM
Chapter 3. Creating Your lirst AppIication 37
N Ouestions 1his secLion searches Lhe MSDN Online íorums (hLLp.//íorums.microsoíL.com/
msdn/). 1hese íorums are hosLed by MicrosoíL and are an excellenL source oí iníormaLion
because Lhey have quesLions and answers on Lopics asked by oLher programmers oí all
levels and experience. 1here's a good chance LhaL somebody has had Lhe same problem
or Lhe same quesLion as you, so your chance oí íinding an answer Lo your problem is
much beLLer. ¥ou can have coníidence in Lhe answers you geL because answers on Lhe
MSDN íorums are oíLen validaLed by MicrosoíL employees or MvPs. A check mark in a
green circle Lells you which answer has been validaLed as correcL.
Coding Your ConsoIe AppIication
Now LhaL you know how Lo geL help ií needed, l believe you are ready Lo code your íirsL
console applicaLion.
TO COD£ A CONSOL£ APPLlCATlON
$ To beg|r. e|¦|e· ¦ype o· copy ¦|e ¦o||o.|rg code |r yoJ· code .|rdo.
1 Modu]e Modu]e1
2 ` 1h1s app]1caf1on W1]] fake fWo numbe¡s and d1sp]ay fhe sum
3 5ub Ma1n1}
4 ` Dec]a¡1ng fWo 1nfege¡ numbe¡s va¡1ab]es fhaf W1]] ho]d fhe 2 pa¡fs
S ` of fhe sum and one 1nfege¡ va¡1ab]e fo ho]d fhe sum
6 D1m numbe¡1. numbe¡2. sum As lnfege¡
7
ß ` Ass1gn1ng fWo va]ues fo fhe 1nfege¡ va¡1ab]es
9 numbe¡1 = 10
10 numbe¡2 = S
11 ` Add1ng fhe fWo 1nfege¡s and sfo¡1ng fhe ¡esu]f 1n fhe sum va¡1ab]e
12 sum = numbe¡1 + numbe¡2
13
14 `D1sp]ay1ng a sf¡1ng W1fh fhe 2 numbe¡s and fhe ¡esu]f.
1S Conso]e.W¡1feL1ne1¨1he sum of ¨ & numbe¡1.1o5f¡1ng1} & ¨ and ¨ & numbe¡2.
1o5f¡1ng1} & ¨ 1s ¨ + sum.1o5f¡1ng1}}
16 Lnd 5ub
17 Lnd Modu]e
MVPs stands for Most VaIuabIe
ProfessionaIs. MVPs are profes-
sionaIs that are not Microsoft
empIoyees but are recognized
by Microsoft as experts in their
fieIds.
ABG 8
C03622132.indd 37 10/18/05 6:41:39 PM
38 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
%
No. ¦|a¦ ¦|e code |s |r ¦|e .|rdo.. yoJ car save yoJ· .o·| by
c||c||rg ¦|e GUjY bJ¦¦or T||s .||| save ¦|e cJ··er¦ ¦||e C||c| ¦|e
GUjY5`` bJ¦¦or ¦o save a|| nod|¦|ed ¦||es |r ¦|e p·o|ec¦
&
No. |¦ |s ¦|ne ¦o bJ||d (o· conp||e) ¦|e app||ca¦|or C||c| ¦|e 6i]`X
nerJ ard ¦|er c||c| or 6i]`XAm:]fgh7cbgc`Y5dd`]WUh]cb
|¦ yoJ |eyed ¦|e code e.ac¦|y as |¦ appea·s above. eve·y¦||rg s|oJ|d be ¦|re ard yoJ s|oJ|d see ¦|e nessage
'|J||d sJcceeded' |r ¦|e s¦a¦Js ba· a¦ ¦|e bo¦¦on o¦ ¦|e .|rdo. (See ||gJ·e `11) |¦ eve·y¦||rg |s ro¦ o|ay.
yoJ'|| see e··o·s |r ¦|e |··o· ||s¦ .|rdo. (as s|o.r |r ||gJ·e `1´) |¦ yoJ |eyed ¦|e code ard |ave e··o·s. ¦·y
copy|rg ard pas¦|rg ¦|e code |rs¦ead ¦·on ¦|e conp|e¦ed code sanp|es (|¦¦p¹¹...n|c·oso¦¦con¹nsp·ess¹
conpar|or¹J¯`'b´´1`´¹) T|er bJ||d ¦|e code aga|r
You can aIso save using the foI-
Iowing keystrokes. CLrl-S to save
the current fiIe and CLrl-ShiíL-S to
save aII fiIes.
G < C
AZVgc^c\idGZVY8dYZ
Ancther wcv cf lecrninç
cbcµt ccJe is tc recJ cll the
ccmments in cn cµµliccticn
scµrce ccJe. l will exµlcin c lct
cf the scµrce ccJe in this bccl.
bµt cfter µsinç scme tcµics mcre
thcn cnce (cr twice} l miçht nct
re-exµlcin ecch line. bµt instecJ
exµlcin cnlv the new mctericl. l
will trv tc inclµJe helµfµl ccm-
ments in the ccJe tc çµiJe vcµ
cs vcµ´re lecrninç. At the enJ cf
ecch chcµter. l will clsc inclµJe
hvµerlinls thct will µcint tc
crticles. viJecs. cnJ white µcµers.
l will cften inclµJe levwcrJs
tc helµ vcµ secrch fcr mcre
infcrmcticn in cnline helµ. 1hct
shcµlJ helµ vcµ tc µrcçress in
vcµr lecrninç in the lcnçµcçe
cnJ in .Nl1 in çenercl. Ycµ hcve
tc leeµ in minJ thct this bccl is
recllv shcwinç vcµ cll vcµ ccn Jc
with the µrcJµct cnJ the steµs tc
çet there. lt´s nct c bccl cn OOP
cr the visµcl 3csic lcnçµcçe
itself. We cre still çcinç tc çc
thrcµçh 70-80º cf the lcnçµcçe
cnJ mcst OOP ccnceµts. bµt it
will clwcvs be thrcµçh bµilJinç
cµµliccticns cnJ never thrcµçh
Jrv references cnJ exµlcncticns.
;^\jgZ("&&
8`cgl8eebe?\fgj\aWbjTaWfgTghf
UTej\g[Ã5h\_WFhVVXXWXWÄ`XffTZX
;^\jgZ("&'
8eebe?\fgj\aWbjj\g[Xeebef
C03622132.indd 38 10/18/05 6:41:40 PM
Chapter 3. Creating Your lirst AppIication 39
|r C|ap¦e· ¯. yoJ'|| |ea·r aboJ¦ a|| ¦|e debJgg|rg ¦ec|r|oJes yoJ car Jse .|er yoJ ge¦ ar e··o·
'
To see ¦|e e.ecJ¦|or ·esJ|¦s o¦ yoJ· app||ca¦|or. c||c| ¦|e GhUfh8YVi[[]b[ bJ¦¦or |r ¦|e na|r ¦oo|ba·
(o· ||¦ |')
Wow! 1haL was íasL, wasn'L iL¯ ¥ou probably saw a command window íor a íew seconds
and Lhen iL disappeared. lL didn'L leave you a loL oí Lime Lo see ií your applicaLion displayed
Lhe expecLed ouLpuL. ln Lhe nexL secLion, you'll look aL a new way oí running your
applicaLion Lo solve Lhis problem. 1o do Lhis, you'll need Lo cusLomize Lhe lDF.
Customizing the lD£
¥ou can easily cusLomize Lhe lDF Lo íiL your needs. Here, you wanL Lo execuLe your applica-
Lion and Lhen have Lhe applicaLion pause auLomaLically aL Lhe end oí Lhe lasL insLrucLion Lo
allow you as much Lime as you need Lo view Lhe ouLpuL. ¥ou'll do Lhis by adding an icon
and iLs aLLached command Lo Lhe main Loolbar and Lo Lhe Debug menu. 1he name oí Lhe
command you'll be adding Lo Lhe lDF is "SLarLing WiLhouL Debugging."
TO CUSTOMlZ£ TH£ lD£
$
Se|ec¦ ¦|e Hcc`g nerJ ard ¦|er se|ec¦
7ighca]nY
%
|r ¦|e CJs¦on|.e d|a|og bo.. se|ec¦ ¦|e
7caaUbXg ¦ab
&
|r ¦|e Ca¦ego·|es a·ea or ¦|e |e¦¦ s|de o¦ ¦|e
.|rdo.. se|ec¦ 8YVi[
'
Sc·o|| do.r |r ¦|e Connards a·ea ard se|ec¦
GhUfhK]h\cih8YVi[[]b[
C03622132.indd 39 10/18/05 6:41:40 PM
40 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
`oJ· sc·eer s|oJ|d ro. |oo| |||e ¦|e
ore |r ||gJ·e `1`
;^\jgZ("&(
6hfgb`\mXj\aWbjj\g[FgTegJ\g[bhg
7XUhZZ\aZfX_XVgXW
Both figures have the "before¨
on the Ieft and the "after¨ on the
right, overIapping the before.
ABG 8
;^\jgZ("&)
5XYbeXTaWTYgXeVhfgb`\m\aZg[X
7XUhZ@Xahj\g[g[XFgTegJ\g[bhg
7XUhZZ\aZVb``TaW
;^\jgZ("&*
5XYbeXTaWTYgXeVhfgb`\m\aZg[Xgbb_
UTej\g[g[XFgTegJ\g[bhg7XUhZZ\aZ
Vb``TaW
Now you musL add Lhe command Lo Lhe Debug menu. 1o do LhaL, drag SLarL WiLhouL
Debugging írom Lhe Commands area and drop iL onLo Lhe Debug menu. (Figure 8-í4 shows
a beíore and aíLer view oí Lhis process.) Now repeaL Lhese sLeps, excepL Lhis Lime, drop Lhe
command onLo Lhe Loolbar Lo Lhe righL oí Lhe SLarL Debugging buLLon Look aL
Figure 8-í5 íor a beíore and aíLer look aL Lhe Loolbar. When íinished, click Close in Lhe
CusLomize dialog box.
C03622132.indd 40 10/18/05 6:41:41 PM
Chapter 3. Creating Your lirst AppIication 4l
1o make sure Lhe cusLomizaLion worked, click
Lhe new icon on Lhe Debug menu or Lhe Loolbar.
(Or, use Lhe keyboard shorLcuL CLrl-F5.) ¥ou
should see a command prompL window wiLh Lhe
expecLed ouLpuL, which is Lhe sLring "1he sum oí
í0 and 5 is í5." ¥ou should also see Lhe message
"Press any key Lo conLinue . . ." as shown in
Figure 8-í6.
As you probably realized by now, Lhe eííecL
oí Lhe new command is Lo add Lhe "Press any key
Lo conLinue." and pause Lhe execuLion aíLer Lhe
lasL insLrucLion is execuLed. Press any key Lo close
Lhe command prompL window and reLurn Lo Lhe
lDF. When you're done, you can close Lhe projecL by going Lo File/Close ProjecL. ¥ou'll be
prompLed Lo save or discard your changes. Click Save and Lhen, ií Lhe name and locaLion are
íine, click Save again in Lhe Save ProjecL dialog box.
Creating a windows AppIication
¥ou've jusL builL a console applicaLion. 1he nexL
sLep is Lo develop Lhe same applicaLion as a
Windows applicaLion.
TO BUlLD A wlNDOwS APPLlCATlON
$
\|er c·ea¦|rg ¦|e corso|e app||ca¦|or. yoJ
sa. ¦|e Ne. |·o|ec¦ d|a|og bo. Cper |¦
aga|r by go|rg ¦o ¦|e |||e nerJ ard se|ec¦|rg
BYkDfc^YWh""" T||s ¦|ne. se|ec¦ K]bXckg
5dd`]WUh]cb ¦·on ¦|e Tenp|a¦es sec¦|or ard ¦ype |r
Am:]fghK]bXckg5dd`]WUh]cb \a|e sJ·e yoJ· sc·eer
|oo|s |||e ¦|e ore |r ||gJ·e `1¯ ard ¦|er c||c| C?
;^\jgZ("&+
6b``TaWceb`cgj\aWbjj\g[g[X
XkcXVgXWeXfh_gTaWT`XffTZX\aW\
VTg\aZTcThfXWXkXVhg\ba
;^\jgZ("&,
6eXTg\aZTJ\aWbjfTcc_\VTg\bahf\aZ
g[XAXjCeb]XVgW\T_bZUbk
C03622132.indd 41 10/18/05 6:41:43 PM
42 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
`oJ'|| |nned|a¦e|y see ¦|a¦ ¦|e ·esJ|¦
o¦ ¦||s ope·a¦|or |s oJ|¦e d|¦¦e·er¦ ¦o·
¦|e \|rdo.s app||ca¦|or p·ocess ¦|ar
|¦ .as ¦o· ¦|e corso|e app||ca¦|or p·o
cess `oJ s|oJ|d see ¦|e \|rdo.s
|o·n Des|gre· as d|sp|ayed |r
||gJ·e `18
;^\jgZ("&-
<78j\g[g[XJ\aWbjf9be`7Xf\ZaXeTaW
TaX`cglYbe`
;^\jgZ("&.
J\aWbjf9be`7Xf\ZaXefheYTVX
j\g[T5hggbaVbageb_
%
Cr ¦|e |e¦¦ s|de o¦ ¦|e |D|. nove yoJ· noJse ove· ¦|e Too|bo.
¦ab ¦o oper ¦|e Too|bo. .|rdo. C||c| ¦|e p|Js (÷) s|gr re.¦
¦o ¦|e Connor Cor¦·o|s `oJ'|| see a ||s¦ o¦ ¦o·n cor¦·o|s ¦|a¦ a·e
o¦¦er seer |r a \|rdo.s app||ca¦|or Se|ec¦ ¦|e |J¦¦or cor¦·o|.
d·ag |¦ or¦o ¦|e Des|gre· sJ·¦ace `oJ s|oJ|d ge¦ a ¦o·n ¦|a¦ |oo|s
|||e ¦|e ore |r ||gJ·e `1º
C03622132.indd 42 10/18/05 6:41:44 PM
Chapter 3. Creating Your lirst AppIication 43
\aybe yoJ dor'¦ ·ea||.e |¦ bJ¦ a¦ ¦||s noner¦ yoJ a|·eady
|ave a ¦J|| ard va||d \|rdo.s app||ca¦|or .|¦|oJ¦ |av|rg .·|¦
¦er a s|rg|e ||re o¦ code T|e app||ca¦|or doesr'¦ do ary¦||rg
ve·y Jse¦J| a¦ ¦||s po|r¦. bJ¦ |¦ .o·|s¦ `oJ car eas||y ve·|¦y ¦||s
by ·Jrr|rg ¦|e app||ca¦|or ¹Js¦ ||¦ l5 ard see ¦o· yoJ·se|¦
T||s |s pa·¦ o¦ ¦|e nag|c ard ¦|e beaJ¦y o¦ Js|rg ¦|e \|sJa|
S¦Jd|o
·
|D| erv|·orner¦ ¦o· p·og·ann|rg |rs¦ead o¦ Js|rg a
¦e.¦ ed|¦o· |||e No¦epad \|sJa| S¦Jd|o .·|¦es a |o¦ o¦ code ¦o·
yoJ. ard |r C|ap¦e· '. .e'|| ¦a|e a |oo| a¦ sone o¦ ¦|e ac¦|v|¦y
¦|a¦'s ¦a||rg p|ace be||rd ¦|e sceres ¦o na|e |¦ appea· |||e
nag|c \|er ¦|r|s|ed. c||c| ¦|e C|ose bJ¦¦or or ¦|e ¦o·n ¦o
·e¦J·r ¦o ¦|e |D|
&
DoJb|ec||c| ¦|e bJ¦¦or or ¦|e des|gre· sJ·¦ace `oJ'||
ge¦ ¦|e ¦an|||a· soJ·ce code .|rdo. bJ¦ .|¦| d|¦¦e·er¦
cor¦er¦ ¦||s ¦|ne |o· ro.. ¦ype o· copy ¦|e code be¦.eer ¦|e
SJb \a|r() ard ¦|e |rd SJb ¦·on ¦|e corso|e app||ca¦|or yoJ
c·ea¦ed p·ev|oJs|y ard add |¦ |r ¦|e |J¦¦or1_C||c| ne¦|od as
s|o.r |r ||gJ·e `´J (`oJ'|| |ea·r no·e aboJ¦ ¦||s ne¦|od ard ¦|e .|o|e p·ocess be||rd ¦|e doJb|ec||c|
|r C|ap¦e· 4)
'
|r ¦|e soJ·ce code. ¦|rd ¦|e .o·ds 7cbgc`Y"Kf]hY@]bY ard ·ep|ace ¦|en .|¦| ¦|e .o·ds
AYggU[Y6cl"G\ck T|er bJ||d ard e.ecJ¦e ¦|e app||ca¦|or by ||¦¦|rg :)
(
\|er ¦|e ¦o·n cones Jp. c||c| ¦|e bJ¦¦or ard yoJ'|| see ¦|e ·esJ|¦ o¦ yoJ· app||ca¦|or a nessage bo.
.|¦| ¦|e sane s¦·|rg yoJ've seer |r ¦|e corso|e app||ca¦|or |¦ s|oJ|d |oo| |||e
||gJ·e `´1 C||c| C? |r ¦|e nessage bo. ard ¦|er oJ|¦ ¦|e p·og·an by c||c||rg ¦|e
7`cgY bJ¦¦or or ¦|e na|r ¦o·n
;^\jgZ("'&
BhgchgbY@l9\efgJ\aWbjf4cc_\VTg\ba
;^\jgZ("'%
5hggbaV_\V^`Xg[bWj\g[g[XVbWXYeb`
bheceXi\bhfXkT`c_X
C03622132.indd 43 10/18/05 6:41:45 PM
44 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
CongraLulaLions! ¥ou've jusL creaLed your íirsL Lwo applicaLions. a console applicaLion and
a Windows applicaLion.
ln Lhis chapLer, you learned some key iníormaLion LhaL will help you build on Lhe skills
you've sLarLed developing in Lhe previous chapLers. ¥ou learned Lhe diííerences beLween a
console applicaLion and a Windows applicaLion. ¥ou sLarLed Lhe producL, wenL Lhrough Lhe
lDF, and learned iLs major componenLs. ¥ou creaLed Lwo versions oí Lhe same applicaLion. a
console applicaLion and a Windows applicaLion. While learning abouL console applicaLions,
you also learned whaL Lhe SoluLion Fxplorer is as well as how Lo search and use Lhe producL
documenLaLion and Lhe Help sysLem.
ln Lhe nexL chapLer, you'll build on Lhis knowledge and wriLe a simple Web browser. For
now, ií you wanL Lo read more abouL some Lopics covered in Lhis chapLer, simply creaLe a
search query by pressing Fíand Lhen Lype in Lhe keywords provided in Lhe box Lo Lhe leíL.
l've also added a link Lo videos LhaL are a good source oí iníormaLion Lo conLinue your
learning.
ln Summary...
@ZnldgYhVcYA^c`h
idBdgZ>c[dgbVi^dc
Keywords. Jeclcrinç vcricbles.
strinç ccncctencticn
Ancther çccJ scµrce
cf infcrmcticn is the viJecs
frcm MSDN. 1hese viJecs
were sµecificcllv crecteJ tc
ccver the visµcl 3csic 200S
lxµress lJiticn µrcJµct. Ycµ
ccn finJ viJec fcr lesscn 1
ct the fcllcwinç lcccticn.
httµ.//çc.micrcscft.ccm/fwlinl/
?linliJ=440J0&clciJ=0x40º
C03622132.indd 44 10/18/05 6:41:45 PM
45
J[Tg<fTCeb]XVg2')
J[Tg<fg[X7Xf\Za
?Tlbhg2'*
Chgg\aZ<g4__GbZXg[Xe('
6[TcgXe'
6eXTgXLbheBja
JXU5ebjfXe\a?Xff
G[Ta9\iX@\ahgXf
)
Now LhaL you've goLLen a liLLle experience wiLh creaLing simple applicaLions
in MicrosoíL
®
visual 8asic
®
2005 Fxpress FdiLion, you'll build a more com-
plicaLed applicaLion in Lhis chapLer and íinish iL in ChapLer 6. ln Lhis chapLer,
you'll sLarL wiLh Lhe basic íramework oí Lhe applicaLion, in Lhe nexL Lwo
chapLers, you'll conLinue Lo learn new íeaLures and add Lhem Lo enhance
your projecL.
ln Lhis chapLer, you're going Lo learn how Lo build your own basic Web
browser, and you'll be able Lo do iL in íive minuLes or less!
C04622132.indd 45 10/18/05 6:47:40 PM
46 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
what ls a Project?
ln Lhe previous chapLer, you creaLed a projecL Lo hold your source code. l'll now Lake a
momenL Lo explain whaL a projecL is and whaL iníormaLion iL conLains. 1he projecL is a con-
Lainer íor all iLems in your applicaLion, such as íorms, source code, and resources. lL also
sLores imporLanL coníiguraLion daLa LhaL belongs Lo Lhe applicaLion as a whole. Lhe locaLion
oí Lhe execuLable (i.e., binaries) on your hard drive, Lhe version iníormaLion, and many more
seLLings LhaL aííecL all oí Lhe characLerisLics oí your applicaLion. For insLance, iL's also sLoring
programmer-deíined applicaLion seLLings LhaL are very imporLanL íor Lhe user experience.
Users love Lo cusLomize Lheir soíLware environmenL Lo Lheir comíorL level and personal sLyle.
A Lypical uLilizaLion oí Lhose user seLLings is Lo make sure Lhe applicaLion can preserve user
cusLomizaLion írom one execuLion Lo anoLher. A good example oí cusLom user seLLings can
be seen wiLh your own user preíerences in lnLerneL Fxplorer, such as your home page
address, your home page seLLings, which Loolbars are displayed, and wheLher your Loolbars
are locked in size and so íorLh.
ln Lhe nexL chapLer, you'll learn abouL some oí Lhe mosL imporLanL seLLings sLored in
Lhe projecL coníiguraLion íile and how Lo use Lhem in your applicaLion. ln Lhe íinal chapLer
oí Lhis book, you will use programmaLic Lechniques Lo preserve Lhe user's seLLings and
cusLomizaLions.
1he name you choose when creaLing your applicaLion is going Lo become your
projecL name. lL will also become Lhe deíaulL íolder name on your hard drive where your
applicaLion will be sLored when you save your applicaLion. lL will also become Lhe deíaulL
namespace oí your applicaLion. A namespace is used Lo organize Lhe classes in a single
hierarchical sLrucLure. lL does Lhe same íor any oLher Lypes you mighL deíine. 1he creaLion
oí a namespace helps prevenL naming collisions. WhaL is a naming collision¯ LeL's look aL an
example Lo illusLraLe Lhis concepL.
Suppose a company called AdvenLureWorks wroLe a new Windows Forms class
named ANewForm. 1hey would creaLe a namespace called AdvenLureWorks and puL Lheir
ANewForm class in iL Lo uniquely name Lheir class. 1he íully qualiíied name oí a class is
always composed oí Lhe namespace íollowed by a doL and Lhen Lhe name oí Lhe class(es).
AdvenLureWorks's unique class would be AdvenLureWorks.ANewForm.
C04622132.indd 46 10/18/05 6:47:41 PM
Chapter 4. Create Your Own web Browser in Less Than live Minutes! 47
Now leL's suppose LhaL you are creaLing a new projecL using visual SLudio and decide Lo
name your projecL MyLibrary. visual SLudio would Lhen creaLe íor you a namespace called
MyLibrary. Suppose LhaL you Lhen deíine a new class and name iL ANewForm. ¥ou mighL noL
be aware LhaL a company called AdvenLureWorks also called Lheir new class using Lhe same
name. Fven Lhough AdvenLureWorks mighL be doing compleLely diííerenL Lhings wiLh Lheir
class, a problem could arise because Lhe Lwo classes are named Lhe same.
Now suppose LhaL you're Lrying Lo use boLh classes called ANewForm in your new
applicaLion. lí you simply use ANewForm, Lhe compiler will noL be able Lo deLermine
which ANewForm class you wanL Lo use Lhe one írom your library or Lhe one írom Lhe
AdvenLureWorks library, Lhis is a naming collision. 8y preíixing Lhe class name wiLh Lhe
namespace name, you are Lhen Lelling Lhe compiler exacLly which class you wanL Lo use
(AdvenLureWorks.ANewForm).
what ls the Design Layout?
¥ou will soon creaLe a new design layouL in Lhe íorm designer. ln doing Lhis, you'll be creaL-
ing whaL Lhe applicaLion conLains and how iLs conLenL is presenLed when Lhe user execuLes
Lhe applicaLion.
1o accomplish Lhis phase oí a projecL, you Lypically do noL need Lo Lype a greaL deal oí
code, as explained laLer in Lhis chapLer, visual SLudio Lakes care oí LhaL íor you. ¥ou simply
have Lo worry abouL how your applicaLion looks. When you're done designing all Lhe visual
aspecLs Lo your liking, your nexL Lask usually involves aLLaching Lhe source code Lo your
visual layouL so LhaL your applicaLion reacLs Lo and acLs upon Lhe user's inpuL.
ln Lhis chapLer, you will compleLe Lhe basic layouL. ¥ou will learn more advanced layouL
Lechniques in íollowing chapLers. LeL's sLarL Lhe nexL projecL. a simple Web browser.
C04622132.indd 47 10/18/05 6:47:41 PM
48 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
TO Ck£AT£ A SlMPL£ w£B BkOwS£k
$
S¦a·¦ \|sJa| |as|c ´JJ' |.p·ess |d|¦|or by c||c||rg GhUfh5``Dfc[fUag and A]WfcgcZhJ]giU`6Ug]W
&$$)9ldfYgg9X]h]cb
%
C·ea¦e a re. \|c·oso¦¦ \|rdo.s
·
/pp||ca¦|or |·o|ec¦ Js|rg ary o¦ ¦|e ¦ec|r|oJes s|o.r |r ¦|e p·e
v|oJs c|ap¦e·s ¦|·oJg| e|¦|e· ¦|e :]`Y nerJ o· ¦|e Ne. |·o|ec¦ |cor |r ¦|e ¦oo|ba· Nane ¦|e re.
app||ca¦|or AmCkb6fckgYf
&
Cr ¦|e des|gr sJ·¦ace. yoJ'|| see ¦|e enp¦y \|rdo.s |o·n .|¦| a ¦|¦|e ba· raned:cfa% C||c| or
¦|e ¦|¦|e ba· orce |oo| ove· ¦o ¦|e p·ope·¦|es .|rdo. or ¦|e bo¦¦on ·|g|¦ o¦ ¦|e |D|. as s|o.r |r
||gJ·e 41
;^\jgZ)"&
CebcXeg\Xfj\aWbjYbe@lBja5ebjfXe
Tcc_\VTg\ba9be`Vbageb_
C04622132.indd 48 10/18/05 6:47:42 PM
Chapter 4. Create Your Own web Browser in Less Than live Minutes! 49
\e'|| be Js|rg nos¦ o¦ ¦|e p·ope·¦|es yoJ see ||s¦ed |e·e¦ ||g|¦ ro. .|a¦ |s |npo·¦ar¦ ¦o· yoJ ¦o Jrde·
s¦ard |s ¦|a¦ nos¦ o¦ ¦|ese p·ope·¦|es |r¦|Jerce |o. ¦|e cor¦·o| yoJ |ave se|ec¦ed be|aves o· .|a¦ |¦ |oo|s
|||e .|er yoJ e.ecJ¦e yoJ· app||ca¦|or
|o· a|| sanp|es |r ¦||s boo|. | sJgges¦ ¦|a¦ yoJ so·¦ ¦|e p·ope·¦|es ||s¦ |r ascerd|rg a|p|abe¦|ca| o·de·. |¦ .|||
be nJc| eas|e· ¦o ¦|rd p·ope·¦|es ¦|a¦ | ·e¦e·erce |r ¦|e e.anp|es To so·¦ ¦|e p·ope·¦|es |r a|p|abe¦|ca|
o·de·. c||c| or ¦|e AIphabeticaI bJ¦¦or ¦·on ¦|e |·ope·¦|es .|rdo. ¦oo|ba·T|e o¦|e· op¦|or |s ¦o
a··arge ¦|e cor¦·o|s by ca¦ego·|es. bJ¦ ¦||s n|g|¦ s|o. do.r yoJ· p·og·ess ¦|·oJg|oJ¦ ¦||s boo|
\|ereve· yoJ se|ec¦ a p·ope·¦y. yoJ'|| see a b·|e¦ desc·|p¦|or o¦ |¦s Jsage a¦ ¦|e bo¦¦on o¦ ¦|e |·ope·¦|es
.|rdo. |e¦e· ¦o ||gJ·e 41 as ar e.anp|e |r ¦||s case. ¦|e ¦e.¦ p·ope·¦y .as se|ec¦ed /¦ ¦|e bo¦¦on o¦
¦|e |·ope·¦|es .|rdo.. yoJ car see a sJcc|rc¦ ¦e.¦ nessage desc·|b|rg ¦|e ¦Jrc¦|or o¦ ¦|e ¦e.¦ p·ope·¦y
/s ner¦|ored |r C|ap¦e· `. ny bes¦ adv|ce ¦o· |ea·r|rg ¦||s so¦¦.a·e |s ¦o ¦·y. ¦·y. ard ¦·y aga|r T|e·e a·e a
va·|e¦y o¦ ¦oo|s |r \|sJa| |as|c ´JJ' |.p·ess |d|¦|or ard a·e ¦|e·e¦o·e nary poss|b|||¦|es `oJ .||| |ea·r ¦o Jse
nos¦ o¦ ¦|ese ¦oo|s by Js|rg ¦||s boo|. bJ¦ |¦'s |nposs|b|e ¦o |ea·r a|| va·|a¦|ors ard poss|b|||¦|es |¦ yoJ dor'¦
do sone e.p|o·|rg or yoJ· o.r \|¦| ¦|a¦ |r n|rd. ¦o Jrde·s¦ard ¦|e e¦¦ec¦ o¦ a c|arge or a pa·¦|cJ|a·
p·ope·¦y. ¦·y a|| d|¦¦e·er¦ poss|b|e va|Jes |ac| ¦|ne yoJ nod|¦y a p·ope·¦y. bJ||d ard c|ec| ¦|e e.ecJ¦|or
|o.eve·. dor'¦ na|e no·e ¦|ar ore c|arge a¦ a ¦|ne |¦ .||| be d|¦¦|cJ|¦ ¦o· yoJ ¦o |ro. .||c| ore o¦ yoJ·
c|arges ac¦Ja||y ¦·|gge·ed a v|sJa| nod|¦|ca¦|or |y e.p|o·|rg no·e ¦|ar ore poss|b|||¦y. yoJ'|| be ab|e ¦o
see ¦|e e¦¦ec¦ o¦ yoJ· c|arges |nned|a¦e|y
'
\a|e sJ·e yoJ |ave se|ec¦ed ¦|e |o·n cor¦·o| raned lorml |r s¦ep ` ard ¦|er nod|¦y ¦|e ¦o||o.|rg
p·ope·¦|es Js|rg ¦|e va|Jes |r Tab|e 41 T|e p·ope·¦y rane ¦o nod|¦y |s |oca¦ed |r ¦|e |e¦¦ co|Jnr.
ard ¦|e va|Je ¦o .||c| ¦o se¦ ¦|e p·ope·¦y |s |oca¦ed |r ¦|e ·|g|¦ co|Jnr `oJ nay |ave a|·eady conp|e¦ed
¦||s s¦ep. bJ¦ ¦o ¦ac|||¦a¦e yoJ· da¦a er¦·y. ve·|¦y ¦|a¦ yoJ |ave so·¦ed ¦|e p·ope·¦|es |r ascerd|rg a|p|abe¦|
ca| o·de·
Some properties have a {+) beside
them, which means that it's a tree
view property. whenever you cIick
the {+), you'II expand this prop-
erty to dispIay the property attri-
butes, which you wiII then be abIe
to set. whenever you wiII be asked
to enter vaIues for properties that
are in a tree view, l wiII use the
foIIowing notation structure as
found in this format. H^oZ/L^Yi],
which refers to the Size property
and the width attribute.
< @C BEG4AG
IVWaZ)"&
?\fgbYYbe`cebcXeg\XfgbV[TaZX
Property VaIue
1ext My Own 8rowser
Size.WiJth 640
Size.Heiçht 480
C04622132.indd 49 10/18/05 6:47:43 PM
50 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
\e'·e ro. go|rg ¦o add ¦|·ee \|rdo.s |o·ns cor¦·o|s ¦o yoJ· b·o.se· app||ca¦|or a ¦e.¦ bo. cor¦·o| |r
.||c| ¦o er¦e· ¦|e des¦|ra¦|or |||. a bJ¦¦or ¦o rav|ga¦e ¦o ¦|e \eb page. ard a \eb b·o.se· cor¦·o| |r
.||c| ¦|e \eb page cor¦er¦ .||| be d|sp|ayed
(
D·ag ard d·op a \eb b·o.se· cor¦·o| or¦o ¦|e des|gre· sJ·¦ace T|e \eb b·o.se· cor¦·o| |s |oca¦ed
|r ¦|e hcc`Vcl or ¦|e |e¦¦ s|de o¦ ¦|e |D|. |¦'s ¦|e |as¦ cor¦·o| |r ¦|e WcaacbWcbhfc`gsec¦|or |y
de¦aJ|¦. ¦||s cor¦·o| .||| ¦||| ¦|e des|gre· sJ·¦ace er¦|·e|y /s .e dor'¦ .ar¦ ¦||s be|av|o· ¦o· ¦||s pa·¦|cJ|a·
app||ca¦|or. yoJ'|| c||c| or ¦|e ¦o||o.|rg b|ac| ¦·|arg|e
.||c| .||| p·odJce ¦|e cor¦er¦ o¦ a sna·¦ ¦ag (|¦ yoJ'·e ¦an|||a· .|¦| \|c·oso¦¦ C¦¦|ce ´JJ`. yoJ've p·obab|y
ercoJr¦e·ed sna·¦ ¦ags be¦o·e Sna·¦ ¦ags .||| be e.p|a|red ¦J·¦|e· |r C|ap¦e· ') |r ¦||s pa·¦|cJ|a· e.an
p|e. |¦ .||| |e|p yoJ ¦o Jrdoc| ¦|e cor¦·o| ¦·on |¦s pa·er¦ cor¦a|re· (¦|e ¦o·n) C||c| or ¦|e sna·¦ ¦ag ard
se|ec¦ IbXcW_]bdUfYbhWcbhU]bYf
)
|.pard ¦|e cor¦·o| so ¦|a¦ |¦ occJp|es a|nos¦ ¦|e er¦|·e des|gre· space To do ¦||s. c||c| or ary o¦ ¦|e
cor¦·o| |ard|es ¦o c|arge |¦s s|.e
*
To na|e sJ·e yoJ
|ave ¦|e sane
s|.e as s|o.r |r
||gJ·e 4´. se|ec¦ ¦|e
\eb b·o.se· cor¦·o| by
c||c||rg ary.|e·e or
¦|e cor¦·o| T|er go ¦o
¦|e |·ope·¦|es .|rdo.
ard nod|¦y ¦|e va|Jes
¦o· ¦|e a|| ¦|e p·ope·¦|es
||s¦ed |r Tab|e 4´
\od|¦y ¦|e va|Jes |r ¦|e
sane .ay yoJ nod|¦|ed
¦|e ¦o·n cor¦·o|s |r
s¦ep 4
To add a controI to a form, you
need to perform a drag-and-drop
operation. This means you'II move
your mouse to the tooIbox, drag
the desired controI to the designer
surface, and drop the controI
onto it.
G < C
;^\jgZ)"'
@lBja5ebjfXeTcc_\VTg\ba
C04622132.indd 50 10/18/05 6:47:43 PM
Chapter 4. Create Your Own web Browser in Less Than live Minutes! 5l
+
D·ag ard d·op a ¦e.¦ bo. cor¦·o| ard a bJ¦¦or cor¦·o| ¦·on ¦|e ¦oo|bo. connor cor¦·o|s sec¦|or so
¦|a¦ yoJ· ¦o·n |oo|s |||e ||gJ·e 4´ C|arge ¦|e p·ope·¦|es o¦ ¦|e cor¦·o|s as yoJ d|d .|¦| ¦|e \eb
b·o.se· cor¦·o| |r s¦ep ¯ Se|ec¦ ore cor¦·o| a¦ a ¦|ne ard nod|¦y |¦s p·ope·¦|es .|¦| ¦|e da¦a |r Tab|e 4`
Property VaIue
(Ncme} my8rowser
Size.WiJth 607
Size.Heiçht 885
lcccticn.X í2
lcccticn.Y í2
IVWaZ)"'
JXU6bageb_cebcXeg\XfgbV[TaZX
,
/¦ ¦||s po|r¦. .e ro. |ave a conp|e¦e app||ca¦|or `oJ car conp||e ard e.ecJ¦e yoJ· app||ca¦|or by
p·ess|rg l5
ControI Property VaIue
1extbcx (Name) LxLURL
1extbcx LocaLion.X í2
1extbcx LocaLion.¥ 4íí
1extbcx Size.WidLh 526
1extbcx Size.HeighL 20
3µttcn (Name) bLnCo
3µttcn LocaLion.X 544
3µttcn LocaLion.X 4íí
3µttcn LexL CO
IVWaZ)"(
6bageb_fCebcXeg\XfTaWIT_hXf
C04622132.indd 51 10/18/05 6:47:44 PM
52 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
lí you íollowed Lhe previous sLeps exacLly, your applicaLion should now be running.
8ecause we didn'L code any íuncLionaliLy, enLering an URL and hiLLing Lhe CO buLLon will
noL do anyLhing.
l will use an analogy Lo explain a íundamenLal concepL. A lighL bulb by iLselí is noL a use-
íul piece oí hardware. 1o obLain lighL írom iL, you need Lo connecL Lwo wires carrying elec-
LriciLy. Similar Lo whaL an elecLrician would do Lo creaLe Lhis elecLrical circuiL, we need Lo
aLLach or wire Lhe conLrol and Lhe acLion LogeLher by wriLing code Lo handle Lhe evenL oí
clicking Lhe CO buLLon. Keep Lhis analogy in mind when you see reíerences Lo Lhe Lerm wire
or wirinç used wiLhin Lhis book.
8eíore we Lry Lo execuLe Lhis applicaLion, leL me explain Lhe line oí code you'll add in
sLep 2 on Lhe nexL page and link iL Lo Lhe OOP concepLs previously inLroduced in ChapLer í.
When you dropped Lhe conLrols onLo Lhe designer suríace, you creaLed insLances oí Lhe
cIass represenLed by Lhose conLrols. For example, when you dropped Lhe Web browser con-
Lrol, you creaLed an instance oí Lhe class SysLem.Windows.Forms.Web8rowser LhaL you
named my8rowser. 1he Web8rowser class has many meLhods, and Lhe NavigaLe method is
Lhe one you chose. As iLs name implies, Lhis meLhod allows Lhe Web8rowser Lo navigaLe Lo
an URL. A meLhod in visual 8asic can be a subrouLine or a íuncLion. 1he URL was passed as
an argument Lo Lhe NavigaLe meLhod. An argumenL, also called a parameLer, is used Lo pass
daLa Lo a meLhod.
1he argumenL in your case is Lhe LexL Lhe user enLered in Lhe insLance oí Lhe SysLem.
Windows.Forms.1exL8ox class LhaL you appropriaLely named LxLURL. 1o reLrieve Lhe conLenL
oí Lhe 1exL8ox named LxLURL, you used Lhe 1exL property oí LhaL conLrol. A properLy allows
you Lo seL or reLrieve Lhe conLenL oí a data member in a class wiLhouL accessing Lhe daLa
member direcLly. 1haL way, Lhe provider oí Lhe class (e.g., MicrosoíL) can modiíy Lhe imple-
menLaLion oí Lhe 1exL properLy wiLhouL concerning Lhe user wiLh implemenLaLion deLails. ln
OOP, Lhis is called encapsuIation. ¥ou can compare Lhis process Lo a person driving a car.
you don'L need Lo know how Lhe engine and Lransmission work in order Lo drive Lhe car. 1he
car example is a good one, buL anoLher good example is Lhe NavigaLe meLhod. ¥ou don'L
need Lo know how iL's implemenLed, you simply wanL iL Lo do iLs job. As menLioned earlier,
many Lhings are happening when you design a íorm wiLh visual SLudio. ¥ou have seen LhaL
you didn'L need Lo creaLe any oí Lhe classes or insLances represenLing your conLrols because
visual SLudio is doing all oí LhaL íor you!
C04622132.indd 52 10/18/05 6:47:44 PM
Chapter 4. Create Your Own web Browser in Less Than live Minutes! 53
TO wlk£ TH£ CLlCK ACTlON TO A BUTTON
$
C|ose ¦|e ·Jrr|rg app||ca¦|or ard go bac| ¦o ¦|e |D| DoJb|ec||c| ¦|e bJ¦¦or cor¦·o| `oJ'|| see ¦|e
code .|rdo. as s|o.r |r ||gJ·e 4`
lf you try to type some code and
it doesn't work, your appIication is
probabIy stiII running. lf you don't
cIose the appIication and you go
back to VisuaI Basic, you won't be
abIe to modify the source code. A
good way to verify that you have
cIosed and terminated the appIica-
tion is to Iook in the VisuaI Basic
£xpress titIe bar. lf you see the
name of your appIication foIIowed
by the word (rµnninç}, this means
your appIication is stiII active and
you won't be abIe to add code.
lf you try to add code, the status
bar wiII report that you are in
read-onIy mode with the foIIowing
message. Cannot currentIy modify
this text in the editor. lt is in read-
onIy.
ABG 8
|¦ yoJ ¦e·n|ra¦ed ¦|e e.ecJ¦|or o¦ yoJ· app||ca¦|or p·ope·|y. yoJ s|oJ|d see ¦|e soJ·ce code .|rdo. .|¦| ¦|e
b¦rCo c||c| ever¦ ¦enp|a¦e \|er yoJ doJb|ec||c|ed ¦|e bJ¦¦or cor¦·o|. yoJ s|gra|ed \|sJa| S¦Jd|o ¦|a¦ yoJ
.ar¦ed ¦o .|·e ¦|e c||c| ac¦|or ¦o ¦|e bJ¦¦or cor¦·o| Typ|ca||y. eac| cor¦·o| car ¦·|gge· nJ|¦|p|e ever¦s
deperd|rg or .||c| be|av|o· yoJ .ar¦ ¦o |r¦e·cep¦ .|¦| yoJ· code |ac| cor¦·o| |as a de¦aJ|¦ ever¦ ¦|a¦ |s
¦·|gge·ed by ¦|e p·og·anne· by Js|rg a doJb|ec||c| ac¦|or or ¦|e cor¦·o| or ¦|e des|gre· sJ·¦ace |r yoJ·
case. \|sJa| S¦Jd|o c·ea¦ed ¦|e c||c| ever¦ ¦enp|a¦e so ¦|a¦ yoJ coJ|d er¦e· ¦|e ¦o||o.|rg code
% Type |r ¦|e ¦o||o.|rg code a¦ ¦|e cJ·so·
P\%URZVHU1DYLJDWHW[W85/7H[W
&
|·ess l5 ¦o conp||e ard e.ecJ¦e ¦|e app||ca¦|or |¦ yoJ
raned yoJ· cor¦·o|s co··ec¦|y |r s¦ep 8 |r ¦|e p·ev|oJs
e.e·c|se ard er¦e·ed ¦|e ||re o¦ code as s|o.r |r s¦ep ´. yoJ
s|oJ|d ro. |ave yoJ· o.r \eb b·o.se· app||ca¦|or C¦
coJ·se. yoJ .or'¦ |ave a|| ¦|e be||s ard .||s¦|es o¦ |r¦e·re¦
|.p|o·e·. bJ¦ be pa¦|er¦¬.e'·e ge¦¦|rg ¦|e·e T·y go|rg ¦o yoJ·
¦avo·|¦e |||s ¦o see |¦ |¦'s .o·||rg as e.pec¦ed |o· |rs¦arce. |
.er¦ ¦o www.micrcscft.ccm ard |¦ .o·|ed |Js¦ ¦|re¦ `oJ car
see ¦|e ·esJ|¦ |r ||gJ·e 44
;^\jgZ)"(
6bWXj\aWbjYbeg[XUga:bV_\V^XiXag
;^\jgZ)#)
@lBja5ebjfXef[bj\aZg[X@\VebfbYg!Vb`JXUf\gX
C04622132.indd 53 10/18/05 6:47:46 PM
54 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
Putting lt AII Together
We've jusL seen LhaL when you drag a conLrol onLo Lhe design suríace, you're acLually creaL-
ing an objecL oí LhaL conLrol class. When you're naming Lhe conLrol in Lhe ProperLies win-
dow, you're acLually assigning a name Lo Lhe variable you've jusL creaLed-which is exacLly
whaL we did íor Lhe Lhree conLrols used in your browser. ln íacL, Lhis is why you wanL Lo give
your conLrols meaningíul names so LhaL you can use Lhem laLer programmaLically.
As you now know, Lhere was a greaL deal oí acLiviLy Laking place when you dropped
conLrols onLo Lhe designer suríace. 1o beLLer undersLand whaL was Laking place in Lhe
background, we've Lalked abouL imporLanL OOP concepLs behind Lhe line oí code you've
added Lo respond Lo Lhe click evenL.
Now LhaL you've run Lhe applicaLion, here is a lisL oí quesLions you may have.
N WhaL happens ií l puL noLhing in Lhe LexL box and hiL FnLer¯
N WhaL happens ií l enLer an invalid URL¯
N WhaL happens ií l enLer anyLhing l íeel like¯
My answer Lo you is simply, "1ry iL. 1ry iL now." 1he real deal is LhaL your Web browser
will acLually behave like any oLher Web browser and will navigaLe Lo whaLever URL is Lyped
inLo Lhe LexL box. lí you don'L Lype anyLhing, clicking on Lhe CO buLLon will have no eííecL.
lí you Lype anyLhing you íeel like, Lhe browser conLrol will come back wiLh a Page noL íound
or Code 404 page.
Now is your Lime Lo experimenL. Remember Lhis book's rule. 1R¥, 1R¥, 1R¥. So go ahead
and play wiLh iL. Change some oí Lhe properLies and see Lhe resulLs aL run Lime. AlLhough
we haven'L used many íeaLures yeL, you'll add more in ChapLer 6. 1his projecL is íar írom
over! 8y adding new íeaLures, you'll arrive aL a poinL where your applicaLion will sLarL Lo look
much more íamiliar.
Before moving on, l'm inviting
you to Iook at a video from the
MSDN
®
web site that taIks about
Object-Oriented Programming, or
OOP. You've had a good introduc-
tion to OOP both in this chapter
and in Chapter l. To understand
the concept from another angIe,
navigate to httµ.//çc.micrcscft.ccm/
fwlinl/?linliJ=440J0&clciJ=0x40º
and view Lesson 6, Parts l and 2.
< @C BEG4AG
PhiIosophies differ when it comes
to naming the variabIe that rep-
resents controIs on the design
surface. ln this book, l'm going to
use up to three Ietters to describe
and identify the controI type by
Iooking at its name, such as "btn¨
for a button controI. The vari-
abIe name then becomes btnGo.
l wiII introduce the Iist when l
taIk about common controIs in
Chapter 5.
@BE8 < A9 B
C04622132.indd 54 10/18/05 6:47:46 PM
Chapter 4. Create Your Own web Browser in Less Than live Minutes! 55
ln Summary...
ln Lhis chapLer, you learned how Lo build a Web browser. ¥ou sLarLed Lo.
N Add more Lhan one conLrol Lo Lhe designer suríace
N SeL properLies in Lhe ProperLies window
N Wire an evenL Lo a conLrol and learn how Lo add code LhaL will execuLe when Lhe evenL
is Lriggered
ln Lhis example, you've linked many OOP concepLs by using only one line oí code.
¥ou've added Lhe code Lo respond Lo Lhe buLLon click evenL by calling Lhe NavigaLe
meLhod oí your Web browser objecL. ¥our Web browser navigaLed Lo a URL passed in as an
argumenL Lo Lhe NavigaLe meLhod. 1he argumenL íor Lhe NavigaLe meLhod was passed in
using Lhe LexL box conLrol 1exL properLy. FveryLhing was compleLed and íully working jusL by
Lweaking some properLies and adding ONL¥ one line oí code! 1haL's whaL l call producLiviLy.
ln Lhe nexL chapLer, you'll conLinue Lhis process by learning all oí Lhe major íeaLures oí
visual 8asic 2005 Fxpress FdiLion. ¥ou'll become more producLive aL developing applicaLions
by learning íeaLures such as lnLelliSense
®
, snap lines, code snippeLs, smarL Lags, Lhe My
namespace, and much more.
A^c`hidBdgZ
>c[dgbVi^dc
Other çccJ scµrces cf infcr-
mcticn cre the viJecs frcm
MSDN sµecificcllv crecteJ tc
ccver the visµcl 3csic 200S
lxµress lJiticn µrcJµct. 1he
viJecs fcr lesscns 2 cnJ 7
ccver scme cf the tcµics vcµ
hcve jµst lecrneJ cnJ will µrc-
viJe vcµ with cncther µcint cf
view. Ycµ ccn finJ the viJecs
fcr lesscns 2 cnJ 7 bv tvµ-
inç in the fcllcwinç hvµerlinl.
httµ.//çc.micrcscft.ccm/fwlinl/
?linliJ=440J0&clciJ=0x40º.
C04622132.indd 55 10/18/05 6:47:47 PM
C04622132.indd 56 10/18/05 6:47:47 PM
57
´|o¡|e· '
Creqting Your
First Full Windows
®

Applicqtion
*
ln ChapLer 4, you sLarLed building your own Web browser, and in ChapLer 5
you'll add Lo iLs capabiliLies. 8uL beíore you do LhaL, l wanL Lo inLroduce
some MicrosoíL® visual 8asic
®
2005 íeaLures LhaL will help you develop
your MicrosoíL Windows
®
programming knowledge and skills. We'll look
inLo some oí Lhe more useíul rapid applicaLion developmenL (RAD) íeaLures
oí visual 8asic 2005 Fxpress FdiLion.
lí you already have some programming experience, you'll be pleasanLly
surprised Lo íind LhaL iL's even easier Lo program using visual 8asic 2005
Fxpress FdiLion.
You can find aII kAD features described in this chapter in every version of
VisuaI Studio 2005. So if you aIready know how to use these features in the
£xpress £dition and decide to expIore any of the other versions of VisuaI
Studio 2005, you'II find it much easier to switch and be productive.
ABG 8
Snqp qnd Align Those
Controls Using Snqp
Lines, 58
Using lntelliSense²Your
New 8est Friendl, 59
Reql-Time Lrror Detection
qnd Correction, 64
Oh My, My . . . My ls
Creqt, 66
Renqming, 68
Common Windows
Controls, 72
Whqt Hqppens When qn
Lvent ls Triggered?, 75
C05622132.indd 57 10/18/05 7:05:02 PM
58 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
Snap and AIign Those ControIs Using Snap Lines
NoL being a very skilled user inLeríace designer myselí, l've always had problems working on
a program wiLh many conLrols Lo align. Fven more diííiculL was Lrying Lo geL Lhe alignmenL
righL Lhe íirsL Lime l dropped Lhe conLrols onLo Lhe íorm. l've always had Lo go Lo Lhe
ProperLies window and align Lhe conLrols manually by enLering Lheir x and v coordinaLes,
which slows down Lhe developmenL process quiLe a biL! Again, leL me reiLeraLe one oí Lhe
philosophies Lhe visual 8asic 2005 Leam had in mind when creaLing Lhis awesome new prod-
ucL. 1hey wanLed Lo make sure you didn'L have Lo períorm mulLiple sLeps aL several diííerenL
places Lo accomplish a simple Lask. And Lhey succeeded wiLh a loL oí new íeaLures, one oí
Lhese is Lhe new snap lines íeaLure LhaL allows you Lo easily align objecLs on Lhe designer
suríace. LeL's do an exercise so you can see Lhe snap lines íeaLure in acLion.
TO Ck£AT£ A N£w wlNDOwS lOkM USlNG SNAP LlN£S
$
S¦a·¦ \|sJa| |as|c ´JJ' |.p·ess |d|¦|or by c||c||rg Start. AII Programs. ard Mìcrosoft VìsuaI ßasìc
2005 fxpress fdìtìon C·ea¦e a re. \|c·oso¦¦ \|rdo.s /pp||ca¦|or p·o|ec¦ Js|rg ary o¦ ¦|e ¦ec|
r|oJes s|o.r |r ¦|e p·ev|oJs c|ap¦e·s by Js|rg e|¦|e· ¦|e |||e nerJ o· ¦|e Ne. |·o|ec¦ |cor |r ¦|e ¦oo|ba·
Nane ¦|e re. app||ca¦|or 1estProject
%
`oJ s|oJ|d see ¦|e des|gre· sJ·¦ace |¦ yoJ dor'¦. ·|g|¦c||c|
or ¦|e ¦||erane forml.vb |r ¦|e So|J¦|or |.p|o·e· ard
c|oose Vìew Desìgner T|er. Js|rg ¦|e ¦oo|bo.. d·ag ¦|·ee |abe|s
ard ¦|·ee ¦e.¦ bo. cor¦·o|s or¦o ¦|e des|gr sJ·¦ace
&
S¦ac| ¦|e |abe|s ve·¦|ca||y / ¦||r b|Je ||re (a srap ||re) .|||
appea· or e|¦|e· ¦|e ·|g|¦ s|de o· ¦|e |e¦¦ s|de o¦ ¦|e |abe|s ¦o
|e|p gJ|de ¦|e a||grner¦ \|er ¦|e |abe|s a·e a||gred co··ec¦|y.
·e|ease ¦|e |abe| cor¦·o|
/s s|o.r |r ||gJ·e '1. a sna|| b|Je |o·|.or¦a| ||re a|so appea·s ¦o
¦|e |e¦¦ o¦ ¦|e |abe| cor¦·o| T||s ||re ·ep·eser¦s ¦|e n|r|nJn space
be¦.eer a cor¦·o| ard aro¦|e· cor¦·o| o· be¦.eer a cor¦·o| ard
|¦s cor¦a|re·
when you have many controIs to
drop on the designer surface, you
can save some time by "pinning¨
down the tooIbox so that it stays
open. To disabIe the AutoHide
feature, cIick the Pin button on
top of the tooIbox. To re-enabIe
the AutoHide feature, cIick the Pin
button again.
G < C T-
;^\jgZ*"&
'·o¡ ||·e |· o.||c· w||| |wc |o|e| .c·|·c|
C05622132.indd 58 10/18/05 7:05:03 PM
Chapter 5. Creating Your lirst luII windows AppIication 59
'
/¦¦e· a||gr|rg ¦|e |abe|s ve·¦|ca||y. do ¦|e sane .|¦| ¦|e bJ¦¦ors |nned|
a¦e|y ¦o ¦|e ·|g|¦ o¦ eac| |abe| T|e des|gre· sJ·¦ace s|oJ|d |oo| |||e ¦|e
ore s|o.r |r ||gJ·e '´
(
No¦|ce ¦|a¦ ¦|e |abe|s a·e a||gred .|¦| ¦|e bo¦¦on o¦ ¦|e ¦e.¦ bo. cor¦·o|s
|o· a c|eare· appea·arce. ¦|e |abe|s s|oJ|d a||gr .|¦| ary ¦e.¦ ¦|a¦ .||| be
er¦e·ed |r ¦|e ¦e.¦ bo. To a||gr ¦|e |abe|s co··ec¦|y. nove eac| |abe| Jr¦|| yoJ
see a |o·|.or¦a| pJ·p|e ||re |rs¦ead o¦ a b|Je ||re. as s|o.r |r ||gJ·e '` |r
||gJ·e '4. |abe|s 1 ard ´ |ave beer p·ope·|y a||gred .|¦| ¦|e base||re o¦ ¦|e
co··espord|rg ¦e.¦ bo. cor¦er¦s. bJ¦ |abe| ` |s s¦||| a||gred .|¦| ¦|e bo¦¦on o¦
¦|e ¦e.¦ bo. |¦se|¦
;^\jgZ*"'
/|| ||e .c·|·c| o·e ·cw o||o·eJ
;^\jgZ*"(
|xom¡|e c| o||o·me·| w||| ||e .cm
mc· |ex| |oe||·e
;^\jgZ*")
|:·||me exe.:||c· c| ||| o||o·me·| ¡·c||em
Using lnteIIiSense²Your New Best lriend!
lnLelliSense
®
is one oí Lhe greaLesL Lools developed íor boLh beginner and experienced pro-
grammers. 1his íeaLure provides conLexLual language reíerences wiLhin Lhe code ediLor and
can even compleLe Lhe Lyping íor you. 1his means you can geL immediaLe code synLax help
speciíic Lo Lhe code you're wriLing wiLhouL leaving Lhe code ediLor. For example, ií you're
inside a íorm and you ask lnLelliSense íor help, you'll geL access Lo code consLrucLs LhaL
make sense íor LhaL parLicular íorm. ¥ou've already experienced lnLelliSense while doing Lhe
previous examples wiLhouL really knowing LhaL's whaL you were using.
Do not cIose the test project, you'II need it for the sections that foIIow. lf
you cIose the test project and VisuaI Basic 2005 £xpress £dition and then
re-open them Iater, you might Iose the current view and your form and code
might not show up automaticaIIy. lf you do happen to cIose the test form,
cIick the View Code button on the SoIution £xpIorer tooIbarto get the code
of the seIected form or on the View Designer button to get the Designer of
the seIected form. AIternativeIy, you can right-cIick on the form fiIename, in
this case ;dgb&#kW, and then seIect View Codeto get to the source codeor
ViewDesigner to get to the design surface.
< @C BEG4AG
C05622132.indd 59 10/18/05 7:05:04 PM
60 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
lnteIIiSense and CtrI+Spacebar
One oí Lhe easiesL ways Lo bring up Lhe lnLelliSense window is Lo press CLrl-Spacebar.
Figure 5-5 shows an example írom Lhe projecL currenLly opened in Lhe code ediLor. ¥ou can
see in Lhis íigure a lisL oí possible choices based on Lhe conLexL oí a íorm elemenL named
Formí.
;^\jgZ*"*
|·|e|||'e·e |·vc|eJ |/ |||||·o ||e
´¯||÷'¡o.e|o· |e/|·c|e .cm||·o
||c·
There's a neat feature in VisuaI
Studio that tracks the changes you
made to the source code, simiIar
to the Track Changes feature in
Microsoft word. ln VisuaI Studio,
however, whenever you modify
your code, a coIored Iine is added
to the beginning of that Iine. A
yeIIow Iine indicates that a change
was made but has not been saved
yet. lf the Iine is green, it means
the change was saved and is now
part of the code even if you exit
VisuaI Studio. lt's a reaIIy usefuI
feature, and if you want to enabIe
it, just cIick TooIs/Options, then
seIect the Show AII Settings check
box at the bottom Ieft of the win-
dow. ln the tree view, expand Text
£ditor, seIect GeneraI, and then
seIect Track Changes.
MOk £ l Nl O
lnteIIiSense and Period/Left Parenthesis
1he second way you can geL help using lnLelliSense is by Lyping a period (".") aíLer an ele-
menL. 1he lnLelliSense window will show up wheLher you are using .NF1 objecLs or your own
objecLs. For example, l requesLed Lhe lisL oí possible consLrucLs involving Lhe variable Formí.
ln Lhis example, l was looking íor Lhe variable 1exL8oxí, which represenLed Lhe íirsL LexL box
on our Form. 8y Lyping a period (".") aíLer Lhe keyword Me, l goL Lhe lisL oí all relevanL
objecLs in Lhis conLexL. 1hen by Lyping Lhe leLLer t, l goL Lhe lisL oí all relevanL componenLs
LhaL have names beginning wiLh LhaL leLLer. l jusL had Lo scroll down Lo Lhe iLem l was look-
ing íor. 1ext3cx1. Figure 5-6 illusLraLes Lhese sLeps in Lhe code ediLor. Finally, l pressed Lhe
1ab key Lo inserL my selecLion, 1ext3cx1, in Lhe code.
C05622132.indd 60 10/18/05 7:05:04 PM
Chapter 5. Creating Your lirst luII windows AppIication 6l
TO US£ lNT£LLlS£NS£
;^\jgZ*"+
´e|||·o |e|¡ |·cm |·|e|||'e·e |/
|/¡|·o |· o ¡e·|cJ (· o||e· o vo||J
c||e.|
Know that at any time whiIe
you're using lnteIIiSense, you can
press the Tab key to move quickIy
through the seIections lnteIIisense
presents if the item you're Iooking
for is aIready seIected.
G < C
$
|·on ¦|e ¦oo|bo.. d·ag a bJ¦¦or cor¦·o| ¦o ¦|e
¦o·n
%
DoJb|ec||c| ¦|e bJ¦¦or ¦o b·|rg Jp ¦|e bJ¦¦or
c||c| ever¦ |ard|e· |r ¦|e code ed|¦o· T|er.
.|e·e ¦|e cJ·so· |s b||r||rg. p·ess CT||÷Spaceba·
T|e |r¦e|||Serse .|rdo. .||| oper
& Type ¦|e |e¦¦e· m. ¦|er e. ard ¦|er pe·|od (.)
'
S¦a·¦ ¦yp|rg textboxl |e¦o·e yoJ ¦|r|s| ¦|e
.o·d. |r¦e|||Serse s|oJ|d b·|rg Jp ¯ex||cx1
|·ess ¦|e 1ab |ey ¦o |rse·¦ ¦|e conporer¦
(
Type a pe·|od ('.') aga|r. ¦ype text. ¦|er p·ess
¦|e Tab |ey o· ¦|e Spaceba· T|e code ||re
s|oJ|d |oo| |||e ¦||s ore
Me.1exf8ox1.1exf
)
No. add ¦|e eoJa| s|gr (~) ard ¦|e s¦·|rg
||¦e·a| ¨HeIIo WorId" |r ¦|e erd. ¦|e ||re
s|oJ|d |oo| |||e ¦||s
Me.1exf8ox1.1exf = ¨he]]o Wo¡]d¨
¥ou can now build and execuLe Lhe applicaLion by pressing F5 and veriíy LhaL iL works.
When you click Lhe buLLon you creaLed, you should geL Lhe sLring "Hello World" in 1exL8oxí,
which normally should be Lhe íirsL one oí Lhe Lhree LexL boxes.
¥ou can also geL addiLional help írom lnLelliSense ií Lhere's more Lhan one available
choice íor your siLuaLion. 1yping a leíL parenLhesis ("(") displays a lisL oí all possible choices.
For insLance, when we did Lhe console applicaLion, we wroLe Lo Lhe console using Lhe
Console.WriLeLine meLhod. We used Lhis meLhod wiLh a sLring argumenL, buL you can do
more wiLh Ccnscle.Writeline Lhan jusL use a stringas an argumenL. lnLelliSense will indicaLe
ií Lhere is more Lhan one opLion. For Console.WriLeLine, lnLelliSense indicaLes Lhere are
more Lhan í8 possible variaLions, as you can see in Figure 5-7. l was looking íor Lhe second
variaLion, which is a 8oolean argumenL. Now iL's your Lurn Lo Lry iL.
;^\jgZ*",
|·|e|||'e·e o|ve /c: ||e ||| c|
o|| ¡c|||e vo·|o||c· c| :|·o ||e
¼·||e||·e me||cJ
C05622132.indd 61 10/18/05 7:05:05 PM
62 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
TO S£L£CT lkOM A LlST Ol OPTlONS lN lNT£LLlS£NS£
lt is important to note that
regardIess of your current sticky
tab seIection, lnteIIiSense wiII
aIways switch in reaI time to pro-
vide the best match for what you
are typing.
ABG 8
$
|¦ ¦|e soJ·ce code |s ro¦ v|s|b|e. |Js¦ c||c| or
¦|e ¦ab ove· ¦|e code ed|¦o· .|e·e yoJ see
¦|e ¦||erane |o·n1vb /dd a re. ||re |r ¦|e
|:||c·1_´||.| ever¦ ard ¦ype Messageßox.Show
ard ¦|er ¦ype a |e¦¦ pa·er¦|es|s ('(') T|e
|r¦e|||Serse \|rdo. opers ard s|o.s ¦|e·e a·e ´1
poss|b|e va·|a¦|ors ¦o· \essage|o.S|o.
%
Sc·o|| ¦|·oJg| ¦|e ||s¦ o¦ op¦|ors Js|rg ¦|e Jp
o· do.r a··o.s or yoJ· |eyboa·d Se|ec¦ ¦|e
op¦|or |der¦|¦|ed by ¦|e '1' o¦ ´1' |r ¦|e ye||o.
·ec¦arg|e. .||c| |s ca||ed a ¦oo|¦|p
&
Conp|e¦e ¦|e ¦o||o.|rg ||re o¦ code so ¦|a¦ |¦
|oo|s |||e
Message8ox.5hoW1"he]]o Aga1n"}
'
|J||d ard e.ecJ¦e ¦|e app||ca¦|or \|er yoJ
c||c| ¦|e bJ¦¦or yoJ s|oJ|d see ¦|e |e||o
\o·|d s¦·|rg ard ¦|er a d|a|og bo. s|oJ|d s|o. Jp
.|¦| ¦|e nessage |e||o /ga|r
lnteIIiSense liItering. kemoving the "Uncommon¨
¥ou mighL have noLiced LhaL when Lhe lnLelliSense window appears, you have Lwo Labs aL
Lhe boLLom. Common and All. 1hese are called "sLicky Labs" because Lhey reseL Lhe deíaulL
view when you click Lhem. 1hese Labs are parL oí a new íeaLure called lnLelliSense FilLering.
WiLh lnLelliSense FilLering, you can adjusL Lhe level oí deLail lnLelliSense supplies in Lhe code
ediLor. When you selecL Lhe Common Lab, lnLelliSense íilLers ouL mosL uncommon and rarely
used opLions, bringing you only Lhe mosL common opLions based on Lwo Lhings. Lhe mosL
plausible choices íor Lhe conLexL or Lhe one used mosL írequenLly in Lhe applicaLion.
lnteIIiSense Code Snippets. The Time Saver
1he íinal lnLelliSense íeaLure is lnLelliSense code snippeLs. Code snippeLs are reusable pieces
oí code LhaL enable you Lo compleLe a wide range oí Lasks wiLhouL Lyping a single line oí
code. 1here are abouL 200 code snippeLs available in visual 8asic 2005 Fxpress FdiLion. 1hey
are caLegorized by íuncLion and cover a wide varieLy oí Lasks.
Q Common programming sLrucLures such as excepLions and 1ry-CaLch blocks, and so íorLh.
(We'll see more abouL Lhese laLer in Lhis chapLer.)
Q Sending e-mail messages
Q Resizing a íorm
C05622132.indd 62 10/18/05 7:05:06 PM
Chapter 5. Creating Your lirst luII windows AppIication 63
Q visual 8asic language elemenLs such as lí, For, and While sLaLemenLs wiLh Lhe correcL synLax
Figure 5-8 shows a glimpse oí Lhe code snippeLs íirsL-level menu choices. All code
snippeLs are made wiLh cusLomizable íields, which means LhaL Lhey conLain íields LhaL are
replaceable wiLh code elemenLs írom your own applicaLions. Coing íorward, you will be able
Lo download addiLional code snippeLs írom various sources such as MicrosoíL MSDN® Web
siLe, online communiLies, and oLher .NF1 vendors. ¥ou will also be able Lo add your own
code snippeLs Lo Lhe code snippeLs library Lo íulíill your needs in oLher projecLs.
;^\jgZ*"-
|·|e|||'e·e .cJe ·|¡¡e| me·:
See the "linding AdditionaI
lnformation¨ section at the end of
this chapter for more information
on code snippets.
MOk £ l Nl O
How to lnvoke Code Snippets
¥ou can invoke Lhe code snippeLs in Lhe code ediLor Lwo diííerenL ways. by righL-clicking and
choosing lnserL SnippeLs or by using Lhe keysLroke combinaLion ¯-1ab
TO US£ COD£ SNlPP£TS
$
|s|rg ¦|e p·ev|oJs ¦es¦ p·o|ec¦. go bac| ¦o ¦|e code ed|¦o· |r ¦|e |:||c·1_´||.| ever¦ ard ca|| Jp ¦|e
InteIIìSense Code Snìppets nerJ. Js|rg e|¦|e· ne¦|od desc·|bed above
%
Cr ¦|e ¦|·s¦|eve| nerJ. doJb|ec||c| Common Code Patterns. doJb|ec||c| CondìtìonaIs and Loops.
ard ¦|er se|ec¦ ¦|e |c· |ex| s¦a¦ener¦ |oo| a¦ ||gJ·e 'º ¦o ge¦ a ¦ee| ¦o· .||c| nerJ c|o|ces yoJ
s|oJ|d |ave or yoJ· sc·eer DoJb|ec||c| or ¦|e s¦a¦ener¦ ¦o |rse·¦ ¦|e code |r ¦|e code ed|¦o·
C05622132.indd 63 10/18/05 7:05:06 PM
64 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
&
Crce yoJ se|ec¦ ¦|e |c· |ex| s¦a¦ener¦. a gere·|c ¦enp|a¦e ¦o· ¦|a¦ |argJage cors¦·Jc¦ appea·s
|e¦e· ¦o ||gJ·e '1J T|e g·eer ¦|e|ds |r ¦|e 'be¦o·e' sc·eers|o¦ (||gJ·e '1Ja) a·e ·ep|acener¦ ¦|e|ds
p·epopJ|a¦ed .|¦| sone de¦aJ|¦ va|Jes ¦|a¦ yoJ car nod|¦y |e¦o·e yoJ go ¦o ¦|e re.¦ s¦ep. ed|¦ ¦|e code
¦o na¦c| ¦|e 'a¦¦e·' sc·eers|o¦ (||gJ·e '1Jb)
;^\jgZ*".
´cJe ·|¡¡e| w||||· ||e V|:o| |o|.
|o·o:ooe me·:
;^\jgZ*"&%
(o· |e|c·e o·J (|· /||e· |cc| |c· ||e |c· |ex| .cJe ·|¡¡e|
'
|J||d ard e.ecJ¦e yoJ· app||ca¦|or by p·ess|rg |'. ¦|er c||c| ¦|e bJ¦¦or or ¦|e d|sp|ayed ¦o·n ¦o e.e
cJ¦e ¦|e code sr|ppe¦ yoJ've |Js¦ |rse·¦ed |r ¦|e secord ¦e.¦ bo.. yoJ s|oJ|d see ¦|e rJnbe·s 1
¦|·oJg| 1J sepa·a¦ed by connas T||s oJ¦pJ¦ |s ¦|e ·esJ|¦ o¦ ¦|e |o· Ne.¦ s¦a¦ener¦ |oop|rg 1J ¦|nes.
add|rg ¦|e |rde. va|Je ard a conna ¦o ¦|e ¦e.¦ bo. .|¦| eac| |oop |r ¦||s sanp|e. ¦|e |rde. |s '|'
keaI-Time £rror Detection and Correction
We all learn diííerenLly and aL diííerenL speeds. We also may make misLakes when we are
learning someLhing new, especially when we wriLe code! So visual 8asic 2005 includes a íea-
Lure LhaL gives you real-Lime compiler íeedback and suggesLs correcLive acLions Lo íix mis-
Lakes. ln a sense, Lhe visual 8asic compiler is always working in Lhe background while you
are coding. Whenever you make a misLake, you'll see a blue squiggle under Lhe íaulLy code.
Or, ií Lhe code is noL necessarily wrong buL Lhere is a poLenLial problem, you'll see a green
squiggle under Lhe code. lí you move your mouse over Lhe squiggle, a TooITip will appear
C05622132.indd 64 10/18/05 7:05:07 PM
Chapter 5. Creating Your lirst luII windows AppIication 65
Lo indicaLe why your code was rejecLed by Lhe compiler. ln some cases, a SmarL 1ag will
appear under Lhe lasL leLLer oí Lhe íaulLy code. 1he SmarL 1ag looks a liLLle diííerenL Lhan
usual and is represenLed by a small yellow line surronded by a red box like Lhis.
lí you move Lhe mouse, an exclamaLion poinL in a red circle will appear.
1his indicaLes LhaL visual 8asic has íound a poLenLial íix íor your problem. Click Lhe down
arrow nexL Lo Lhe exclamaLion poinL and you'll be presenLed wiLh one or more poLenLial íixes
íor your problem. LeL's Lry iL wiLh our LesL projecL.
TO US£ k£AL-TlM£ £kkOk D£T£CTlON
$

|r ¦|e |:||c·1_´||.| ever¦. add ¦|e ¦o||o.|rg ||re o¦ code
D1m foo as lnfeg¡
%
\|sJa| |as|c |rd|ca¦es a p·ob|en .|¦| ¦|e .o·d |·|eo· .|¦| a b|Je soJ|gg|e \ove yoJ· noJse ove· ¦|e
Sna·¦ Tag
&

\|er yoJ see ¦|e e.c|ana¦|or po|r¦ |r a ·ed c|·c|e. c||c| ¦|e do.r a··o.
'
/s yoJ car see |r ||gJ·e '11. yoJ'·e p·eser¦ed .|¦| a ||s¦ o¦ po¦er¦|a| ¦|.es ¦o· ¦|e e··o· C¦ coJ·se.
¦|e ||s¦ o¦ po¦er¦|a| ¦|.es |s based or yoJ· cJ··er¦ cor¦e.¦ T|e e··o· co··ec¦|or ¦ea¦J·e .||| a|.ays ¦·y
¦o ¦|rd ¦|e bes¦ so|J¦|or Se|ec¦ C|arge |·|eo· ¦o |·|eoe·
(
T|e b|Je soJ|gg|e s|oJ|d d|sappea· / g·eer soJ|gg|e s|oJ|d appea· Jrde· ¦|e va·|ab|e rane |cc
/ga|r. nove yoJ· noJse ove· ¦|e .o·d ¦|a¦ |as ¦|e soJ|gg|e / Sna·¦ Tag .a·rs ¦|a¦ ¦|e |oca|
va·|ab|e '¦oo' |s ro¦ Jsed ary.|e·e e|se |r ¦|e app||ca¦|or \a·r|rgs a·e ro¦ c·|¦|ca| ard .or'¦ s¦op ¦|e
app||ca¦|or ¦·on e.ecJ¦|rg. so yoJ car |gro·e ¦||s .a·r|rg
;^\jgZ*"&&
||| c| ¡c|e·||o| ||xe |c· ||e .:··e·| e··c·
C05622132.indd 65 10/18/05 7:05:08 PM
66 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
We've jusL seen one case where Lhe real-Lime error correcLion can inLervene, buL iL can
inLervene on many oLher occasions. ldenLiíying Lhe errors wiLh Lhe background compiler is easy
because iL is auLomaLic. SomeLimes, Lhe harder parL is knowing whaL Lo do wiLh errors. 1he error
correcLion íeaLure also helps you wiLh Lhis by showing you more Lhan one opLion Lo íix Lhe
problem, as shown in above in Lhe inLeger example. Now leL's look aL a diííerenL example.
TO CHOOS£ lkOM S£V£kAL COkk£CTlON OPTlONS
$

/dd ¦|e ¦o||o.|rg ||re o¦ code |r ¦|e |:||c·1_´||.| ever¦ code
ReadOn]y ba¡ As Doub]e
/ b|Je soJ|gg|e s|oJ|d appea· Jrde· |eadCr|y
%
\ove yoJ· noJse ove· ¦|e Sna·¦ Tag |¦ |rd|ca¦es ¦|a¦ |eadCr|y |s ro¦ va||d or a |oca| va·|ab|e
dec|a·a¦|or
&
C||c| ¦|e do.r a··o. ¦o ¦|e ·|g|¦ o¦ ¦|e e.c|ana¦|or po|r¦ \|sJa| |as|c sJgges¦s a ¦|. ard |||Js¦·a¦es
¦|e co··ec¦|or by s¦·|||rg oJ¦ ¦|e |rco··ec¦ code ard |rse·¦|rg ¦|e co··ec¦ code (See ||gJ·e '1´) |r
¦||s case. yoJ .ar¦ ¦o accep¦ ¦|e c|arge |¦ |¦ na|es serse |r yoJ· app||ca¦|or |¦ yoJ .ar¦ ¦o accep¦ ¦|e
c|arge. |Js¦ c||c| or ¦|e b|Je sJgges¦|or
;^\jgZ*"&'
´cJe o:|c.c··e.||c· exom¡|e
Oh My, My . . . Bn ls Great
So íar, we've seen quiLe a íew nice íeaLures LhaL are parL oí visual 8asic 2005 Fxpress FdiLion.
WhaL we've noL yeL seen and explored is Lhe enormous number oí classes íound in Lhe .NF1
Framework. 1he .NF1 Framework is vasL, and someLimes íinding Lhe good classes and meLh-
ods Lo solve a problem is diííiculL. Keeping Lhe same producLiviLy goal and mindseL, Lhe
C05622132.indd 66 10/18/05 7:05:09 PM
Chapter 5. Creating Your lirst luII windows AppIication 67
visual 8asic 1eam decided LhaL Lhey had Lo assemble Lhe mosL common classes írom Lhe
.NF1 Framework in one collecLion. 1hey decided Lo creaLe Lhis collecLion in a Lask-orienLed
way so LhaL programmers can use iL Lo accomplish Lhe mosL common programming Lasks.
FurLhermore, Lhey decided LhaL Lo provide easy access Lo Lhose classes, Lhey would creaLe a
new namespace and wrap Lhose common classes and meLhods inLo a new seL oí caLegorized
classes called Lhe Mv namespace. 8asically, Lhe Mv namespace gives Lhe programmer access
Lo Lwo diííerenL areas. .NF1 Framework classes and parLs oí a projecL (such as Lhe íorms, Lhe
applicaLion seLLings and resources, and so íorLh.) LeL's look aL Lhe lisL oí high-level classes
íound in Lhe Mv namespace.
;^\jgZ*"&(
V|:o| ·e¡·ee·|o||c· c| ||e ||·||eve|
me·: |c· ||e |/ ·ome¡o.e
lf you are not using windows XP,
do a search to find a .wav fiIe on
your hard drive and enter the path
as the argument to the Iine of
code shown above.
6 4HG < BA
N ApplicaLion
N CompuLer
N Forms
N Resources
N SeLLings
N User
Figure 5-í8 is a visual represenLaLion oí Lhese high-level classes írom Lhe code ediLor.
From Lhe code ediLor window, you simply have Lo Lype Lhe keyword Mv íollowed by a
period (".") Lo gain access Lo Lhe Mv namespace.
For insLance, now you can play a .wav íile simply by using Lhe íollowing synLax.
My.Compufe¡.Aud1o.P]ay1¨c:\W1ndoWs\med1a\W1ndoWs XP 5fa¡fup.Wav¨}
1esL Lhe above line oí code in Lhe 3µttcn1_Clicl evenL Lo play Lhe Windows XP sLarLup
sound. 1hen build your applicaLion and execuLe iL. lL's LhaL easy! lL's inLuiLive and a loL easier
Lo code. 8eíore Lhe exisLence oí Lhe Mv namespace, Lo geL Lhe same íuncLionaliLy you would
have had Lo use eiLher Win82
®
APls or DirecL Sound írom Lhe MicrosoíL DirecLX
®
íamily
and enLer many more lines oí code.
For more complicaLed Lasks such as deLecLing neLwork connecLiviLy, applicaLion evenLs,
and so íorLh, you gain a loL oí producLiviLy using Lhe Mv namespace. 8eíore, you would
have had Lo wriLe nearly 200 lines oí raw .NF1 Framework code and maybe more! Now
you simply have Lo íind Lhe consLrucL in Lhe Mv namespace, selecL iL, and add Lhe conLexL
oí your applicaLion. For easier Lasks, you mighL noL save a loL oí lines oí code using Lhe Mv
namespace, buL you cerLainly save Lime oLherwise spenL researching corresponding classes in
Lhe .NF1 Framework.
C05622132.indd 67 10/18/05 7:05:09 PM
68 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
what Types of Tasks Can Bn HeIp Me with?
You can extend the Bn namespace
and cIasses to add your own
methods. They'II aIso show up
in lnteIIiSense. See the "linding
AdditionaI lnformation¨ section
at the end of this chapter for a
Iink to a MSDN white paper that
expIains in detaiI how to do this.
MOk £ l Nl O
1he rename íeaLure íound in visual 8asic 2005 is quiLe useíul. lL provides you, Lhe program-
mer, wiLh an easy, auLomaLic, and eííecLive way oí changing a symbol's name everywhere Lhe
symbol is reíerenced in Lhe code. 1he variables, conLrols, and any oLher iLems in your appli-
caLions can be updaLed Lo meaningíul names using Lhe renaming symbol íuncLionaliLy.
what Can You kename?
So íar in Lhe LesL projecL we've worked in, we have noL paid aLLenLion Lo Lhe conLrols' names
because we didn'L have Lo wriLe much code and because Lhe projecL was a quick proLoLype
Lo LesL new íeaLures. AL Lhis poinL, our conLrols are all named someLhing like 1exLboxí,
1exLbox2, Labelí, and so on. 1haL's okay íor whaL we've been doing, buL when you develop
íull applicaLions, you always wanL Lo give meaningíul names Lo your conLrols and variables
so LhaL your code becomes selí-documenLed and easier Lo read and mainLain.
kenaming
Here are jusL a íew oí Lhe common Lasks you
can expecL Lo íind in Lhe Mv namespace.
N Display an applicaLion splash screen
N CeL your compuLer name
N CeL neLwork seLLings
N veriíy LhaL a Web siLe is up and running
N Reading compleLely a LexL íile inLo
a sLring
N Send someLhing Lo prinL on Lhe deíaulL
prinLer
N CeL applicaLion seLLings
Oí course, Lhe Mv namespace is noL an
answer Lo all problems. lL's a greaL soluLion
íor a number oí common scenarios, buL Lo
solve some oLher problems, you mighL have
Lo call Lhe diííerenL classes írom Lhe .NF1
Framework direcLly and wriLe more lines
oí code.
Here is a Iist of what can be refer-
enced as a symboI.
N Type definitions such as
cIasses, moduIes, structures,
enums, deIegates, interfaces
N Type members such as meth-
ods, properties, events
N Member variabIes of cIasses,
moduIes, structures, enums
N LocaI variabIes inside func-
tions and properties
MOk £ l Nl O
C05622132.indd 68 10/18/05 7:05:10 PM
Chapter 5. Creating Your lirst luII windows AppIication 69
How and where to Use the kename leature
So íar in our LesL projecL, we have deíined some LexL boxes, labels, and oLher conLrols, buL
Lhey all have Lheir auLogeneraLed names (e.g., 1exLboxí or Labelí). We can use Lhe rename
íeaLure Lo give meaningíul names Lo Lhese conLrols. 1he renaming íeaLure can be used írom
Lhree diííerenL places wiLhin Lhe lDF. in Lhe ProperLies window, direcLly in Lhe code, and in
Lhe SoluLion Fxplorer.
1he íirsL place we can use Lhe renaming íeaLure is in Lhe ProperLies window aL design
Lime. So íar in our LesL projecL, we've used Lhe íorm name Formí. ln Lhe nexL exercise, we'll
rename Formí Lo 1esLProjecLForm. 1he expecLaLion is LhaL Lhis change geLs propagaLed
LhroughouL Lhe code in Lhe projecL. 8uL jusL Lo see how Lhe íuncLionaliLy works, we'll look
inLo all íiles where Lhe Formí symbol is used.
TO US£ TH£ k£NAM£ l£ATUk£
$

|r So|J¦|or |.p|o·e·. c||c| ¦|e Show AII fìIes bJ¦¦or
`oJ s|oJ|d see a |o¦ o¦ re. e|ener¦s s|o.|rg Jp |r ¦|e p·o|ec¦ \e'|| e.an|re ¦|e code ¦|a¦ \|sJa| |as|c
´JJ' |.p·ess |d|¦|or aJ¦ona¦|ca||y gere·a¦ed .|er .e nar|pJ|a¦ed cor¦·o|s or ¦|e des|gr sJ·¦ace
%
|.pard |o·n1vb ard ·|g|¦c||c| ¦|e ¦||e rane |c·m1|e|o·e·v| ard ¦|er se|ec¦ Vìew Code |oo| a¦
||gJ·e '14 ¦o· a v|sJa| ·ep·eser¦a¦|or o¦ .|a¦ yoJ s|oJ|d see
&
No. |e¦ ne |r¦·odJce yoJ ¦o a Jse¦J| ¦ea¦J·e Sea·c| \|¦| ¦|e |o·n1Des|gre·vb soJ·ce code |r ¦|e
code ed|¦o·. p·ess CtrI+f ard ¦|er ¦ype forml |r ¦|e ||rd \|a¦ ¦e.¦ bo. |e¦o·e yoJ c||c| ¦|e ||rd
Ne.¦ bJ¦¦or. na|e sJ·e ¦o se|ec¦ Current Project ¦·on ¦|e |oo| |r ||s¦ bo.
DDEIZgb^cdad\n
Althcµçh this bccl is nct
cn OOP bccl. we´ll certcinlv
µse mcnv cf thcse ccnstrµcts in
the recl cµµliccticn we´ll bµilJ.
We've tclleJ cbcµt OOP in
Chcµter 1 cnJ Chcµter 4. bµt if
vcµ wcnt tc lecrn mcre cbcµt
this µcrcJiçm. vcµ ccn çc bccl
tc the Stcrt Pcçe. lf it´s nct vis-
ible. jµst clicl the view menµ.
Other WinJcws. cnJ then select
Stcrt Pcçe. ln the Stcrt Pcçe.
clicl cn the Hcw Jc l . . .?
hvµerlinl in the Cettinç StcrteJ
secticn. Once vcµ´re in the helµ
svstem. vcµ´ll see c secticn
cclleJ ´lecrninç the visµcl 3csic
lcnçµcçe.¨ 1he first chcice is
OOP. Clicl cnJ recJ whenever
vcµ wcnt tc lecrn mcre cbcµt
OOP cnJ the visµcl 3csic lcn-
çµcçe. lt´s nct c ccmµlete OOP
bccl. bµt it´s mcre thcn encµçh
fcr whct we cre Jcinç in this
text.
;^\jgZ*"&)
´e|||·o |c ||e o:|coe·e·o|eJ .cJe |c·
||e |c·m e|eme·|
C05622132.indd 69 10/18/05 7:05:10 PM
70 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
||gJ·e '1' s|o.s |o. ¦|e sea·c| s|oJ|d be cor¦|gJ·ed
;^\jgZ*"&*
¯|e eo·.| J|o|co |cx
'
Sea·c| ¦o· a|| occJ··erces o¦ |o·n1 |r ¦|e code by c||c||rg ¦|e fìnd Next bJ¦¦or `oJ s|oJ|d see ¦|a¦
¦|e sea·c| goes ¦|·oJg| ¦oJ· d|¦¦e·er¦ ¦||es |c·m1v|. |c·m1|e|o·e·v|. /¡¡||.o||c·|e|o·e·v|. ard
/¡¡||.o||c·m/o¡¡
Crce a|| |rs¦arces o¦ |o·n1 |ave beer ¦oJrd. a d|a|og bo. .||| d|sp|ay a nessage say|rg ¦|a¦ ¦|e sea·c| |s
conp|e¦e ard ¦|a¦ ¦|e·e a·e ro no·e occJ·erces |e¦¦ based or yoJ· sea·c| c·|¦e·|a
(
No. ¦|a¦ a|| |rs¦arces o¦ |o·n1 |ave beer |der¦|¦|ed. .e car ·erane |¦ ¯e||·c|e.||c·m To do ¦|a¦.
se|ec¦ ¦|e forml.vb [Desìgn] ¦ab ¦o ·e¦J·r ¦o ¦|e des|gre· sJ·¦ace T|er c||c| ¦|e ¦|¦|e ba· ¦o se|ec¦
¦|e ¦o·n
)
|r ¦|e |·ope·¦|es .|rdo.. be sJ·e ¦|e ¦o·n cor¦·o| raned |c·m1 |s se|ec¦ed \od|¦y ¦|e (Nane)
p·ope·¦y by c|arg|rg |c·m1 ¦o 1estProjectform |·ess fnter ¦o beg|r ·eran|rg / sna|| |oJ·g|ass
s|oJ|d be d|sp|ayed .|||e ¦|e ·eran|rg |s |r p·ocess
*
No. ·epea¦ ¦|e sea·c| ¦·on S¦eps ` ard 4 ard yoJ'|| see ¦|a¦ ¦|e or|y occJ··erce |e¦¦ |s a s¦·|rg ¦|a¦
co··espords ¦o ¦|e |o·n cor¦·o| ¦e.¦ p·ope·¦y (|e. ¦|¦|e ba· rane)
+
`oJ car a|so ·erane a synbo| d|·ec¦|y |r ¦|e code |r |o·n1vb. p|ace ¦|e cJ·so· ary.|e·e |r ¦|e
.o·d 1ext3cx1 |r ¦|e ¦o||o.|rg ||re o¦ code
Me.1exf8ox1.1exf = ¨he]]oWo¡]d¨
1his is c çccJ µcint tc intrc-
Jµce c new bµttcn frcm the
Sclµticn lxµlcrer tcclbcr. 1he
Shcw All liles bµttcn lccls lile
this in the tcclbcr.
ln visµcl 3csic 200S.
cnJ esµecicllv in the lxµress
lJiticn. the Jesiçn µhilcscµhv
hcs clwcvs been µrcJµctivitv.
1herefcre. the Sclµticn lxµlcrer
interfcce is nct filleJ with files
cnJ settinçs thct mcst µsers
wcµlJ nct neeJ cn c Jcilv
bcsis. 3µt scme elements cre
inclµJeJ in the interfcce fcr
lecrninç µµrµcses cr fcr when
vcµ cre sclvinç ccmµlex µrcb-
lems cnJ mcv wcnt tc Jirectlv
Jiç in cll the files cssccicteJ
with vcµr µrcject. Scme files cre
çenercteJ cµtcmcticcllv when
vcµ µerfcrm ccticns in the lDl
cnJ in mcst sitµcticns it´s jµst
fine lile thct. vcµ Jc nct neeJ
tc ccre cbcµt cll the Jetcils cll
the time. ln sitµcticns where
vcµ wcnt mcre ccntrcl. vcµ ccn
clicl the Shcw All liles bµttcn
cnJ çc Jirectlv tc the ccmµc-
nent cf vcµr chcice.
C05622132.indd 70 10/18/05 7:05:11 PM
Chapter 5. Creating Your lirst luII windows AppIication 7l
,
||g|¦c||c| ard se|ec¦ Rename. a d|a|og bo. |||e ¦|e ore |r ||gJ·e '1ba appea·s |ep|ace 1ext3cx1
.|¦| tbMesscçe. ¦|e d|a|og bo. s|oJ|d ro. |oo| |||e ¦|e ore |r ||gJ·e '1bb
;^\jgZ*"&+
|e·ome J|o|co |cxe (o· |e|c·e o·J
(|· o||e· o ·e·ome
$# C||c| OK ¦o ·ep|ace a|| occJ·erces o¦ ¯ex||cx1 .|¦| ¦b\essage
/ sea·c| s|oJ|d ¦|rd or|y ore conner¦ ard ¦|e Nane p·ope·¦y o¦ ¦|e ¦e.¦ bo. cor¦·o| .|¦| ¦|e s¦·|rg
'Te.¦|o.1' |¦'s ¦|a¦ easy |nag|re |o. nJc| ¦|ne yoJ .oJ|d save Js|rg ¦|e |erane ¦ea¦J·e |¦ yoJ |ad 1J
¦||es .|¦| |Jrd·eds o¦ ||res o¦ code No¦ or|y .oJ|d yoJ be sJ·e ¦o ¦|rd eve·y occJ··erce. ¦|e code .oJ|d
be a |o¦ eas|e· ¦o ·ead
$$
T|e ¦||·d .ay ¦o ·erane a synbo|. ard |r ¦||s case or|y ¦o· p·o|ec¦ e|ener¦s. |s ¦o do |¦ d|·ec¦|y |r
So|J¦|or |.p|o·e· |ver ¦|oJg| ea·||e· .e c|arged ¦|e |o·n1 va·|ab|e |r¦o sone¦||rg no·e near|rg
¦J|. ¦|e ¦||e rane |o·n1 |asr'¦ c|arged becaJse |¦'s ro¦ |r ¦|e soJ·ce code. |¦'s |r ¦|e So|J¦|or |.p|o·e· ard
cor¦a|red |r ¦|e p·o|ec¦. so |¦'s s¦||| |o·n1vb |o· cors|s¦ercy. ·|g|¦c||c| ¦|e ¦||erane |o·n1vb |r ¦|e
So|J¦|or |.p|o·e·. ¦|er se|ec¦ Rename ard c|arge ¦|e ¦||erane ¦o 1estProjectformOtherName.vb T.o
¦||rgs .||| |apper ¦|·s¦. yoJ'|| see ¦|a¦ ¦|e ¦||erane ard a|| deperder¦ ¦||eranes a·e aJ¦ona¦|ca||y c|arged
¦o ¦|e re. rane. ard secord. yoJ'|| see ¦|a¦ a|| ·e¦e·erces ¦o Tes¦|·o|ec¦|o·n a·e ro. c|arged ¦o
Tes¦|·o|ec¦|o·nC¦|e·Nane `oJ car ve·|¦y ¦||s ·eran|rg c|arge by p·ess|rg C¦·|÷| ard pe·¦o·n|rg a
sea·c| or ¦|e o|d rane (|o·n1vb)
C05622132.indd 71 10/18/05 7:05:12 PM
72 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
Common windows ControIs
l will noL spend a loL oí Lime here explaining all Lhe deLails and properLies oí each conLrol in
Lhe Loolbox. 1his book is noL a reíerence abouL Windows Forms programming. OLher books
do a greaL job wiLh LhaL Lopic. However, 1able 5-í provides a quick inLroducLion Lo Lhe mosL
common conLrols you will íind in mosL Windows ApplicaLions.
VisuaI representation Name Description
8uLLon lndicaLes LhaL Lhe user makes a decision
and wanLs Lo communicaLe his acLion.
ln your applicaLion, a buLLon clicked by
Lhe user Lriggers an evenL LhaL your code
needs Lo handle.
1exL8ox 1he LexL box is used Lo geL user inpuL.
On Lhe screen iL can be a single or mulLi-
line conLrol. lL can also provide password
characLer masking ií you need Lhis
behavior in your applicaLion. lL's a good
choice Lo inpuL iníormaLion LhaL is noL
resLricLive in choices, íor insLance a
8oolean decision like yes/no, on/oíí, or a
lisL oí speciíic choices like a lisL oí counLry
names. lL's good íor names, addresses,
phone numbers, URLs, eLc.
Label 1he label is usually simple LexL used Lo
describe oLher conLrols. lL is noL an inLer-
acLive conLrol.
´c·||·:eJ c· ·ex| ¡ooe
C05622132.indd 72 10/18/05 7:05:13 PM
Chapter 5. Creating Your lirst luII windows AppIication 73
VisuaI representation Name Description
Radio8uLLon MulLiple choices are oííered buL Lhe user
can only pick one írom Lhe lisL. LeL's say
you have an applicaLion and you wanL Lo
provide Lhe opLion Lo prinL in grayscale
or color. ¥ou could use Lwo radio buLLons
Lo selecL Lhe desired meLhod.
Check8ox A Check8ox is greaL íor 8oolean choices
(e.g., on/oíí, yes/no, eLc.). lL can also be
used in a group oí check boxes Lo indi-
caLe characLerisLics oí a single enLiLy. For
insLance, in a car ordering Lool LhaL is
parL oí a dealership applicaLion you could
have check boxes íor all Lhe car charac-
LerisLics. (i.e., AM/FM radio, CD changer,
heaLed seaLs, meLallic painL, eLc.).
Combo8ox A Combo8ox is a combinaLion oí a LexL
box and a drop-down lisL wiLh valid
choices. lL's greaL Lo display an ediLable
LexL box wiLh a lisL oí permiLLed values.
¥ou can have auLocompleLe and Lhe val-
ues can be sorLed. 1he values can come
írom sLaLic enLry or írom oLher sources oí
daLa like a daLabase. For insLance, a good
example is selecLing a sLaLe. ¥ou can
eiLher enLer Lhe sLaLe name or selecL iL
írom Lhe lisL oí possible values.
´c·||·:eJ |·cm ¡ooe ¯¯
´c·||·:eJ c· ·ex| ¡ooe
C05622132.indd 73 10/18/05 7:05:13 PM
74 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
1here are many more conLrols Lhan Lhose shown here. 8uL Lhis Lable should make iL
clear LhaL Lhere are a pleLhora oí conLrols available Lo períorm many Lasks. 1o save Lime and
eííorL, you can usually íind a conLrol Lo provide Lhe resulLs you wanL wiLh very liLLle eííorL. lL
is especially desirable ií Lhe conLrol you pick can resLricL choices or how Lhe daLa is selecLed
wiLhouL having Lo períorm any oLher validaLion. ln soíLware developmenL, always keep Lhe
80/20 rule in mind. 80 percenL oí resulLs íor 20 percenL eííorL.
VisuaI representation Name Description
LisL8ox A LisL8ox is a shorL lisL oí valid choices íor
Lhe domain Lhis componenL represenLs.
1his conLrol is greaL when Lhere is a lisL oí
possible choices LhaL is noL Loo big
in number. lL does noL allow Lhe user Lo
enLer LexL buL leLs Lhe user selecL one or
more Lhan one choice by using C1RL
or SHlF1.
1ool1ip 1he 1ool1ip conLrol is helpíul íor display-
ing iníormaLion abouL a conLrol when a
user holds Lhe mouse over Lhe conLrol.
NumericUpDown 1his conLrol is really useíul when you
wanL Lhe user Lo selecL a numerical value
írom a predeíined seL oí numbers. lL
allows Lhe user Lo selecL a single numeri-
cal value írom Lhe lisL using Lhe up and
down buLLon Lo incremenL and decre-
menL Lhe number. lL could be períecL Lo
íorce Lhe user Lo pick up a numerical
value íor a year componenL oí a daLe.
´c·||·:eJ |·cm ¡ooe ¯¯
C05622132.indd 74 10/18/05 7:05:14 PM
Chapter 5. Creating Your lirst luII windows AppIication 75
what Happens when an £vent ls Triggered?
All Windows applicaLions are evenL-driven. 1his means LhaL whenver you selecL a menu iLem,
click a buLLon or even move írom one LexL box Lo anoLher, you are generaLing an evenL.
8locks oí code aLLached Lo each oí your acLions execuLe as you work. FvenLs are generaLed
noL only by your acLions, buL also by Lhe surrounding environmenL, namely Windows iLselí
or by oLher exLernal sources. 1o undersLand whaL "oLher exLernal sources" means, Lhink
abouL any lnLerneL messenger applicaLion like MSN Messenger. When you chaL wiLh some-
one and exchange daLa back and íorLh, you are acLually generaLing evenLs. ln nonLechnical
Lerms, daLa incoming írom your íriend over Lhe lnLerneL is an evenL.
1hese evenLs exisL íor a mulLiLude oí acLions you usually do wiLhouL realizing Lhey are
evenLs. Some evenLs are handled íor you by auLogeneraLed code like clicking Lhe red X in
Lhe righL corner oí an applicaLion and some oLhers need Lo be handled by your code.
WiLh Lhis secLion, we'll sLarL Lo work on wiring source code Lo evenLs. For pracLice, we will
wire Lwo objecLs írom our LesL projecL. 8eíore beginning, use whaL you've learned so íar Lo
make Lhe LesL projecL look like Figure 5-í7.
;^\jgZ*"&,
´:|cme· |·|c·mo||c· |c·m
To add the menus and tooIbar
buttons, go to the tooIbox in the
Menus & TooIbars category and
add a MenuStrip controI and a
TooIStrip controI to the form.
Next, seIect each controI, open the
smart tag menu, and seIect lnsert
Standard ltems.
ABG 8
C05622132.indd 75 10/18/05 7:05:14 PM
76 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
When an evenL is Lriggered, Lhe code LhaL is wired Lo handle Lhe evenL is execuLed. lí
Lhere is no code aLLached Lo a parLicular evenL, Lhen noLhing happens. Our applicaLion is
basically in LhaL sLage righL now. (FxcepL íor Lhe buLLon LhaL was doing some work íor us as
shown previously.) We will add some íuncLionaliLy Lo our LesL projecL applicaLion by wiring
Lhe Save menu choice and Lhe Loolbar Save buLLon Lo Lhe source code LhaL will save all Lhe
conLenL oí Lhe LexL boxes Lo a simple LexL íile in Lhe currenL direcLory. 8ecause Lhe save and
open íile dialogs are sLandard and also Lo geL a consisLenL íeel Lo applicaLions, Lhe visual
8asic developmenL Leams decided Lo wriLe save and open conLrols and make Lhem available
Lo you. We'll Lake advanLage oí Lhis shorLcuL in our exercise.
TO wlk£ SOUkC£ COD£ TO £V£NTS
$
D·ag ¦|e SavefìIeDìaIog cor¦·o| or¦o ¦|e ¦o·n ¦·on ¦|e ¦oo|bo. d|a|ogs ca¦ego·y T||s cor¦·o| |as ro
des|gr ¦|ne ·ep·eser¦a¦|or. |¦ .||| appea· or|y |r ¦|e conporer¦ ¦·ay. ¦|e g·ay sec¦|or be|o. ¦|e
des|gre· sJ·¦ace See ||gJ·e '18 ¦o· ¦|e |oca¦|or o¦ ¦|e Save|||eD|a|og
;^\jgZ*"&-
|e|o· ||me ·e¡·ee·|o||c· c| ||e
'ove|||e||o|co
C05622132.indd 76 10/18/05 7:05:15 PM
Chapter 5. Creating Your lirst luII windows AppIication 77
\e'|| Jse ¦|e Save|||eD|a|og ¦o .|·e ¦|e c||c| ever¦ ¦o bo¦| ¦|e Save bJ¦¦or or ¦|e ¦oo|ba· ard ¦o ¦|e Save
c|o|ce |r ¦|e |||e nerJ To |ave ¦|e sane ope·a¦|or pe·¦o·ned .|er e|¦|e· ever¦ occJ·s. .e'|| .·|¦e a
b|oc| o¦ code ca||ed a ne¦|od T|e b|oc| o¦ code .||| pe·¦o·n ¦|e sane ope·a¦|or .|e¦|e· |¦ |s ¦·|gge·ed
by ¦|e bJ¦¦or or ¦|e ¦oo|ba· o· by ¦|e nerJ se|ec¦|or
%
C||c| ¦|e ||¦¦|e b|Je d|s| |cor or ¦|e ¦oo|s¦·|p ¦o se|ec¦ |¦ C|ec| ¦|e |·ope·¦|es .|rdo. ¦o be sJ·e yoJ
|ave ¦|e co··ec¦ cor¦·o|
\e·|¦y ¦|a¦ ¦|e rane ard ¦ype o¦ cor¦·o| |s ¦|e ore yoJ |r¦erd ¦o .o·| .|¦|
&
DoJb|ec||c| ¦|e b|Je d|s| or ¦|e des|gre· sJ·¦ace ard yoJ .||| be p·eser¦ed .|¦| ¦|e de¦aJ|¦ ever¦
¦enp|a¦e ¦o· ¦||s cor¦·o|. .||c| |s ¦|e c||c| ever¦
'
/dd ¦|e ¦o||o.|rg ||re o¦ code ¦o ¦|e SaveToo|S¦·|p|J¦¦or_C||c| ever¦ p·ocedJ·e (| .||| e.p|a|r .|a¦ |¦
does be|o.)
Me.5aveI1]eD1a]og1.5hoWD1a]og1}
(
T||s b|oc| o¦ code d|sp|ays ¦|e Save|||eD|a|og1 by ca|||rg ¦|e S|o.D|a|og() ne¦|od or |¦ /¦ ¦||s
po|r¦. |¦ yoJ .ar¦ ¦o see ¦|e e¦¦ec¦ o¦ ¦|e c|arge. |Js¦ bJ||d ard e.ecJ¦e ¦|e app||ca¦|or by p·ess|rg
f5 ard c||c| ¦|e save |cor ¦o see ¦|a¦ ¦|e save d|a|og .||| s|o. Jp
)
|r eve·y Save|||eD|a|og. ¦|e·e |s a save bJ¦¦or ard a carce| bJ¦¦or T|e carce| bJ¦¦or |s aJ¦ona¦|ca||y
¦a|er ca·e o¦ ¦o· Js |J¦ .e reed ¦o .|·e .|a¦ |s go|rg ¦o |apper .|er ¦|e Jse· c||c|s or ¦|e save
bJ¦¦or o¦ ¦|a¦ re. d|a|og
*
To .|·e ¦|e save bJ¦¦or. se|ec¦ ¦|e savefìIeDìaIogl |cor |r ¦|e conporer¦ ¦·ay ard doJb|ec||c| |¦ ¦o
ge¦ ¦o ¦|e nos¦ connor ever¦. .||c| |s ¦|e |||eC| ever¦ |r ¦||s case
Jh^c\8dbbZcih
^cNdjg8dYZ
One çccJ hcbit vcµ shcµlJ
stcrt embrccinç when writinç
ccJe is tc ccmment vcµr ccJe.
Riçht ncw. the ccJe fcr the
µrcblems we cre sclvinç isn´t
tcc ccmµliccteJ. 3µt leeµ in
minJ thct cJJinç ccmments
serves the fcllcwinç µµrµcse.
first. vcµr ccJe beccmes mµch
mcre mcintcincble beccµse vcµ
ccn ccme bccl six mcnths lcter
cnJ. if the ccmments cre çccJ.
vcµ will be cble tc µnJerstcnJ
whct vcµ JevelcµeJ. lt clsc
mcles vcµr ccJe mcre recJ-
cble cnJ fccilitctes çettinç helµ
frcm scmebcJv. Write vcµr
ccmments in ncrmcl lnçlish
withcµt tcc mcnv jcrçcn wcrJs.
Ancther thinç tc remember is
´c·||·:eJ c· ·ex| ¡ooe
lf you want to Iook at aII the
possibIe events that can be fired
for a particuIar controI, you can
cIick on the yeIIow Iightning icon
that is Iocated at the top of the
Properties window as seen in the
graphic from the previous tip. To
come back to the properties, you
just have to cIick the IittIe sheet
symboI to the Ieft of the yeIIow
Iightning icon.
G < C
C05622132.indd 77 10/18/05 7:05:16 PM
78 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
+
\e'|| Jse code sr|ppe¦s ¦o |rse·¦ ¦|e code ¦|a¦ .||| e.ecJ¦e .|er ¦|e Jse· p·esses ¦|e Save bJ¦¦or
|enenbe· ¦|a¦ ¦o ge¦ ¦o ¦|e code sr|ppe¦s yoJ reed ¦o ·|g|¦c||c| |r ¦|e code ed|¦o· ard se|ec¦ |rse·¦
Sr|ppe¦. ¦|er ¦o||o. ¦|e d|¦¦e·er¦ pa¦| c|o|ces `oJ reed ¦o .·|¦e ¦o a ¦||e so Jse ¦|e ¦o||o.|rg pa¦|
|·ocess|rg D·|ves. |o|de·s ard |||es\·|¦e Te.¦ ¦o a |||e \|er ¦|r|s|ed. yoJ· code s|oJ|d |oo| |||e ¦|e ||s¦|rg
be|o. |.an|re ¦|e conner¦s ¦o Jrde·s¦ard .|a¦ .e a·e ¦·y|rg ¦o acconp||s|
Sone ||res o¦ code car ge¦ ·a¦|e· |org \|sJa| |as|c a||o.s yoJ ¦o Jse a space ard ar Jrde·sco·e (_) ¦o
|rd|ca¦e ¦|a¦ a ||re o¦ code cor¦|rJes or ¦|e re.¦ ||re / ||re o¦ code car ¦yp|ca||y be b·o|er .|e·e a space
occJ·s. |o.eve·. |r sone |oca¦|ors (sJc| as .|¦||r a oJo¦ed s¦·|rg). a ||re carro¦ be b·o|er
SaveFileDialogí_FileOk MeLhod
1 P¡1vafe 5ub 5aveI1]eD1a]og1I1]eOk18yva] sende¡ As 5ysfem.Ob1ecf. 8yva] e As
2 5ysfem.ComponenfMode].Cance]LvenfA¡gs} hand]es 5aveI1]eD1a]og1.I1]eOk
3 1¡y
4 ` 1he p¡ope¡fy I1]eName f¡om fhe f1¡sf a¡gumenf 1n W¡1feA]]1exf ¡efe¡s fo
S ` fhe I1]eName se]ecfed by fhe use¡ 1n fhe 5aveI1]eD1a]og1
6 ` 1hen We a¡e pass1ng fhe confenf of each 1exf8ox and concafenaf1ng fhe
7 ` Ca¡¡1age Refu¡n-L1ne Ieed consfanf
ß ` 1he ]asf pa¡amefe¡ 1nd1cafed 1f We shou]d append fo a f1]e 1f 1f
9 ` ex1sfs. Ia]se W1]] nof and fhe¡efo¡e ¡ec¡eafe fhe f1]e each f1me.
10 ` ln o¡de¡ fo add fhe confenf of fhe f1¡sf box We need fo puf f¡ue fo¡
11 ` fhe 2d and 3¡d W¡1fe ofhe¡W1se on]y fhe ]asf one Wou]d be fhe¡e
12 ` 1f fhey We¡e bofh f¡ue.
13 My.Compufe¡.I1]e5ysfem.W¡1feA]]1exf1Me.5aveI1]eD1a]og1.I1]eName.
14 Me.fbMessage.1exf & vbC¡Lf. Ia]se}
1S My.Compufe¡.I1]e5ysfem.W¡1feA]]1exf1Me.5aveI1]eD1a]og1.I1]eName.
16 Me.1exf8ox2.1exf & vbC¡Lf. 1¡ue}
17 My.Compufe¡.I1]e5ysfem.W¡1feA]]1exf1Me.5aveI1]eD1a]og1.I1]eName.
1ß Me.1exf8ox3.1exf & vbC¡Lf. 1¡ue}
19 Cafch f1]eLxcepf1on As Lxcepf1on
20 1h¡oW f1]eLxcepf1on
21 Lnd 1¡y
2s2 Lnd 5ub
,
No. .e |Js¦ reed ¦o a¦¦ac| ¦|e sane ever¦ code ¦o ¦|e |||e¹Save nerJ c|o|ce DoJb|ec||c| ¦|e Save
c|o|ce |r ¦|e |||e nerJ ard add ¦|e sane code as |r s¦ep 4 |J||d ¦|e app||ca¦|or ard e.ecJ¦e |¦ by
p·ess|rg f5 Type sone ¦e.¦ |r ¦|e ¦e.¦ bo.es ard ¦|er save ¦|e cor¦er¦ ¦o a ¦||e by Js|rg ¦|e Save nerJ o·
¦|e Save ¦oo|ba· bJ¦¦or `oJ s|oJ|d c|ec| ¦|a¦ ¦|e cor¦er¦ o¦ ¦|e ¦||e yoJ· app||ca¦|or saved |s ·ea||y .|a¦
.as or ¦|e ¦o·n To ve·|¦y ¦|a¦ |¦ .o·|ed p·ope·|y. oper ¦|e ¦||e .|¦| \|sJa| S¦Jd|o by se|ec¦|rg ¦|e
|||e¹Cper |||e nerJ. b·o.s|rg ¦o ¦|e |oca¦|or o¦ ¦|e saved ¦||e. ard ¦|er oper|rg |¦ ¦o v|e. |¦s cor¦er¦s
thct the ccmments cre never
ccmµileJ in the cµµliccticn vcµ
execµte sc thev will never slcw
Jcwn the µerfcrmcnce cf vcµr
cµµliccticn.
As vcµ ccn see in the list-
inç in steµ 8. vcµ ccn ccm-
ment vcµr ccJe bv insertinç c
sinçle qµcte cnJ then tvµinç
vcµr ccmment. Ycµr ccmment
shcµlJ cµµecr in çreen. if nct.
then vcµr line is nct seen cs
c ccmment bv the ccmµiler.
Ancther çccJ wcv tc ccmment
vcµr ccJe is tc µse twc bµt-
tcns frcm cne cf the tcclbcrs.
let´s scv vcµ JeciJe thct cll
the µrevicµs ccJe in the lileOl
event is nct the ccJe vcµ wcnt
tc execµte beccµse vcµ wcnt
tc test scmethinç else. Ycµ Jc
nct wcnt tc Jelete cll the text
bµt vcµ ccn ccmment cµt the
ccJe bv selectinç it cnJ then
cliclinç cn the Ccmment Oµt
the SelecteJ lines bµttcn. AnJ
if vcµ wcnt tc µnccmment c
blccl cf ccJe. vcµ jµst hcve
tc select the ccJe vcµ wcnt tc
µnccmment cnJ then clicl the
Unccmment the SelecteJ lines
bµttcn.
´c·||·:eJ |·cm ¡ooe ¯¯
C05622132.indd 78 10/18/05 7:05:16 PM
Chapter 5. Creating Your lirst luII windows AppIication 79
We jusL handled Lwo evenLs, buL l wanL Lo poinL ouL LhaL we already handled evenLs pre-
viously by coding Lhe 8uLLoní_Click and modiíying properLies oí oLher conLrols. For insLance,
we modiíied Lhe 1exL properLy oí our 1exL8ox conLrols when we handled Lhe buLLon click.
And we were able Lo do LhaL by using Lhe conLrol's name properLy.
ln Summary.
Wow, LhaL was a big chapLer wiLh a loL oí new íeaLures. 1hese íeaLures will deíiniLely help
you wriLe your applicaLions. We wenL over some lDF íeaLures such as Lhe snap lines Lo help
you Lo align Lhe conLrols on Lhe íorm. We wenL on Lo Lhe rich íeaLures oí lnLelliSense LhaL
help you wiLh Lyping oí your code by eiLher suggesLing appropriaLe choices, compleLing
code senLences íor you, or providing you wiLh code snippeLs. ln Lhe end, lnLelliSense is Lhere
Lo reduce Lhe amounL oí Lyping you do so LhaL your producLiviLy can increase. On Lop oí
LhaL, iL is a greaL Lool íor beginners.
We Lhen saw how Lhe compiler is working in Lhe background by helping you in real-Lime
Lo deLecL errors and by providing you wiLh suggesLions Lo íix Lhem. We also reviewed Lhe
inLroducLion oí Lhe Mv namespace in visual 8asic. 1his new namespace provides an easy way
Lo a mulLiLude oí common Lasks by encapsulaLing a loL oí lines oí .NF1 raw code in a simpler
one-line synLax. OíLen, one line oí code is replacing up Lo 200 lines oí raw .NF1 code.
We Lhen saw Lhe beneíiLs oí Lhe renaming íeaLure Lo replace symbols in all projecL
linJinç AJJiticncl
lnfcrmcticn
CcJe Sniµµet ediLor will
allow you Lo creaLe, ediL, and
debug your own code snippeLs.
1he Lool also allows you Lo
íill all Lhe meLadaLa, shorLcuL,
replacemenL variables, and
replacemenL assemblies. Here
is Lhe link Lo LhaL Lool. httµ.//
msJn.micrcscft.ccm/vbcsic/
JcwnlccJs/200S/tccls/
sniµµeteJitcr/
Ancther çccJ scµrce cf
infcrmcticn is the viJecs frcm
MSDN sµecificcllv Jcne tc ccver
the visµcl 3csic 200S lxµress
lJiticn µrcJµct. l sµççest vcµ
wctch the fcllcwinç twc lesscns
cs c ccmµlement. thev will rein-
fcrce c lct cf tcµics ccvereJ in
this chcµter.
Lesson 2 video. httµ.//
çc.micrcscft.ccm/fwlinl/
?linliJ=440J0&clciJ=0x40º
Lesson 8 video. httµ.//
çc.micrcscft.ccm/fwlinl/
?linliJ=440J0&clciJ=0x40º
´c·||·:eJ c· ·ex| ¡ooe
C05622132.indd 79 10/18/05 7:05:17 PM
80 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
1hose Lwo lessons will be
a preLLy good visual live sum-
mary Lo Lhis chapLer.
69K6C8:9IDE>8/ lf
vcµ wcnt tc extenJ the Mv
ncmesµcce. here is c çccJ
MSDN whiteµcµer cn the tcµic.
httµ.//www.msJn.micrcscft.ccm/
msJnmcç/issµes/0S/07/mv/
Jefcµlt.csµx
´c·||·:eJ |·cm ¡ooe ¯¯
íiles. lL is especially useíul Lo replace auLogeneraLed variable names wiLh more meaningíul
variable names. We examined Lhe mosL common conLrols you will íind in every Windows
applicaLion wiLh some graphical examples and jusLiíicaLion on Lheir usage. Finally, we ended
Lhe chapLer wiLh how evenL-based programming is períormed.
ln Lhe nexL chapLer, you'll puL inLo pracLice everyLhing you jusL learned in Lhis chapLer.
¥ou'll also Lake a look aL some new íeaLures, conLrols, and concepLs LhaL you'll use as we
conLinue wiLh Lhe Web browser projecL.
C05622132.indd 80 10/18/05 7:05:17 PM
8l
;bjgb5e\aZHcLbhe
4cc_\VTg\ba+%
<agXeTVg\aZG[ebhZ[
7\T_bZ5bkXf++
4CebYXff\baT_?bb^TaW
9XX_TgLbhe9\aZXeg\cf,'
6[TcgXe)
@bW\YlLbheJXU
5ebjfXeAbj
6
AíLer reading Lhe avalanche oí new concepLs presenLed in Lhe íirsL íew chap-
Lers, you're ready Lo apply Lhem and Lake your Web browser Lo Lhe nexL
level. ln Lhis chapLer, you'll add rich íeaLures Lo your browser such as a splash
screen, an AbouL box, Lool sLrips, menu sLrips, a Lool sLrip conLainer LhaL will
give you a rich user experience "à la lnLerneL Fxplorer," a sLaLus sLrip conLrol,
a progress bar, proíessional-looking Loolbars wiLh "déjà vu" icons, and new
MicrosoíL® visual 8asic
®
2005 lnLegraLed DevelopmenL FnvironmenL (lDF)
íeaLures, like Lhe DocumenL OuLline window. ¥ou'll also learn Lo respond Lo
evenLs coming írom Lhe Web 8rowser conLrol.
C06622132.indd 81 10/18/05 7:23:28 PM
82 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
How to Bring Up Your AppIication
When you load an applicaLion, you oíLen see someLhing called a spIash screen. Some good
examples oí splash screens are Lhe opening iníormaLion boxes you see íor MicrosoíL Oííice,
visual SLudio
®
2005, and mosL oLher programs. AlLhough Lhey're oíLen very nice looking,
Lhose screens aren'L Lhere jusL Lo display Lhe soíLware version and appealing arLwork or Lo
make sure you're noL bored. 1hese screens serve a íuncLion. Once you've sLarLed an applica-
Lion, a loL oí processing is happening, íor insLance, Lhe applicaLion is connecLing Lo daLabas-
es, populaLing conLrols wiLh daLa írom Lhe daLabase, geLLing saved coníiguraLions íor Lhe
user inLeríace (Ul) preíerences, and so on. Displaying Lhe splash screen while all oí Lhis pro-
cessing is happening helps iníorm Lhe user LhaL Lhe applicaLion is working.
ln realiLy, a splash screen is anoLher Windows íorm LhaL does noL allow any inpuL írom
Lhe user. lL usually has a nice presenLaLion íorm wiLh some arLwork, Lhe applicaLion name, iLs
version, and oíLen some legal LexL. One oí Lhe íirsL Lhings you'll do íor your applicaLion is Lo
add a splash screen.
ln Lhis chapLer, l've creaLed a sLarLing browser applicaLion LhaL is Lhe same as Lhe
applicaLion you creaLed in ChapLer 4. lí you insLalled Lhe companion conLenL Lo Lhe deíaulL
locaLion, iL should be aL Lhe íollowing locaLion on your hard drive. My DocumenLs\MicrosoíL
Press\v8 2005 Fxpress\ChapLer6\. Look íor a íolder named SLarL under Lhe ChapLer6 íolder.
Double-click Lhe MyOwn8rowser.sln soluLion. lí you wanL, you can also sLarL írom your own
ChapLer 4 browser projecL.
TO Ck£AT£ A SPLASH SCk££N
$
Cper ¦|e Add New Item d|a|og bo. `oJ car e|¦|e· go ¦o ¦|e |·o|ec¦ nerJ ard se|ec¦ Add
Wìndows form o· yoJ car go ¦o ¦|e So|J¦|or |.p|o·e·. ·|g|¦c||c| ¦|e p·o|ec¦ rane (|r ¦||s case.
MyOwnßrowser). se|ec¦ Add. ard ¦|er se|ec¦ \|rdo.s |o·n T|e /dd Ne. |¦en d|a|og bo. .||| appea·
ard ¦|e ¦|·s¦ ·o. o¦ c|o|ces |rd|ca¦es ¦|a¦ a sp|as| sc·eer ¦enp|a¦e a|·eady e.|s¦s
% Se|ec¦ ¦|e SpIash Screen ¦enp|a¦e ard rane |¦ spIash.vb. as s|o.r |r ||gJ·e b1 T|er c||c| Add
C06622132.indd 82 10/18/05 7:23:28 PM
Chapter 6. Modify Your web Browser Now! 83
&
Co ¦o ¦|e Des|gr v|e. o¦ ¦|e sp|as| sc·eer. ard yoJ'|| see ¦|a¦ ¦|e sc·eer |s sp||¦ |r¦o nJ|¦|p|e soJa·es
|r ¦ac¦. ¦|e sp|as| sc·eer |s ca||ed a Tab|e |ayoJ¦ |are| cor¦·o| T||s cor¦·o| |e|ps yoJ by a··arg|rg ¦|e
|ayoJ¦ o¦ yoJ· conporer¦s |r a ¦ab|e ¦o·na¦ .|¦| a se¦ o¦ ·o.s ard co|Jnrs `oJ car cJs¦on|.e ¦|e ·o.s
ard co|Jnrs by nod|¦y|rg p·ope·¦|es |r ¦|e |·ope·¦|es .|rdo. o· by Js|rg soJ·ce code T|e cor¦·o| a|so
¦ea¦J·es a sna·¦ ¦ag ¦o eas||y add o· ·enove ·o.s ard co|Jnrs (T|e sp|as| sc·eer |as ¦.o ·o.s ard ¦.o
co|Jnrs by de¦aJ|¦) |¦ yoJ |oo| ca·e¦J||y. yoJ'|| see ¦|a¦ ¦|e Sp|as| Sc·eer ¦enp|a¦e |as aro¦|e· Tab|e
|ayoJ¦ |are| |oca¦ed |r ¦|e bo¦¦on·|g|¦ ce||
Iigµre 6-I
4WW\aZTaXjJ\aWbjfYbe`-AXj<gX`gX`c_TgXV[b\VXf
£ach ceII in a TabIe Layout PaneI
can contain onIy one controI, but
you can aIways insert another
TabIe Layout PaneI as was done
for the spIash screen tempIate.
ABG 8
Often, it's difficuIt to seIect a par-
ticuIar controI. To see aII controIs
in the currentIy dispIayed form,
you can seIect the drop-down Iist
at the top of the Properties win-
dow. ligure 6-2 shows the drop-
down Iist for the SpIash Screen
form. To seIect any particuIar
controI, highIight it on the Iist and
it wiII be automaticaIIy seIected on
the design surface.
G < C
Iigµre 6-2
9\aW\aZT__g[XVbageb_fbaT
fX_XVgXWYbe`
C06622132.indd 83 10/18/05 7:23:29 PM
84 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
1he applicaLion LiLle, version, and copyrighL iníormaLion are all obLained dynamically.
1his means Lhe íorm will geL Lhe values írom a variable or a seLLing somewhere in your proj-
ecL. ln íacL, aL run Lime Lhose Lhree pieces oí iníormaLion are obLained when Lhe splash
screen is loaded by looking up applicaLion seLLings sLored in Lhe ProjecL Designer
ApplicaLion pane.
TO Vl£w TH£ APPLlCATlON TlTL£, V£kSlON, AND COPYklGHT Pk0P£kTl£S
$ Se|ec¦ MyOwnßrowser ¦·on ¦|e So|J¦|or |.p|o·e·. ·|g|¦c||c|. ard c|oose Propertìes
T|e |·o|ec¦ Des|gre· page opers T|e |·o|ec¦ Des|gre· |as a se·|es o¦ |r¦o·na¦|or ¦abs (as s|o.r |r
||gJ·e b`) `oJ'|| .o·| na|r|y |r ¦|e ¦|·s¦ ¦ab ¦o· ro.. .||c| |s ¦|e /pp||ca¦|or pare `oJ'|| cor¦|gJ·e seve·a|
e|ener¦s |r ¦||s pare /|| ¦|e e|ener¦s yoJ'|| nod|¦y .||| a¦¦ec¦ |o. ¦|e app||ca¦|or |oo|s
Iigµre 6-J
Ceb]XVg7Xf\ZaXecTZX
C06622132.indd 84 10/18/05 7:23:30 PM
Chapter 6. Modify Your web Browser Now! 85
%
To c|arge ¦|e app|||ca¦|or |cor. go ¦o ¦|e d·opdo.r ||s¦ raned |cor. c||c| or |¦. ard se|ec¦
<ßrowse.> ||rd ¦|e C|ap¦e· b d|·ec¦o·y .|e·e yoJ've |rs¦a||ed ¦|e boo|'s sanp|e ¦||es ard |oo| ¦o·
¦|e gIobe.ìco ¦||e |r ¦|e Images ¦o|de· (|¦ yoJ |rs¦a||ed ¦|e conpar|or cor¦er¦ a¦ ¦|e de¦aJ|¦ |oca¦|or. ¦|er
|¦ s|oJ|d be a¦ ¦|e ¦o||o.|rg |oca¦|or or yoJ· |a·d d·|ve \y DocJner¦s\\|c·oso¦¦ |·ess\\| ´JJ' |.p·ess\)
`oJ've c|arged ¦|e |cor o¦ yoJ· app||ca¦|or assenb|y o·. |r o¦|e· .o·ds. yoJ've c|arged ¦|e |cor o¦ ¦|e
e.ecJ¦ab|e b|ra·y (e.e) ¦||e |¦se|¦ |¦ yoJ bJ||d ¦|e app||ca¦|or ard |oo| or yoJ· |a·d d·|ve .|e·e ¦|e app||ca
¦|or |s conp||ed (as yoJ |ea·red |r a p·ev|oJs c|ap¦e·. a|| yoJ· p·o|ec¦s a·e by de¦aJ|¦ |oca¦ed a¦
\y DocJner¦s\\|sJa| S¦Jd|o ´JJ'\|·o|ec¦s\\yC.r|·o.se·\\yC.r|·o.se·\b|r\DebJg o· \b|r\·e|ease). yoJ'||
¦|rd ¦|a¦ yoJ· app||ca¦|or. \yC.r|·o.se·e.e. |as ¦|e g|obe |cor ¦|a¦ yoJ've |Js¦ se|ec¦ed |rs¦ead o¦ a
de¦aJ|¦ |cor
&
C||c| ¦|e AssembIy Informatìon. . . bJ¦¦or `oJ s|oJ|d see a sc·eer ¦|a¦ |oo|s |||e ¦|e ore |r
||gJ·e b4
You're not changing the icon of
the main form when doing this
appIication icon change. ln order
to do this, you need to change the
form icon's property by assigning
a bitmap image. You're going to
change the main form icon in the
Iast section of this chapter.
ABG 8
Iigµre 6-4
4ffX`U_l<aYbe`Tg\baW\T_bZUbk
The assembIy version informa-
tion that you see here is aIso what
the appIication wiII dispIay in the
spIash screen you're creating.
You'II see the source code that wiII
dispIay the information on the
spIash screen Iater in the chapter.
ABG 8
'
C|arge ¦|e Copy·|g|¦ |r¦o·na¦|or ·ep|ace ¦|e .o·d B^Xgdhd[i .|¦| yoJ· rane ard |eep ¦|e ·es¦ o¦
¦|e |r¦o·na¦|or as |¦ |s (|¦ |¦'s ro¦ a|·eady ¦|||ed .|¦| yoJ· |r¦o·na¦|or. c|arge |¦ ¦o na¦c| yoJ· rane
o· conpary |r¦o)
(
|rse·¦ spaces be¦.eer ¦|e .o·ds BnDlc7gdlhZg |r ¦|e T|¦|e ¦e.¦ bo. (T||s s¦·|rg |s Jsed ¦o d|sp|ay
¦|e app||ca¦|or ¦|¦|e or ¦|e sp|as| sc·eer) |rse·¦ ¦.o spaces ¦o ge¦ ¦|e ¦o||o.|rg ¦|¦|e My Own
ßrowser
C06622132.indd 85 10/18/05 7:23:31 PM
86 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
) C||c| OK ¦o c|ose ¦|e /ssenb|y |r¦o·na¦|or d|a|og bo.
*
To a¦¦ac| ¦|e sp|as| sc·eer ¦o yoJ· app||ca¦|or. se|ec¦ spIash ¦·on ¦|e Sp|as| Sc·eer d·opdo.r ||s¦ a¦
¦|e bo¦¦on o¦ ¦|e |·o|ec¦ Des|gre·'s app||ca¦|or ¦ab
+
Save ¦|e app||ca¦|or (C¦·|÷S||¦¦÷S saves a|| ¦||es ard C¦·|÷S saves ¦|e cJ··er¦ ¦||e) ard p·ess f5
¦o ·Jr |¦
ln the Project Designer, when
the £nabIe XP VisuaI StyIes check
box is checked, your appIication
wiII inherit the Iook and feeI of
Microsoft windows
®
XP itseIf.
lor exampIe, it gives you controIs
with rounded corners that Iight up
on mouse over. There's aIso a new
coIorfuI progress bar and many
other things that you've probabIy
seen before. Note that when exe-
cuted on a pIatform that doesn't
support windows XP Themes,
the appIication reverts to the tra-
ditionaI windows Iook and feeI
{windows 2000 or windows 98).
@BE8 < A9 B
Iigµre 6-5
Fc_Tf[fVeXXa\aTVg\ba
¥ou should see Lhe splash screen íor abouL Lwo seconds beíore Lhe browser íorm
appears. 1wo seconds isn'L a long Lime, so you mighL noL have Lime Lo really look aL iL. 1o
see whaL iL acLually looks like aL run Lime, look aL Figure 6-5. 1he LiLle, version number, and
copyrighL iníormaLion appear auLomaLically. 8uL how does Lhe iníormaLion geL Lhere¯
1hose Lhree pieces oí iníorma-
Lion are obLained programmaLically
using a íamiliar consLrucL-Lhe Bn
namespace. (8y programmaLically, l
mean wriLing code Lo seL or geL
someLhing you would normally seL
or geL using a Ul Lool, such as Lhe
ProperLies window or Lhe ProjecL
Designer.) One imporLanL parL oí
Lhe Splash Screen LemplaLe is Lhe
Form Load evenL, each íorm has a
Load evenL LhaL happens jusL
beíore Lhe íorm is displayed. 1his is where you'll usually períorm Lhe iniLializaLion íor con-
Lrols on your íorm. Review Lhe splash_Load meLhod (in splash.vb) below íor Lhe highlighLed
code Lo undersLand where and how Lhe Myclasses and meLhods are used Lo populaLe Lhe
íields on Lhe splash screen.
C06622132.indd 86 10/18/05 7:23:32 PM
Chapter 6. Modify Your web Browser Now! 87
P¡1vafe 5ub sp]ashLoad18yva] sende¡ As Ob1ecf. 8yva] e As 5ysfem.LvenfA¡gs} hand]es Me.Load
`5ef up fhe d1a]og fexf af ¡unf1me acco¡d1ng fo fhe app]1caf1on`s assemb]y 1nfo¡maf1on.
`1ODO: Cusfom1ze fhe app]1caf1on`s assemb]y 1nfo¡maf1on 1n fhe ¨App]1caf1on¨ pane of fhe
` p¡o1ecf p¡ope¡f1es d1a]og 1unde¡ fhe ¨P¡o1ecf¨ menu}.
`App]1caf1on f1f]e
lf My.App]1caf1on.lnfo.11f]e <> ¨¨ 1hen
App]1caf1on11f]e.1exf = My.App]1caf1on.lnfo.11f]e
L]se
`lf fhe app]1caf1on f1f]e 1s m1ss1ng. use fhe app]1caf1on name. W1fhouf fhe exfens1on
App]1caf1on11f]e.1exf = 5ysfem.lO.Pafh.GefI1]eNameW1fhoufLxfens1on1
My.App]1caf1on.lnfo.Assemb]yName}
Lnd lf
`Io¡maf fhe ve¡s1on 1nfo¡maf1on us1ng fhe fexf sef 1nfo fhe ve¡s1on conf¡o] af des1gn
` f1me as fhe fo¡maff1ng sf¡1ng. 1h1s a]]oWs fo¡ effecf1ve ]oca]1zaf1on 1f des1¡ed.
` 8u1]d and ¡ev1s1on 1nfo¡maf1on cou]d be 1nc]uded by us1ng fhe fo]]oW1ng code and
` chang1ng fhe ve¡s1on conf¡o]`s des1gnf1me fexf fo ¨ve¡s1on {0}.{1:00}.{2}.{3}¨ o¡
` somefh1ng s1m1]a¡. 5ee 5f¡1ng.Io¡maf1} 1n he]p fo¡ mo¡e 1nfo¡maf1on.
´
´ ve¡s1on.1exf = 5ysfem.5f¡1ng.Io¡maf1ve¡s1on.1exf.
` My.App]1caf1on.lnfo.ve¡s1on.Ma1o¡. My.App]1caf1on.lnfo.ve¡s1on.M1no¡.
` My.App]1caf1on.lnfo.ve¡s1on.8u1]d. My.App]1caf1on.lnfo.ve¡s1on.Rev1s1on}
ve¡s1on.1exf = 5ysfem.5f¡1ng.Io¡maf1ve¡s1on.1exf. My.App]1caf1on.lnfo.ve¡s1on.Ma1o¡.
My.App]1caf1on.lnfo.ve¡s1on.M1no¡}
`Copy¡1ghf 1nfo
Copy¡1ghf.1exf = My.App]1caf1on.lnfo.Copy¡1ghf
Lnd 5ub
As you can see, My.ApplicaLion.lnío was useíul íor obLaining applicaLion seLLings wiLhouL
reading Lhose seLLings írom a special íile. 1haL's Lhe magic oí Lhe Bn namespace.
C06622132.indd 87 10/18/05 7:23:32 PM
lnteracting Through DiaIog Boxes
88 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
¥ou mighL Lhink Lhe Lime Lhe splash screen is displayed on Lhe screen is Loo shorL. A big-
ger applicaLion may need Lo open connecLions Lo daLabases, reLrieve iníormaLion, and do all
kinds oí iniLializaLion upon launching, so iL mighL Lake more Lhan Lwo seconds. Or, you may
simply wanL your users Lo have more Lime Lo look aL Lhe screen. 1here's an easy way Lo
change Lhe amounL oí Lime iL displays. 1here is a code snippeL on MSDN
®
LhaL will enable
you Lo modiíy Lhe delay. ¥ou can íind Lhe code snippeL aL ]iie/$$bhYc#b^Xgdhd[i#Xdb$kWVh^X$
YdlcadVYh$'%%*$hc^eeZih$YZ[Vjai#Vhem#
l'm sure you've seen the format-
ting in the Properties window for
the version number. This speciaI
notation is how strings are for-
matted. To reaIIy understand, l
recommend you press ll to search
the software documentation for
String.lormat. You'II find aII pos-
sibIe formatting options and how
to use them.
@BE8 < A9 B
You can change the bitmap
image on the spIash screen to
whatever you want by modi-
fying the MainLayoutPaneI
- Backgroundlmage property in
the Properties window. You can
aIso change the size of the spIash
screen to fit the size of your image
or use imaging software to change
the size of the image to fit the
spIash screen.
@BE8 < A9 B
1he dialog boxes you creaLe help Lhe user inLeracL wiLh Lhe soíLware. 1hey are addiLional
íorms LhaL you add Lo your applicaLion. ln Lhis secLion, you'll add Lwo dialog boxes Lo your
Web browser. an AbouL box and a NavigaLe dialog box.
Adding an About Box
1he íirsL dialog box you'll add is an AbouL box, which exisLs in mosL Windows applicaLions.
1his dialog box essenLially conLains Lhe same iníormaLion as Lhe splash screen, buL some-
Limes conLains more legal, sysLem, and version iníormaLion.
8eíore you sLarL Lhis secLion, you'll give Formí a more meaningíul name. (Keep in mind
LhaL everyLhing in your applicaLion needs Lo be meaningíul íor readabiliLy and mainLenance.)
¥ou'll also prepare Lhe applicaLion íor a LransíormaLion inLo a more íeaLure-rich lnLerneL
browser.
C06622132.indd 88 10/18/05 7:23:32 PM
Chapter 6. Modify Your web Browser Now! 89
TO ADD AN ABOUT BOX
$

|r ¦|e So|J¦|or |.p|o·e·. ·erane |o·n1vb ¦o ßrowser.vb
%
Cr ¦|e |·o.se· ¦o·n. de|e¦e ¦|e txtURL ard ¦|e btnCo cor¦·o|s De|e¦e ¦|e btnCo_CIìck ever¦ |ar
d|e· by ·enov|rg |¦s s|gra¦J·e ard cor¦er¦ ¦·on ¦|e |·o.se·vb ¦||e
&
Cr ¦|e |·o.se· ¦o·n. se|ec¦ ¦|e Web ßrowser cor¦·o|. ard Js|rg ¦|e sna·¦ ¦ag. se|ec¦ Dock In
Parent Contaìner
'
/s yoJ d|d ¦o· ¦|e sp|as| sc·eer. add a re. |¦en ¦o yoJ· p·o|ec¦. bJ¦ ¦||s ¦|ne .|er p·eser¦ed .|¦|
¦|e ¦enp|a¦es. c|oose ¦|e About ßox ¦enp|a¦e ard rane |¦ Aboutßox.vb
Similar Lo Lhe splash screen, Lhe AbouL box will be populaLed wiLh iníormaLion írom Lhe
projecL seLLings írom Lhe ProjecL Designer window. AL Lhis poinL, ií you run Lhe applicaLion,
Lhere is no link beLween your AbouL box and Lhe resL oí your browser, so iL won'L show up
anywhere. Usually, Lhe AbouL box shows up when you requesL iL írom Lhe Help menu, so
you'll add Lhis missing link now.
TO LlNK TH£ ABOUT BOX TO TH£ H£LP M£NU
$
Se|ec¦ ¦|e ßrowser.vb [Desìgn]* ¦ab ¦o ·e¦J·r ¦o ¦|e |·o.se· |o·n Des|gr v|e. D·ag a MenuStrìp
cor¦·o| ¦·on ¦|e ¦oo|bo. or¦o ¦|e des|gr sJ·¦ace ¦o add a \erJ S¦·|p cor¦·o| ¦o ¦|e |·o.se· ¦o·n
Nane |¦ msßrowser
%
To add ¦|e |e|p nerJ. se|ec¦ ¦|e nerJ s¦·|p or ¦|e ¦o·n. c||c| ¦|e sna·¦ ¦ag. ard ¦|er se|ec¦ Insert
Standard Items `oJ'|| ge¦ a ¦an|||a· \|rdo.s app||ca¦|or nerJ s¦·|p ard |¦s nerJ c|o|ces .|¦| ¦|e|·
sJbnerJs. |cors. ard |eyboa·d s|o·¦cJ¦s
&
De|e¦e a|| nerJ c|o|ces XkVXcg ¦|e |e|p nerJ ard ¦|e /boJ¦ nerJ c|o|ces Jrde· ¦|e |e|p nerJ
To pe·¦o·n ¦||s c|earJp. se|ec¦ ary nerJ c|o|ce. ¦|er ·|g|¦c||c| ¦o b·|rg Jp ¦|e cor¦e.¦Ja| nerJ
ard se|ec¦ DeIete ¦o ·enove |¦ /|so ·enove ¦|e nerJ sepa·a¦o·s (¦|a¦ |s. ¦|e ||res sepa·a¦|rg nerJ
c|o|ces)
when a procedure directive teIIs
you to add a controI and then
name it XYZ, it means that you
need to add the controI onto the
design surface, then go to the
Properties window and change the
{Name) property of the controI
to XYZ.
ABG 8
C06622132.indd 89 10/18/05 7:23:33 PM
90 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
'
To .|·e ¦|e re. /boJ¦ bo. ¦o·n .|¦| ¦|e /boJ¦ nerJ c|o|ce. doJb|ec||c| ¦|e About. . . nerJ
c|o|ce ¦o ge¦ ¦o ¦|e /boJ¦Too|S¦·|p\erJ|¦en_C||c| ever¦ |ard|e·
( /dd ¦|e ¦o||o.|rg ||re o¦ code ¦o ¦|e ever¦ |ard|e·
Abouf8ox.5hoWD1a]og1}
)
Save ¦|e app||ca¦|or ard ¦|er ·Jr |¦ Se|ec¦ About. . . ¦·on ¦|e |e|p nerJ T|e sc·eer s|oJ|d ·esen
b|e ||gJ·e bb T|e S|o.D|a|og() ne¦|od b·|rgs Jp ¦|e ¦o·n |r ¦|e n|dd|e o¦ ¦|e e.ecJ¦|rg app||ca
¦|or. ard ro¦||rg e|se car |apper Jr¦|| yoJ c||c| ore o¦ |¦s bJ¦¦ors o· ¦|e ·ed X ¦o c|ose ¦|e d|a|og bo. |r
¦||s case. |¦ |as or|y ¦|e C| bJ¦¦or
l modified the AssembIy
lnformation fieIds in the Project
Designer to come up with the
information dispIayed in the
About box. You can do the same.
You simpIy add or modify the con-
tent in the Description, Company,
Product, and Copyright fieIds.
ABG 8
Iigµre 6-6
4UbhgUbkf[bj\aZhc\albheaXj_l
eXY\aXWUebjfXeTcc_\VTg\ba
C06622132.indd 90 10/18/05 7:23:34 PM
Chapter 6. Modify Your web Browser Now! 9l
¥ou're probably wondering why Lhe applicaLion worked when you clicked Lhe OK buLLon
even Lhough you didn'L wriLe any code Lo handle Lhis evenL. 1his is an example oí Lhe pro-
ducLiviLy gains you'll geL when using LemplaLes. 1he LemplaLe includes Lhe code Lo handle
Lhe click buLLon evenL. Review Lhe source code íor Lhe dialog box by righL-clicking Lhe
AbouL8ox.vb íilename in Lhe SoluLion Fxplorer and selecLing view Code. Again, as covered in
Lhe splash screen secLion, using My.ApplicaLion.lnío gives you quick access Lo applicaLion
iníormaLion in Lhe ProjecL Designer. Also noLe LhaL Lhe click evenL has a single line oí code
LhaL Lells Lhe íorm Lo call Lhe Close() meLhod.
Now LhaL you've added Lhe AbouL box, iL should be easy Lo add anoLher LhaL will allow
your users Lo navigaLe Lo Web pages.
Adding a Navigate DiaIog Box
¥ou saw earlier LhaL deleLing Lhe buLLon and Lhe address conLrols removed Lhe abiliLy Lo
navigaLe Lo a Web page. 1his, oí course, is noL useíul íor a Web browser. Now you'll add a
dialog box LhaL will give your user anoLher way Lo navigaLe Lo Web pages.
TO ADD A NAVlGAT£ DlALOG BOX
$
/s yoJ've dore ¦o· ¦|e /boJ¦ bo. ard ¦|e sp|as| sc·eer. add aro¦|e· re. |¦en ¦o yoJ· p·o|ec¦ |s|rg
¦|e ¦enp|a¦es. se|ec¦ a DìaIog ¦enp|a¦e ard rane |¦ Navìgate.vb
%
/dd a |abe| ard a ¦e.¦ bo.
Q Nane ¦|e |abe| IbIInfoUrI Se¦ ¦|e Te.¦ p·ope·¦y ¦o 'Type ar |r¦e·re¦ add·ess ard \y C.r |·o.se· .|||
oper |¦ ¦o· yoJ'
Q Nane ¦|e ¦e.¦ bo. txtUrI. se¦ ¦|e /J¦oConp|e¦e\ode p·ope·¦y ¦o SuggestAppend. ard se¦ ¦|e
/J¦oConp|e¦eSoJ·ce p·ope·¦y ¦o AIIUrI
&

S|.e ard pos|¦|or ¦|e cor¦·o|s ard ¦|e ¦o·n so ¦|a¦ ¦|e Nav|ga¦e ¦o·n |oo|s |||e ¦|e ore |r ||gJ·e b¯
As you probabIy saw in the
object-oriented tutoriaI movie
{go to httµ.//go.microsoft.com/
fwlink/?linkiJ=440J0&clciJ=0x409
and Iook at Iesson 6, parts l and
2), Me means the current instance
of an object. ln this case, this is
an instance of the AboutBox cIass.
Me is used to access aII the pubIic
fieIds, properties, and methods
defined in the cIass. ln this exam-
pIe, Me is aIIowing you to assign
some content to fieIds that beIong
to the AboutBox cIass, which are
aIso the fieIds from the AboutBox
form. kemember that everything
in .N£T is an object-fieIds in a
form are members of a cIass, and
a form instance is an object.
@BE8 < A9 B
A diaIog box is often a @bWT_9be`.
lt has a predefined behavior in
which the user can't cIick anything
other than controIs on that form.
the OK and CanceI buttons or the
red X button to cIose the form.
This means that untiI the diaIog
box is cIosed, the user won't be
abIe to cIick anything eIse in the
appIication. To understand what's
happening here, just think about
the Print diaIog box in Microsoft
word. once it is dispIayed, you
can't go back to your document to
make any changes whiIe the diaIog
box is open. The Print diaIog box
is a modaI form.
@BE8 < A9 B
Iigµre 6-7
ATi\ZTgXYbe`
C06622132.indd 91 10/18/05 7:23:35 PM
92 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
¥ou've seL some oí Lhe auLocompleLe properLies oí Lhe LexL box Lo behave Lhe same way
Lhey do in MicrosoíL lnLerneL Fxplorer. 1his means LhaL Lhe LexL box will suggesL and append
URLs based on Lhe leLLers Lhe user Lypes in. ¥ou'll now wire Lhis íorm Lo Lhe applicaLion using
a new menu called NavigaLe.
TO wlk£ TH£ lOkM TO TH£ APPLlCATlON USlNG TH£ NAVlGAT£ M£NU
$
|e¦J·r ¦o ¦|e |·o.se· ¦o·n |r Des|gr v|e. ard |oo| a¦ ¦|e ¦op o¦ ¦|e |·o.se· ¦o·n `oJ a|·eady |ave
a nerJ s¦·|p .|¦| ¦|e |e|p nerJ. ro. add a re. nerJ ¦o yoJ· nerJ s¦·|p by c||c||rg bes|de ¦|e
|e|p nerJ ard ¦yp|rg &Navìgate T|e |r ¦·or¦ o¦ ¦|e C .||| c·ea¦e ar Jrde·sco·ed N so ¦|a¦ ¦|e Jse·
car p·ess ¦|e |eys¦·o|e conb|ra¦|or /|¦÷N ¦o ¦|·e ¦|e c||c| ever¦ or ¦|e Nav|ga¦e nerJ
%
Crce |¦'s c·ea¦ed. yoJ'|| see ¦|a¦ ¦|e Nav|ga¦e nerJ s|o.s Jp ¦o ¦|e ·|g|¦ o¦ ¦|e |e|p nerJ To
nove a nerJ c|o|ce. s|np|y se|ec¦ |¦ ard d·ag |¦ .|e·e yoJ .ar¦ |r ¦||s case. d·op |¦ ¦o ¦|e |e¦¦ o¦ ¦|e
|e|p nerJ
&
|e¦o·e add|rg ¦|e code ¦o· ¦|e ever¦ |¦se|¦. yoJ reed ¦o add ar |npo·¦ar¦ ||re o¦ code |enenbe·
¦|a¦ |r \|sJa| |as|c. eve·y¦||rg |s ar ob|ec¦. ard |¦ yoJ .ar¦ ¦o nar|pJ|a¦e aro¦|e· ¦o·n ard e.c|arge
da¦a be¦.eer ¦|e ¦.o ¦o·ns. yoJ ¦|·s¦ reed ¦o c·ea¦e ar ob|ec¦ o¦ ¦|a¦ ¦ype ¦|a¦ |s v|s|b|e ¦o yoJ· na|r ¦o·n
(|·o.se· ¦o·n)¬|r ¦||s case. ar ob|ec¦ o¦ ¦ype Nav|ga¦e C·ea¦e ar |rs¦arce o¦ ¦|e Nav|ga¦e ¦o·n oJ¦s|de
¦|e soJ·ce code o¦ ary ever¦ |ard|e· by .·|¦|rg ¦|e ¦o||o.|rg ||re o¦ code |r |·o.se·vb
D1m Nav1gafeW1ndoW As NeW Nav1gafe1}
|oo| a¦ ||gJ·e b8 ¦o see .|e·e ¦o |rse·¦ |¦
Iigµre 6-8
6eXTg\aZTaXj\afgTaVXbYg[XYbe`ATi\ZTgX
C06622132.indd 92 10/18/05 7:23:35 PM
Chapter 6. Modify Your web Browser Now! 93
No. ¦|a¦ yoJ |ave ar |rs¦arce o¦ ¦|e Nav|ga¦e ¦o·n c|ass. yoJ car .·|¦e code ¦o e.c|arge da¦a bac| ard
¦o·¦| be¦.eer ¦|e ¦.o ¦o·ns /rd ¦|a¦'s e.ac¦|y .|a¦ .||| |apper \|er ¦|e Nav|ga¦e ¦o·n d|sp|ays ard ¦|e
Jse· c||c|s ¦|e C| bJ¦¦or .|¦| a ||| |r ¦|e ¦e.¦ bo.. ¦|e b·o.se· cor¦·o| .||| rav|ga¦e ¦o ¦|e spec|¦|ed |||
/|so ro¦e ¦|a¦ ¦|e ||| ¦e.¦bo. .||| c|ea· a¦¦e· rav|ga¦|rg ¦o ¦|e ||| ¦o na|e sJ·e |¦'s enp¦y ¦|e re.¦ ¦|ne
¦|e Jse· accesses |¦
'
Cr ¦|e |·o.se· ¦o·n. doJb|ec||c| ¦|e Navìgate nerJ ¦o add ¦|e Nav|ga¦eToo|S¦·|p\erJ|¦en_C||c|
ever¦ |ard|e·
(
/dd ¦|e ¦o||o.|rg code ¦o ¦|e Nav|ga¦eToo|S¦·|p\erJ|¦en_C||c| ever¦ |ard|e·
lf 1Nav1gafeW1ndoW.5hoWD1a]og1} = W1ndoWs.Io¡ms.D1a]ogResu]f.Ok} 1hen
Me.my8¡oWse¡.Nav1gafe1Nav1gafeW1ndoW.fxfu¡].1exf}
Lnd lf
Nav1gafeW1ndoW.fxfu¡].1exf = ¨¨
)
|J||d ard e.ecJ¦e ¦|e app||ca¦|or by p·ess|rg f5 T|e ¦o·n s|oJ|d ·esenb|e ||gJ·e bº .|er ¦|e Jse·
se|ec¦s ¦|e Nav|ga¦e nerJ
Iigµre 6-9
8kXVhg\babY@lBja5ebjfXehf\aZ
g[XaTi\ZTgXYbe`j\g[ThgbVb`c_XgX
*

No.. ¦es¦ ¦|e app||ca¦|or .|¦| a|| ¦|e nod|¦|ca¦|ors yoJ've nade \e·|¦y eve·y re. aspec¦
Q Does p·ess|rg /|¦÷N ¦a|e yoJ ¦o ¦|e Nav|ga¦e ¦o·n¯
Q Car yoJ ||¦ Carce| .|¦|¹.|¦|oJ¦ cor¦er¦¯
Q Car yoJ rav|ga¦e ¦o a good |||¹bad |||¯
Q |s ¦|e ¦e.¦ bo. enp¦y .|er yoJ cone bac| ¦o ¦|e Nav|ga¦e ¦o·n (¦|a¦ |s. a¦¦e· yoJ've pe·¦o·ned a|| o¦|e·
s¦eps above ard p·essed /|¦÷N ¦o cone bac|)¯
lt's important for you to start
Iearning how to test your own
code by doing what's known as
black box testing. At a high IeveI,
this consists of testing what the
user can do and what is presented
to the user. This means that you
need to test every IittIe detaiI in
the Ul as weII as the situations the
Ul offers to the user. when you
perform a task Iike this, l suggest
you create a spreadsheet that
contains a matrix of aII test cases.
Then fiII it in as you test. This wiII
give you a visuaI representation of
aII tests and features. You're now
doing this manuaIIy because your
appIication is smaII in scope, but
you'II quickIy reaIize that with a
bigger appIication or an appIica-
tion you might seII, you'II need
some sort of automated mecha-
nism to make sure that the tests
are aII executed and you're not
forgetting any. You'II then require
a Ul testing tooI, and in most situ-
ations you'II need to buiId your
own tooIs. But that's out of con-
text for this book, l just wanted to
emphasize the importance of test-
ing your appIication.
@BE8 < A9 B
C06622132.indd 93 10/18/05 7:23:36 PM
94 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
ln Lhis secLion, you'll conLinue Lo add íuncLionaliLy Lo your browser using componenLs LhaL
you mighL have seen in oLher MicrosoíL applicaLions. ¥ou'll add appealing and proíessional
Louches Lo your applicaLion in a íasL and easy way.
Adding a TooI Strip Container and Some TooIs
A Lool sLrip conLainer is a new conLrol LhaL ships wiLh Lhis release oí visual 8asic 2005, and iL
allows you Lo leL your users cusLomize Lhe applicaLion you wroLe in a way similar Lo how
Lhey cusLomize Lhe Loolbars in MicrosoíL Oííice OuLlook
®
or Word. 1he Lool sLrip conLainer
has íive panels, one on each side oí Lhe screen, and a conLenL panel in Lhe middle. ¥ou can
have all oí Lhem on Lhe screen enabled aL one Lime or choose Lhem selecLively aL design
Lime. ¥ou can also conLrol Lhem wiLh source code. ¥ou can puL a Lool sLrip and a menu sLrip
in a Lool sLrip conLainer aL design Lime, and aL run Lime your users have Lhe opporLuniLy Lo
arrange Lheir workspace Lhe way Lhey like. 1he Lool sLrip conLainer gives your applicaLion Lhe
same look and íeel as OuLlook (see Figure 6-í0). For insLance, l was able Lo puL Lwo Lool
sLrips on Lhe leíL oí my screen. 1his means Lhose Lool sLrips are embedded in Lhe Lool sLrip
conLainer leíL panel. 8uL l could easily move any visible Loolbar back Lo Lhe Lop, Lhe righL, or
Lo Lhe boLLom. WiLh a Lool sLrip conLainer, you give your users conLrol oí Lhe layouL oí Lheir
Lool sLrips and menu sLrip, which is a greaL íeaLure Lo have.
A ProfessionaI Look and leeI at Your lingertips
Iigµre 6-I0
Gbb_Fge\c6bagT\aXeXkT`c_X\aBhg_bb^
C06622132.indd 94 10/18/05 7:23:36 PM
Chapter 6. Modify Your web Browser Now! 95
TO ADD A TOOL STklP CONTAlN£k
$ D·ag a ¦oo| s¦·|p cor¦a|re· or¦o ¦|e |·o.se· |o·n des|gr sJ·¦ace
% |erane ¦oo|s¦·|pcor¦a|re·1 ¦o maìnform1ooIStrìpContaìner
& |se ¦|e sna·¦ ¦ag ¦·on ¦|e ¦oo| s¦·|p cor¦a|re· ¦o se|ec¦ Dock fìII In form
WaiL a minuLe . . . where is Lhe Web 8rowser conLrol¯ Don'L worry, iL didn'L disappear.
1he conLrol's z-order has changed. 1he Web 8rowser conLrol is visually under Lhe Lool sLrip
conLainer, and iLs parenL is noL Lhe Lool sLrip conLainer buL Lhe browser íorm.
1he DocumenL OuLline view is a valuable Lool LhaL can help you solve Lhis problem and
save you a loL oí Lime. For Lhose oí you íamiliar wiLh previous versions oí visual SLudio, Lhis
view exisLed beíore íor H1ML and ASPX documenLs only. WiLh visual SLudio 2005, iL has
been exLended Lo Windows Forms. 1o have Lhe DocumenL OuLline view in your lDF, simply
go Lo view/OLher Windows/DocumenL OuLline or press CLrl-AlL-1. 1his view leLs you man-
age all conLrols on your íorm. lL shows how Lhe conLrols are arranged on Lhe screen and
which conLrol(s) belongs Lo anoLher conLrol. For insLance, righL now you cannoL see Lhe Web
8rowser conLrol, buL ií you enable DocumenL OuLline view, you'll see LhaL Lhe Web 8rowser
conLrol is aL Lhe same level as Lhe newly added Lool sLrip conLainer (see Figure 6-íí). 1o
rearrange Lhe order and change how Lhe conLrols are displayed, íollow Lhe nexL sLeps.
The z-order is the controI's posi-
tion reIative to the other windows
or controIs on the screen, think
of it as the third dimension or
being on top of or beneath other
controIs.
ABG 8
Iigµre 6-II
G[X7bVh`XagBhg_\aXj\aWbjYbeg[X@lBja
5ebjfXeceb]XVg
C06622132.indd 95 10/18/05 7:23:37 PM
96 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
TO k£AkkANG£ TH£ OkD£k Ol CONTkOLS
$
|r ¦|e DocJner¦ CJ¦||re .|rdo.. se|ec¦ ¦|e \eb |·o.se· cor¦·o| ca||ed myßrowser ard d·ag |¦ |Js¦
be|o. ¦|e ¦oo| s¦·|p cor¦a|re· cor¦er¦ pare| ca||ed na|r|o·nToo|S¦·|pCor¦a|re·Cor¦er¦|are| (\|er
yoJ d·ag ¦|e \eb |·o.se· cor¦·o|. a b|ac| ||re |rd|ca¦es .|e·e ¦|e cor¦·o| .||| be d·opped |¦ yoJ ·e|ease
¦|e noJse bJ¦¦or)
%
No. d|sp|ay ¦|e ¦o·n aga|r T|e \eb |·o.se· cor¦·o| |s |r ¦|e n|dd|e o¦ ¦|e ¦o·n |J¦ as yoJ car see.
¦|e \erJ S¦·|p cor¦·o| |s ro¦ |r ¦|e ¦oo| s¦·|p cor¦a|re· |epea¦ s¦ep 1 ¦o· ¦|e \erJ S¦·|p cor¦·o|. bJ¦
|rs¦ead o¦ d·opp|rg |¦ |r ¦|e cor¦er¦ pare|. d·op |¦ |r ¦|e ¦op pare| o¦ ¦|e ¦oo| s¦·|p cor¦a|re· (na|r|o·n
Too|S¦·|pCor¦a|re·TopToo|S¦·|p|are|)
Now Lhe only Lhing missing írom Lhe new menu sLrip is a doLLed grip like Lhe one in Lhe
OuLlook íigure. WiLhouL Lhis grip, a user is unable Lo selecL Lhe menu sLrip aL all, iL is íixed in
Lhe Lop panel.
TO ADD A DOTT£D GklP TO TH£ M£NU STklP
$
|·on ¦|e DocJner¦ CJ¦||re .|rdo.. se|ec¦ ¦|e nerJ s¦·|p ca||ed msßrowser. go ¦o ¦|e |·ope·¦|es
.|rdo.. ard se¦ ¦|e C·|pS¦y|e p·ope·¦y ¦o VìsìbIe
%
|Jr ¦|e app||ca¦|or by p·ess|rg f5 \ove ¦|e nerJ s¦·|p ¦·on ore pare| ¦o ¦|e o¦|e· `oJ ro. |ave
ar app||ca¦|or as coo| as CJ¦|oo|
Adding a Status Bar to Your Browser
¥our applicaLion is becoming rich in íeaLures, buL Lo geL iL closer Lo mosL Windows applica-
Lions, you need a sLaLus bar Lo reporL iníormaLion abouL whaL's going on aL any momenL
during Lhe execuLion. 1o accomplish Lhis in your browser, you'll add a SLaLus SLrip conLrol
and, wiLhin Lhis sLaLus sLrip, you'll add a progress bar.
lf your appIication design
demands it and if you want to
constrain the user in any way, you
can aIso hide paneIs and prevent
users from docking any tooI strip
or menu strip in a paneI. Let's
use the current appIication as an
exampIe. lf you want to do this,
seIect the tooI strip container con-
troI named mainlormTooIStrip-
Container. You can either seIect it
from the Properties window or the
Document OutIine. Then modify
the visibIe property of the paneI
you want to hide. lor instance, if
you wouId Iike to hide the bottom
paneI, set the BottomTooIStripPan-
eIVisibIe property to faIse.
@BE8 < A9 B
C06622132.indd 96 10/18/05 7:23:37 PM
Chapter 6. Modify Your web Browser Now! 97
TO ADD A STATUS STklP CONTkOL AND A PkOGk£SS BAk
$
C||c| ¦|e bo¦¦on pare| |ard|e ¦o e.pard |¦ (No¦e ¦|a¦ ¦|e g|yp| a··o. d|·ec¦|or ·eve·ses .|er yoJ
c||c| |¦) T|e ¦oo| s¦·|p cor¦a|re·'s bo¦¦on pare| appea·s as a b|Je s¦·|p
%
D·ag a S¦a¦Js S¦·|p cor¦·o| ¦o ¦|e ¦oo| s¦·|p cor¦a|re·'s bo¦¦on pare| /¦¦e· yoJ d·op |¦ or¦o ¦|e
bo¦¦on pare|. |¦ s|oJ|d e.pard ¦o cove· ¦|e .|o|e pare| sJ·¦ace
&

|erane ¦|e S¦a¦Js S¦·|p cor¦·o| ¦·on S¦a¦JsS¦·|p1 ¦o sscßrowser
'
C|arge ¦|e RenderMode p·ope·¦y o¦ ¦|e S¦a¦Js S¦·|p cor¦·o| ¦o ProfessìonaI T||s .||| a||o. ¦|e
app||ca¦|or ¦o p·eser¦ a s¦a¦Js ba· ¦o ¦|e Jse· Js|rg ¦|e ope·a¦|rg sys¦en co|o·s |o· |rs¦arce. |¦ ¦|e
¦|enes |r \|rdo.s `| a·e b|Je. ¦|er ¦|e s¦a¦Js ba· .||| be b|Je as .e||
(
/dd a |abe| cor¦·o| ¦o ¦|e s¦a¦Js s¦·|p by c||c||rg or ¦|e do.r a··o. o¦ ¦|e S¦a¦Js S¦·|p /dd cor¦·o|
bJ¦¦or ard ¦|er se|ec¦|rg StatusLabeI
)

|erane ¦|e cor¦·o| ¦·on Too|S¦·|pS¦a¦Js|abe|1 ¦o IbIAppIìcatìonStatus
*

/dd a p·og·ess ba· ¦o yoJ· s¦a¦Js s¦·|p |Js¦ as yoJ d|d ¦o· ¦|e |abe| cor¦·o|
+

|erane ¦|e cor¦·o| ¦·on Too|S¦·|p|·og·ess|a·1 ¦o pbStatus
When Lhe sLaLus sLrip and Lhe progress bar are displayed Lo Lhe user, Lhey usually bring
imporLanL iníormaLion abouL Lhe evenLs LhaL are occurring during Lhe execuLion. 1hink oí iL
like a leLLer arriving aL your house. ¥ou hear Lhe mail Lruck and realize Lhe mail has arrived.
1his is Lhe evenL LhaL is raised. ¥ou open your mailbox and Lhe envelope Lo learn LhaL iL's
your crediL card bill. 1he bill is one oí Lhe pieces oí iníormaLion LhaL comes along wiLh Lhe
evenL. 1o analagously populaLe Lhe conLrols in Lhe sLaLus sLrip, you'll have Lo coníigure your
applicaLion Lo exLracL Lhis iníormaLion írom all conLrols (i.e., envelope) when evenLs are hap-
pening (i.e., mail Lruck arriving). And you'll do LhaL programmaLically by wriLing code in
evenL handlers.
C06622132.indd 97 10/18/05 7:23:38 PM
98 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
TO POPULAT£ CONTkOLS wlTH lNlOkMATlON
$
Cr ¦|e des|gr sJ·¦ace. se|ec¦ ¦|e My Own ßrowser ¦o·n by c||c||rg |¦s ¦|¦|e ba· |oo| |r ¦|e |·ope·¦|es
.|rdo. ¦o na|e sJ·e ¦|e |·o.se· ¦o·n |s se|ec¦ed ard c||c| ¦|e fvent bJ¦¦or (ye||o. ||g|¦r|rg) |r ¦|e
|·ope·¦|es .|rdo. ||rd ¦|e Load ever¦ ard doJb|ec||c| |¦ ¦o b·|rg Jp ¦|e de¦aJ|¦ ever¦ |ard|e· |·o.se·_
|oad (T|e ¦o·n |oad ever¦ |s ·a|sed |Js¦ be¦o·e ¦|e ¦o·n d|sp|ays ¦o ¦|e Jse· So. |¦'s a good p|ace ¦o
c|arge p·ope·¦|es ¦|a¦ a¦¦ec¦ ¦|e v|sJa| aspec¦s o¦ a ¦o·n)
%
/dd ¦|e ¦o||o.|rg code ¦o ¦|e ever¦ (|·o.se·_|oad) ¦o nod|¦y ¦|e s¦a¦Js nessage |abe| (|b|/pp||ca
¦|orS¦a¦Js) |r ¦|e s¦a¦Js s¦·|p
Me.]b]App]1caf1on5fafus.1exf = ¨Ready¨
`oJ'|| ro. a¦¦ac| sone code ¦o ¦|e p·og·ess ba· ard nod|¦y ¦|e |abe| or ¦|e s¦a¦Js s¦·|p ¦o |rd|ca¦e .|e·e
¦|e Jse· |s rav|ga¦|rg ¦o \|er ¦|e page |s ¦J||y do.r|oaded ¦o ¦|e c||er¦ |C. yoJ'|| ·ese¦ ¦|e |abe| cor¦er¦
|r ¦|e s¦a¦Js s¦·|p ¦o ¦|e .o·d '|eady' `oJ'|| a|so nod|¦y ¦|e b·o.se· ¦|¦|e ¦o |rc|Jde ¦|e ||| .|e·e ¦|e Jse·
rav|ga¦ed ¦o \|ereve· ¦|e C| bJ¦¦or |s c||c|ed |r ¦|e Nav|ga¦e ¦o·n. ¦|e \eb |·o.se· cor¦·o| raned
ny|·o.se· ·a|ses ¦|e Nav|ga¦|rg ever¦ T|a¦'s .|e·e yoJ'|| s¦a·¦ .·|¦|rg code
&
Se|ec¦ ¦|e myßrowser cor¦·o| ard ¦|er go ¦o ¦|e |ver¦s ||s¦ |r ¦|e |·ope·¦|es .|rdo. DoJb|ec||c|
¦|e Navìgatìng ever¦ ard er¦e· ¦|e ¦o||o.|rg code
` Mod1fy1ng fhe ]abe] 1n fhe sfafus sf¡1p W1fh fhe uRL enfe¡ed by fhe use¡
Me.]b]App]1caf1on5fafus.1exf = ¨Nav1gaf1ng fo: ¨ + e.u¡].hosf.1o5f¡1ng1}
Crce ¦|e Jse· er¦e·s a ||| ard ¦|e docJner¦ |s be|rg do.r|oaded. ¦|e p·og·ess ba· .||| reed ¦o Jpda¦e
|e·|od|ca||y. ¦|e \eb |·o.se· cor¦·o| ·a|ses ¦|e |·og·essC|arged ever¦ T|a¦'s .|e·e yoJ'|| Jpda¦e ¦|e
p·og·ess ba· |r ¦|e s¦a¦Js s¦·|p
'
\a|e sJ·e yoJ |ave ¦|e myßrowser cor¦·o| se|ec¦ed |r ¦|e |·ope·¦|es .|rdo. ard ¦|er go ¦o ¦|e
|ver¦ ||s¦ DoJb|ec||c| ¦|e ProgressChanged ever¦ |r¦e· ¦|e ¦o||o.|rg code (_bb^Tgg[XVb``Xagf
gbhaWXefgTaWg[XfbheVXVbWX)
` 1he Cu¡¡enfP¡og¡ess va¡1ab]e f¡om fhe ¡a1sed evenf
` g1ves you fhe cu¡¡enf numbe¡ of byfes a]¡eady doWn]oaded
` Wh1]e fhe Max1mumP¡og¡ess 1s fhe fofa] numbe¡ of byfes
` fo be doWn]oaded
lf e.Cu¡¡enfP¡og¡ess < e.Max1mumP¡og¡ess 1hen
` Check 1f fhe cu¡¡enf p¡og¡ess 1n fhe p¡og¡ess ba¡
C06622132.indd 98 10/18/05 7:23:38 PM
Chapter 6. Modify Your web Browser Now! 99
` 1s >= fo fhe max1mum 1f yes ¡esef 1f W1fh fhe m1n
lf pb5fafus.va]ue >= pb5fafus.Max1mum 1hen
pb5fafus.va]ue = pb5fafus.M1n1mum
L]se
` Jusf 1nc¡ease fhe p¡og¡ess ba¡
pb5fafus.Pe¡fo¡m5fep1}
Lnd lf
L]se
` When fhe documenf 1s fu]]y doWn]oaded
` ¡esef fhe p¡og¡ess ba¡ fo fhe m1n 10}
pb5fafus.va]ue = pb5fafus.M1n1mum
Lnd lf
\|er ¦|e Jse·'s docJner¦ |s ¦J||y do.r|oaded. ¦|e b·o.se· .||| ·a|se ¦|e DocJner¦Conp|e¦ed ever¦
\|er ¦||s ever¦ |s ·a|sed. ¦|e app||ca¦|or ¦|¦|e reeds ¦o be Jpda¦ed ¦o ¦|e cJ··er¦ ||| ard ¦|e app||ca¦|or
s¦a¦Js |abe| |r ¦|e s¦a¦Js s¦·|p .||| reed ¦o c|arge ¦o ¦|e '|eady' s¦a¦e
(
|r ¦|e ny|·o.se· ever¦ ||s¦. doJb|ec||c| ¦|e DocumentCompIeted ever¦ T|er add ¦|e ¦o||o.|rg
code ¦o |¦
`Gef App]1caf1on f1f]e us1ng fhe My namespace
lf My.App]1caf1on.lnfo.11f]e <> ¨¨ 1hen
Me.1exf = My.App]1caf1on.lnfo.11f]e + ¨ - ¨ + e.u¡].hosf.1o5f¡1ng1}
L]se
`lf fhe app]1caf1on f1f]e 1s m1ss1ng.
`use fhe app]1caf1on name. W1fhouf fhe exfens1on
Me.1exf = 5ysfem.lO.Pafh.GefI1]eNameW1fhoufLxfens1on1
My.App]1caf1on.lnfo.Assemb]yName} + ¨ - ¨ + e.u¡].hosf.1o5f¡1ng1}
Lnd lf
Me.]b]App]1caf1on5fafus.1exf = ¨Ready¨
/s yoJ car see. |r ¦||s code yoJ've |Js¦ ·eJsed s|n||a· soJ·ce code Jsed |r ¦|e /boJ¦ bo. ard ¦|e sp|as|
sc·eer d|a|og sec¦|ors |¦ Jses ¦|e Bn ranespace c|asses ard ne¦|ods
)
Save a|| ¦||es ard ·Jr ¦|e app||ca¦|or ro. `oJ s|oJ|d |ave a .o·||rg p·og·ess ba·. ard a|| re. |r¦o·
na¦|or s|oJ|d be d|sp|ayed nod|¦|ed ¦|¦|e .|rdo. ard s¦a¦Js s¦·|p |abe|
C06622132.indd 99 10/18/05 7:23:38 PM
l00 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
PersonaIize Your AppIication with windows lcons
ln Lhis secLion, you'll conLinue Lo personalize your browser by adding some icons LhaL come
írom known MicrosoíL applicaLions. AíLer Lhis secLion, you'll have a working lnLerneL browser
wiLh mosL navigaLional íeaLures íully implemenLed-maybe noL wiLh all Lhe íuncLionaliLy oí
lnLerneL Fxplorer, buL you should be proud oí yourselí. Look aL Figure 6-í2 Lo see whaL you
will have accomplished aíLer Lhis secLion.
Iigµre 6-I2
LbheUebjfXeTgg[XXaWbYg[\ffXVg\ba
C06622132.indd 100 10/18/05 7:23:39 PM
Chapter 6. Modify Your web Browser Now! l0l
As you can see, you'll implemenL a nice lisL oí íeaLures in Lhis secLion. Here's whaL you're
going Lo accomplish.
N Link all buLLons Lo browser íuncLionaliLies
N Manage Lhe Co buLLon and Lhe FnLer key on Lhe address LexL box in Lhe Lool sLrip
N Change Lhe browser íorm icon Lo Lhe same globe icon LhaL you've seL íor Lhe applicaLion
icon on Lhe hard drive
FirsL, you'll add Lwo new Lool sLrips and all Lheir buLLons. ¥ou'll also add Lhe code Lo
handle all Lhose new buLLons. Fach Lime you add a buLLon, rename iL beíore wriLing Lhe
evenL handling code. ¥ou should do Lhis Lo make sure you have Lhe correcL variable names.
lL's jusL a maLLer oí consisLency and good pracLice.
TO ADD TOOL STklPS AND BUTTONS TO YOUk BkOwS£k
$
S¦a·¦ by add|rg ¦.o re. ¦oo| s¦·|ps ¦o ¦|e b·o.se·'s ¦o·n ·|g|¦ be|o. ¦|e nerJ s¦·|p Nane ¦|e ¦|·s¦
ore tsIcons ard ¦|e o¦|e· ore tsNavìgatìon |se ¦|e Document OutIìne ¦o na|e sJ·e ¦|ey a·e
Jrde· ¦|e ¦op pare| o¦ ¦|e ¦oo| s¦·|p cor¦a|re·
%
Se|ec¦ ¦|e tsIcons ¦oo| s¦·|p T|er. Js|rg ¦|e Add 1ooI Strìp Item d·opdo.r ||s¦. add s|. bJ¦¦ors.
ard rane ¦|en tsbßack. tsbforward. tsbStop. tsbRefresh. tsbHome. ard tsbSearch
&
To nod|¦y ¦|e |nage ¦o· eac| bJ¦¦or. c|arge ¦|e |nage p·ope·¦y o¦ ¦|e Too| S¦·|p |J¦¦or cor¦·o| by
c||c||rg ¦|e ||||ps|s bJ¦¦or () ¦o b·o.se or yoJ· |a·d d|s| ¦o· ¦|e |cor C· yoJ car ·|g|¦c||c| ¦|e |cor
|r ¦|e ¦oo| s¦·|p ard se|ec¦ Set Image. . . `oJ'|| ¦|er |ave ¦|e sane d|a|og bo. ¦o |npo·¦ ¦|e |nage ¦||es
¦·on yoJ· |a·d d|s| T|e |nages ¦o· ¦|ese bJ¦¦ors a·e a|| |oca¦ed |r ¦|e |nages ¦o|de· Jrde· C|ap¦e· b
.|e·e yoJ've |rs¦a||ed yoJ· conpar|or cor¦er¦
'

|o· ¦|e ¦sbSea·c| bJ¦¦or. ·|g|¦c||c| ¦|e bJ¦¦or. se|ec¦ DìspIay StyIe ard se¦ |¦ ¦o ImageAnd1ext
(

\od|¦y ¦|e Te.¦ p·ope·¦y o¦ ¦|e ¦sbSea·c| bJ¦¦or ¦o Search
l suggest that you rename your
buttons immediateIy when you
add them to make sure the event
handIing code has the correct
name. lt is possibIe to rename the
buttons Iater, but it's more tedious
because you have to perform extra
steps, which takes more time. lt's
just easier, cIeaner, and faster to
do it as soon as you create the
controIs.
@BE8 < A9 B
C06622132.indd 101 10/18/05 7:23:40 PM
l02 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
)
|o· eac| bJ¦¦or. add ¦|e ·espec¦|ve ¦Jrc¦|ora||¦y (`oJ'|| see |o. easy |¦ |s ¦o add ¦|e des|·ed ¦Jrc¦|or
a||¦y becaJse ¦|e \eb |·o.se· cor¦·o| .as .e|| des|gred) DoJb|ec||c| ore bJ¦¦or a¦¦e· ¦|e o¦|e·
ard yoJ'|| ge¦ ¦o ¦|e C||c| ever¦ ¦o· eac| ore |r eac| C||c| ever¦. add ¦|e ¦o||o.|rg code See be|o. ¦o·
¦|e co··espord|rg bJ¦¦or rane ard ever¦ soJ·ce code
¦sb|ac| my8¡oWse¡.Go8ack1}
¦sb|o·.a·d my8¡oWse¡.GoIo¡Wa¡d1}
¦sbS¦op my8¡oWse¡.5fop1}
¦sb|e¦·es| my8¡oWse¡.Ref¡esh1}
¦sb|one my8¡oWse¡.Gohome1}
¦sbSea·c| my8¡oWse¡.Go5ea¡ch1}
*
|Jr ¦|e app||ca¦|or ard de¦e·n|re |¦ ¦|e bJ¦¦ors a·e .o·||rg |ve·y¦||rg s|oJ|d be .o·||rg e.cep¦
¦o· ¦|e rav|ga¦|or bJ¦¦ors
¥ou'll now modiíy Lhe behavior oí Lhe Lwo navigaLion buLLons in Lhe Lsblcons Lool sLrip Lo
make sure Lhey're enabled only when Lhey should be-LhaL is, when Lhere are pages in Lhe
browser's hisLory. When you sLarL Lhe applicaLion, Lhe buLLons should be disabled. 1he besL
place Lo puL Lhis code is Lhe Load evenL oí Lhe 8rowser íorm. lL's a good place because Lhe
evenL will happen righL beíore Lhe user acLually sees Lhe íorm. NexL, you need Lo Lhink abouL
where you should puL Lhe code LhaL will enable and disable Lhe Lwo navigaLion buLLons. 1he
ideal place íor Lhe validaLion code is where Lhe navigaLion occurs because you know aL LhaL
momenL Lhe browser will navigaLe Lo a new URL.
C06622132.indd 102 10/18/05 7:23:40 PM
Chapter 6. Modify Your web Browser Now! l03
TO MODllY TH£ B£HAVlOk Ol NAVlGATlON BUTTONS
$ |r |·o.se·vb. nod|¦y |·o.se·_|oad ard ny|·o.se·_Nav|ga¦|rg ¦o |oo| |||e ¦|e ¦o||o.|rg
P¡1vafe 5ub 8¡oWse¡Load18yva] sende¡ As 5ysfem.Ob1ecf. 8yva] e As 5ysfem.LvenfA¡gs} hand]es
My8ase.Load
` D1sab]1ng bofh nav1gaf1on buffons 1n fhe lcons foo] sf¡1p
Me.fsb8ack.Lnab]ed = Ia]se
Me.fsbIo¡Wa¡d.Lnab]ed = Ia]se
Me.]b]App]1caf1on5fafus.1exf = ¨Ready¨
Lnd 5ub
P¡1vafe 5ub my8¡oWse¡Nav1gaf1ng18yva] sende¡ As 5ysfem.Ob1ecf. 8yva] e As 5ysfem.W1ndoWs.
Io¡ms.Web8¡oWse¡Nav1gaf1ngLvenfA¡gs} hand]es my8¡oWse¡.Nav1gaf1ng
` Add fhe code fo enab]e o¡ d1sab]e Wheneve¡ fhe¡e a¡e uRLs
` 1n fhe b¡oWs1ng sess1on`s h1sfo¡y
lf my8¡oWse¡.CanGo8ack 1hen
fsb8ack.Lnab]ed = 1¡ue
L]se
fsb8ack.Lnab]ed = Ia]se
Lnd lf
lf my8¡oWse¡.CanGoIo¡Wa¡d 1hen
fsbIo¡Wa¡d.Lnab]ed = 1¡ue
L]se
fsbIo¡Wa¡d.Lnab]ed = Ia]se
Lnd lf
` Mod1fy1ng fhe ]abe] 1n fhe sfafus sf¡1p W1fh fhe uRL enfe¡ed by fhe use¡
Me.]b]App]1caf1on5fafus.1exf = ¨Nav1gaf1ng fo: ¨ + e.u¡].hosf.1o5f¡1ng1}
Lnd 5ub
% |Jr ¦|e app||ca¦|or ¦o de¦e·n|re |¦ ¦|e bJ¦¦ors be|ave co··ec¦|y ro.
C06622132.indd 103 10/18/05 7:23:41 PM
l04 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
NexL, you'll add Lhe names and conLrols Lo Lhe LsNavigaLion Lool sLrip as you did íor Lhe
previous Lool sLrip. However, Lhis Lime insLead oí jusL adding some Lool sLrip buLLons, you'll
add diííerenL Lypes oí conLrols.
For insLance, you'll modiíy Lhe browser Lo navigaLe Lo Lhe URL speciíied in Lhe LexL box
when Lhe user clicks FnLer. ¥ou'll also modiíy Lhe behavior oí clicking Lhe Co buLLon Lo make
sure iL does Lhe same Lhing.
TO ADD N£w CONTkOLS TO TH£ TSNAVlGATlON TOOL STklP
$
|se ¦|e /dd Too| S¦·|p |¦en d·opdo.r ||s¦ or ¦|e ¦sNav|ga¦|or ¦oo|s¦·|p ard add ¦|e ¦o||o.|rg cor
¦·o|s ¦o ¦|e ¦oo| s¦·|p |abe|. Te.¦|o.. ard |J¦¦or Nane ¦|e cor¦·o|s ¦s|b|/dd·ess. ¦s¦b|·|. ard ¦sbCo
% |se ¦|e ¦ab|e be|o. ard se¦ ¦|e p·ope·¦|es o¦ ¦|e cor¦·o|s
ControI name Type Properties VaIue
LslblAddress 1oolSLripLabel 1exL
LsLbUrl 1oolSLrip1exL8ox Size.WidLh 850
LsbCo 1oolSLrip8uLLon 1exL Co
LsbCo 1oolSLrip8uLLon DisplaySLyle lmageAnd1exL
LsbCo 1oolSLrip8uLLon lmage Co.bmp
1he LsNavigaLion Lool sLrip is noL a dialog box wiLh an OK buLLon or a Cancel buLLon, so
you cannoL use Lhe AccepL8uLLon or Cancel8uLLon properLies. 1hereíore, you need Lo cap-
Lure anoLher evenL LhaL will be Lriggered whenever Lhe user presses FnLer.
1he KeyUp evenL is Lriggered whenever a key is released by Lhe user. For insLance, when-
ever Lhe user Lypes in a leLLer, he presses down Lhe key oí Lhe desired leLLer. When he releas-
es Lhe key, Lhe KeyUp evenL is Lriggered. 1he code you'll add in Lhe nexL exercise will deLer-
mine ií Lhe key Lhe user jusL released was Lhe FnLer key. lí iL was, a new meLhod called
NavigaLe1oUrl will accepL a sLring represenLing Lhe URL as a parameLer and navigaLe Lo Lhe
URL.
C06622132.indd 104 10/18/05 7:23:41 PM
Chapter 6. Modify Your web Browser Now! l05
¥ou'll use Lhe same meLhod íor Lhe Co buLLon. When you develop an applicaLion, you
never wanL Lo duplicaLe Lwo pieces oí code LhaL diííer only by a liLeral. ¥ou always wanL Lo
re-use Lhe source code whenever possible. 1he way Lo do LhaL is Lo creaLe meLhods LhaL are
generic enough Lo be used by more Lhan one componenL. Since Lhe NavigaLe1oUrl meLhod
has only one line oí code, you mighL be LempLed Lo say LhaL ií iL's almosL Lhe same one line
oí code, why use a meLhod¯ 1he answer is simply LhaL in Lhe íuLure you mighL have Lo add
some validaLion. lí LhaL one line oí code is repeaLed LhroughouL Lhe source code, you'll have
Lo updaLe iL in mulLiple places. However, ií Lhere is only one place where you have Lo modiíy
Lhe code, your soluLion is less prone Lo errors and a loL less Ledious.
TO CONllGUk£ TH£ BkOwS£k TO NAVlGAT£ TO TH£ UkL
$ Se|ec¦ ¦|e tstbUrI 1ooI Strìp ¦e.¦ bo.
%
|r ¦|e ever¦ ||s¦ |r ¦|e |·ope·¦|es .|rdo. ¦o· ¦s¦b|·|. doJb|ec||c| ¦|e KeyUp ever¦ |e|o. |s ¦|e code
¦o de¦e·n|re |¦ ¦|e Jse· p·essed ard ·e|eased ¦|e |r¦e· |ey ard a|so ¦|e ne¦|od Nav|ga¦eTo|·| ¦|a¦
.||| erab|e yoJ ¦o Jse ¦|e sane code |r no·e ¦|ar ore p|ace /dd ¦||s code ¦o ¦s¦b|·|_|ey|p ard add ¦|e
Nav|ga¦eTo|·| ne¦|od
P¡1vafe 5ub fsfbu¡]keyup18yva] sende¡ As 5ysfem.Ob1ecf. 8yva] e As 5ysfem.W1ndoWs.Io¡ms.
keyLvenfA¡gs} hand]es fsfbu¡].keyup
` e 1s of fype keyLvenfA¡gs and confa1ns a]] fhe
` 1nfo¡maf1on fhaf f¡1gge¡ed fhe evenf. 1he keyCode
` 1s one fhose 1nfo¡maf1on.
lf e.keyCode = keys.Lnfe¡ 1hen
Me.Nav1gafe1ou¡]1fsfbu¡].1exf}
Lnd lf
Lnd 5ub
P¡1vafe 5ub Nav1gafe1ou¡]18yva] u¡] As 5f¡1ng}
Me.my8¡oWse¡.Nav1gafe1u¡]}
Lnd 5ub
&
DoJb|ec||c| ¦|e Co bJ¦¦or or ¦|e ¦sNav|ga¦|or ¦oo| s¦·|p ard add ¦|e ¦o||o.|rg code ¦o ¦|e
¦sbCo_C||c| ever¦ p·ocedJ·e (No¦|ce ¦|a¦ ¦||s |s ¦|e Nav|ga¦eTo|·| ne¦|od)
P¡1vafe 5ub fsbGoC]1ck18yva] sende¡ As 5ysfem.Ob1ecf. 8yva] e As 5ysfem.LvenfA¡gs} hand]es
fsbGo.C]1ck
Me.Nav1gafe1ou¡]1fsfbu¡].1exf}
Lnd 5ub
By the way, there's more than one
event that is being triggered by
pressing the £nter key, but the
one that you'II trap is the KeyUp
event.
ABG 8
C06622132.indd 105 10/18/05 7:23:41 PM
l06 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
¥ou can now modiíy anoLher piece oí code, Lhe NavigaLe menu click evenL. ¥ou simply
have Lo modiíy Lhe code so LhaL iL calls Lhe NavigaLe1oUrl meLhod, as shown below.
P¡1vafe 5ub Nav1gafe1oo]5f¡1pMenulfemC]1ck18yva] sende¡ As 5ysfem.Ob1ecf. 8yva] e As 5ysfem.
LvenfA¡gs} hand]es Nav1gafe1oo]5f¡1pMenulfem.C]1ck
lf 1Nav1gafeW1ndoW.5hoWD1a]og1} = W1ndoWs.Io¡ms.D1a]ogResu]f.Ok} 1hen
Me.Nav1gafe1ou¡]1Nav1gafeW1ndoW.fxfu¡].1exf}
Lnd lf
Nav1gafeW1ndoW.fxfu¡].1exf = ¨¨
Lnd 5ub
TO MODllY TH£ BkOwS£k lOkM lCON
Finally, you'll modiíy Lhe 8rowser Form icon so LhaL Lhe user sees a globe when Lhe
browser is running or minimized.
$
Se|ec¦ ¦|e ßrowser ¦o·n ard ¦|er |oo| ¦o· |cor
|·ope·¦y |r ¦|e |·ope·¦|es .|rdo. |¦ yoJ see or|y
ever¦s |r ¦|e |·ope·¦|es .|rdo.. c||c| ¦|e Propertìes bJ¦¦or
a¦ ¦|e ¦op o¦ ¦|e |·ope·¦|es .|rdo. C||c| ¦|e ||||ps|s bJ¦¦or
() ¦o b·o.se ¦o· ¦|e gIobe.ìco |r ¦|e C|ap¦e· b d|·ec¦o·y |r
¦|e |nages ¦o|de· Jrde· ¦|e C|ap¦e· b d|·ec¦o·y
1he resulL oí your hard labor is Lhe íinished
producL-Lhe My Own 8rowser applicaLion, as
shown in Figure 6-í8.
Iigµre 6-IJ
9\a\f[XWcebWhVgÂg[X@lBja5ebjfXeTcc_\VTg\ba
C06622132.indd 106 10/18/05 7:23:42 PM
Chapter 6. Modify Your web Browser Now! l07
ln Summary...
ln Lhis chapLer, you Look a simple applicaLion and upgraded iL Lo creaLe a proíessional-
looking applicaLion wiLh many nice íeaLures íor your users. ¥ou learned Lo add a splash
screen Lo your applicaLion and Lo work wiLh dialog boxes. ¥ou creaLed an AbouL box and a
NavigaLe dialog box Lo allow your user Lo navigaLe Lo a URL, you added an auLocompleLe
íeaLure Lo your LexL boxes and Lhe auLosuggesL/append íeaLure using Lhe browser's URLs
hisLory. ¥ou Lhen added Lool sLrips, progress bars, and icons írom Windows. ¥ou dynamically
managed conLrols, and you learned a loL abouL new evenLs and how Lo handle Lhem using
evenL argumenLs.
ln Lhe nexL chapLer, you'll learn Lechniques Lo use when Lhings don'L go well, LhaL is,
you'll learn Lhe arL oí debugging code. ¥ou'll also learn abouL Lhe FdiL and ConLinue íeaLure,
Lhe new daLa visualizers, Lips and Lricks, and much more.
C06622132.indd 107 10/18/05 7:23:42 PM
C06622132.indd 108 10/18/05 7:23:42 PM
l09
7XUhZZ\aZTa
4cc_\VTg\ba$$#
6[TcgXe*
9\k\aZg[X
5eb^Xa5_bV^f
,
As you'll discover more and more, when you develop an applicaLion, you
rarely succeed on your íirsL aLLempL. MosL oí Lhe Lime (and especially when
you sLarL developing applicaLions), Lhe process goes like Lhis. brainsLorm on
paper, look aL Lhe users' needs (oíLen yours), períorm some analysis, proLo-
Lype, design, develop, LesL, íix bugs, LesL Lhe producL again, and íinally
release iL Lo people. 1his is a high-level view oí Lhe process, iL can be much
more complicaLed or simpliíied. lL all depends on Lhe complexiLy oí Lhe proj-
ecL, Lhe number oí people involved, and so on. One Lhing is cerLain. you
always need Lo debug your applicaLions, and MicrosoíL
®
visual 8asic
®
2005
provides many Lools Lo help you íix your bugs íasLer.
C07622132.indd 109 10/18/05 7:31:33 PM
ll0 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
Debugging an AppIication
1o learn Lhe Lools and Lechniques Lo debug your applicaLions, l've creaLed a sample applica-
Lion LhaL you'll use íor Lhis chapLer. lí you insLalled Lhe companion conLenL aL Lhe deíaulL
locaLion, iL should be aL Lhe íollowing locaLion on your hard drive. My DocumenLs\MicrosoíL
Press\v8 2005 Fxpress\ChapLer7\. Look íor a íolder named Debugger SLarL under Lhe
ChapLer 7 íolder. Double-click Lhe Debugger.sln soluLion.
1his soluLion conLains new iLems LhaL you have noL seen yeL, wiLh Lhe íirsL one being
a soluLion wiLh more Lhan one projecL. 1his is a common pracLice while developing
applicaLions. ln Lhis case, Lhe soluLion (named Debugger) conLains Lwo projecLs. a MicrosoíL
Windows
®
Forms applicaLion named Debugger and a managed library named MyLibrary
(managed DLL). 1he acronym DLL sLands íor Dynamic Link Library. A DLL is a library oí
íuncLions LhaL are called dynamically and as needed by an applicaLion. A DLL doesn'L
conLain a main enLry poinL and cannoL be execuLed by iLselí. Also, a DLL can be used by
mulLiple applicaLions aL Lhe same Lime.
1he second new Lhing is LhaL Lhe projecL Debugger has a Lype oí íile LhaL you haven'L
seen yeL. a LexL íile. ¥ou can have mulLiple diííerenL íiles in your projecLs, and a LexL íile is
noL uncommon. ln Lhis case, Lhe LexL íile is used by one oí Lhe meLhods called by Lhe
debugger.exe applicaLion. 1he LexL íile will be used by Lhis applicaLion, so Lo have iL in Lhe
ouLpuL íolder, you need Lo selecL iL in Lhe SoluLion Fxplorer and Lhen change Lhe Copy 1o
OuLpuL DirecLory properLy Lo Copy lí Newer.
Using a DLL in an AppIication
When you design an applicaLion, you usually have more Lhan one componenL. ln many
cases, Lhe componenLs are new classes (Lypes). lL is good pracLice Lo have Lhose Lypes in a
separaLe source code íile insLead oí keeping Lhem wiLh Lhe user inLeríace code. OíLen, Lhe
classes are grouped in a single library or DLL.
When you wanL Lo use a Lype írom a library, you need Lo make your applicaLion aware
oí all Lhe Lypes and meLhods conLained in LhaL library by adding a reference Lo iL in Lhe
applicaLion.
The program is excIusiveIy for
educationaI purposes of this chap-
ter. lt doesn't do anything interest-
ing except teach debugging.
ABG 8
C07622132.indd 110 10/18/05 7:31:33 PM
Chapter 7. lixing the Broken BIocks lll
Adding a keference to Your AppIication
1o add new reíerences Lo your applicaLion, íollow Lhe sLeps here.
TO ADD A k£l£k£NC£ TO YOUk APPLlCATlON
$

Se|ec¦ ¦|e p·o|ec¦ .|e·e yoJ .ar¦ ¦o add ¦|e ·e¦e·erce. |r ¦||s case. se|ec¦ Debugger
%

||g|¦c||c| Add Reference. . . |oo| a¦ ||gJ·e ¯1 ¦o na|e sJ·e yoJ'·e a¦ ¦|e ·|g|¦ p|ace
/s yoJ car see ¦·on ¦|e ¦abs or ¦|e d|a|og bo. ¦|a¦ appea·s. ¦|e ·e¦e·erces car cone ¦·on nJ|¦|p|e
soJ·ces
&
Se|ec¦ ¦|e Projects ¦ab ard ¦|er se|ec¦ ¦|e MyLìbrary p·o|ec¦. .||c| cor¦a|rs ¦|e naraged D||
|·ess OK ¦o add ¦|e ·e¦e·erce ¦o yoJ· p·o|ec¦
8ecause Lhe DLL is in Lhe same soluLion and you jusL added a reíerence oí LhaL DLL Lo
your applicaLion, MicrosoíL visual SLudio
®
now knows Lhere is a dependency beLween Lhe
Lwo and will always build Lhe DLL íirsL so LhaL your applicaLion builds Lhe binary wiLh Lhe
mosL up-Lo-daLe DLL possible.
¥ou can veriíy LhaL Lhe reíerence has been inserLed in Lwo diííerenL ways.
N Use Lhe ProjecL Designer and selecL Lhe Reíerences Lab on Lhe leíL side as shown in
Figure 7-2.
N Use Lhe Show All íiles buLLon in Lhe SoluLion Fxplorer and expand Lhe Reíerences.
;^\jgZ,"&
4WWEXYXeXaVX!!!`XahV[b\VXYeb`g[X
7XUhZZXeceb]XVg
;^\jgZ,"'
Ceb]XVg7Xf\ZaXe-EXYXeXaVXfgTU
j\g[g[X@l?\UeTeleXYXeXaVX
C07622132.indd 111 10/18/05 7:31:34 PM
ll2 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
Using one oí Lhese meLhods, you'll see Lhe reíerence Lo MyLibrary. When you're done
adding Lhe reíerence, your applicaLion can creaLe insLances oí Lhe new Lypes LhaL are builL in
Lhe DLL and use Lhem appropriaLely. 1he build process (compiler and linker) will now accepL
Lhe use oí Lhose new Lypes, buL íor visual SLudio Lo have Lhose new Lypes available via
lnLelliSense® and íor Lhe compiler Lo know abouL Lhose new Lypes, one more sLep is
required. ¥ou mighL already have seen Lhe íirsL line oí code in Lhe 1esLApplicaLion.vb íile. 1he
line reads lmporLs MyLibrary.
WhaL Lhe lmporLs sLaLemenL essenLially does is imporL namespaces or programming ele-
menLs, like new Lypes, LhaL are deíined in reíerenced projecLs and assemblies. 8y adding Lhis
line oí code, you're Lelling visual SLudio Lo look inLo LhaL assembly íor Lhe meLadaLa LhaL will
enable lnLelliSense Lo be populaLed wiLh Lhe public/proLecLed elemenLs. AíLer adding Lhis
line, you'll have access Lo Lhose iLems whenever you have an insLance oí one oí Lhe Lypes
builL in Lhe library.
Breakpoints, LocaIs, £dit and Continue, and VisuaIizers
1here is no beLLer way Lo dive inLo Lhis subjecL Lhan by looking aL and going Lhrough Lhe
code. lí Lhe 1esLApplicaLion.vb source code íile is noL already open, open iL by righL-clicking
on Lhe 1esLApplicaLion.vb íile and selecLing view Code. ¥ou should see red doLs on Lhe leíL-
hand side oí Lhe screen, Lhose red doLs are breakpoints. Figure 7-8 shows Lhe source code
and Lhe breakpoinLs.
;^\jgZ,"(
FbheVXVbWXTaWUeXT^cb\agfYeb`g[X
GXfg4cc_\VTg\baJ\aWbjfYbe`
lf the breakpoints don't appear,
you can add them by cIicking in
the Ieft margin.
G < C
C07622132.indd 112 10/18/05 7:31:35 PM
Chapter 7. lixing the Broken BIocks ll3
When Lhe debugger encounLers a breakpoinL, iL sLops execuLing Lhe applicaLion. ln Lhis
source code, Lhe breakpoinL is on Lhe call Lo Lhe Message8ox.Show(mySLring). 1here is
anoLher breakpoinL in Lhe Library.vb in Lhe íirsL line oí code oí Lhe Divide meLhod. ln Lhe íol-
lowing procedure, you will execuLe Lhe code and go Lhrough a debugging session.
1o debug an applicaLion, you can do one oí Lwo Lhings.
N Press F5 or Lhe SLarL Debugging buLLon.
1he program will sLarL execuLing normally. lí Lhere is a breakpoinL in Lhe source code, Lhe
execuLion will sLop Lhere. OLherwise, Lhe program will conLinue Lo execuLe unless Lhere is
an unhandled excepLion, or error.
N AlLernaLively, you can debug Lhe applicaLion by sLepping Lhrough Lhe code line by line.
1o do Lhis, press F8 or Lhe SLep lnLo buLLon.
For now, you'll jump Lo Lhe íirsL breakpoinL and execuLe Lhe code in Lhe sample program
using Lhe íirsL Lechnique.
TO B£GlN D£BUGGlNG AN APPLlCATlON
$ |·ess f5 o· ¦|e Start Debuggìng bJ¦¦or
%
`oJ .||| see a bJ¦¦or ¦|a¦ says 1ry Me! C||c| |¦ T|e code s|oJ|d s¦op e.ecJ¦|rg a¦ ¦|e ¦|·s¦ b·ea|po|r¦
|r ¦|e D|v|de ne¦|od. ard yoJ s|oJ|d see .|a¦ |s s|o.r |r ||gJ·e ¯4 T|e ye||o. ||g|||g|¦ed ||re
nears |¦ |s ¦|e re.¦ s¦a¦ener¦ ¦o be e.ecJ¦ed
;^\jgZ,")
8kXVhg\bafgbccXWTgg[XY\efgUeXT^cb\ag\ag[X
7\i\WX`Xg[bW
C07622132.indd 113 10/18/05 7:31:35 PM
ll4 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
¥ou're now in debugging mode, and you have access Lo a pleLhora oí Lools and daLa ele-
menLs abouL your applicaLion Lo help you undersLand whaL is happening when your applica-
Lion is execuLed. ¥ou can see Lhe conLenL oí local variables, parameLers, excepLion messages,
Lhe console window, and many more iLems you'll discover in Lhe nexL íew sLeps. All oí LhaL
iníormaLion is useíul when an applicaLion is noL behaving Lhe way iL should and you're Lry-
ing Lo undersLand why. WiLh all Lhe iníormaLion Lhe debugger provides, you can Lry Lo
uncover where Lhe problem lies and see why you have a bug. ¥ou can also use Lhe debugger
íor learning purposes as you are doing righL now. 1he debugger is an excellenL Leacher
when you're new Lo a Lechnology, language consLrucL, or when you're simply Lrying Lo
undersLand a new elemenL. lL is also common Lo use Lhe debugger Lo undersLand someone
else's code. lL is especially helpíul when you need Lo make modiíicaLions Lo exisLing code.
¥ou'll now look aL Lhe íirsL series oí daLa elemenLs oííered by Lhe debugger while you're
sLepping Lhrough your code. AL Lhe boLLom oí Lhe visual SLudio screen, you can see a series
oí Labs, which can include LocaIs, watch, lmmediate window, Output,and£rror List.lí
you don'L see Lhese Labs, you can open Lhese windows by selecLing Lhem on Lhe view and
Debug menus.MosL oí Lhese are noL visible when in ediLing mode. ¥ou saw in ChapLer 8
LhaL Lhe Frror LisL is only Lhere Lo show Lhe resulLs oí Lhe real-Lime compilaLion. While you're
debugging, Lhe Locals Lab is usually on Lop and shows Lhe currenL variables and objecL iníor-
maLion. Look aL Figure 7-5 Lo see Lhe Labs írom your debugging session.
;^\jgZ,"*
GTUfceXfXagWhe\aZWXUhZZ\aZ\aI\fhT_
FghW\b
C07622132.indd 114 10/18/05 7:31:36 PM
Chapter 7. lixing the Broken BIocks ll5
ln Lhe LocalsLab secLion, you can see íour elemenLs oí daLa írom your Divide meLhod.
Me, Divide, numberOne, andnumber1wo. 1hose are, in order, Lhe insLance oí Lhe currenL
objecL, Lhe reLurn value íor Lhe íuncLion named Divide, and Lhe Lwo parameLers. 1he debug-
ger deLecLs all elemenLs LhaL are in scope in LhaL meLhod and displays Lhem in Lhe Locals
Lab. 1he elemenLs in scope are all Lhe elemenLs LhaL are visible írom where Lhe insLrucLion
poinLer is locaLed. ln Lhis case, iL could be eiLher local variables or shared variables. 1his
means LhaL LhroughouL Lhe execuLion oí Lhe Divide meLhod, you'll be able Lo íollow Lhe val-
ues LhaL Lhose iLems will have. Now iL's your Lurn Lo see Lhis íor yourselí.
TO CONTlNU£ D£BUGGlNG TH£ APPLlCATlON
$ |·ess f8 o· ¦|e Step Into bJ¦¦or |·ess f8 aga|r ¦o ge¦ ¦o ¦|e d|v|s|or ope·a¦|or
\|||e debJgg|rg. yoJ car a|.ays |ove· ¦|e noJse ove· p·og·an e|ener¦s ¦o ge¦ ¦|e |r¦o·na¦|or yoJ o¦|
e·.|se ¦|rd |r ¦|e |oca|s ¦ab |o· |rs¦arce. |¦ yoJ |ove· yoJ· noJse ove· ¦|e rJnbe·Cre e|ener¦. yoJ'||
see ¦|e sane va|Je ¦|a¦ |s s|o.r |r ¦|e |oca|s ¦ab. as |||Js¦·a¦ed |r ||gJ·e ¯b
;^\jgZ,"+
:Xgg\aZg[XiT_hXbYg[Xah`UXeBaX_bVT_iTe\
TU_X\agjbW\YYXeXagjTlf
%
|.ecJ¦e ¦|e re.¦ ||re o¦ code Js|rg ore o¦ ¦|e ne¦|ods ner¦|ored be¦o·e by p·ess|rg e|¦|e· f8 o·
¦|e Step Into bJ¦¦or
|e¦'s e.p|a|r .|a¦ |s go|rg or ¦·on ¦|e conp||e·'s pe·spec¦|ve |r ¦|e |oca|s ¦ab. ¦|e D|v|de e|ener¦ (¦|e
·e¦J·r va|Je o¦ ¦|a¦ ¦Jrc¦|or) cor¦a|rs ¦|e ·esJ|¦ o¦ ' d|v|ded by `. .||c| s|oJ|d be 1
C07622132.indd 115 10/18/05 7:31:36 PM
ll6 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
|J¦ .|y does ¦|e debJgge· s|o. ´¯ `oJ |Js¦ ¦oJrd a bJg (No¦e |'ve |rse·¦ed ¦||s e··o· ¦o denors¦·a¦e
|o. sna|| n|s¦a|es car c·ea¦e b|gge· p·ob|ens) |¦ yoJ |ove· ¦|e noJse ove· ¦|e ¹ ope·a¦o·. yoJ'|| see ¦|e
.o·d YdjWaZ |·ess |1 ard sea·c| ¦o· ope·a¦o· |r |e|p `oJ'|| ¦|rd ¦|a¦ ¦|e ·esJ|¦ deperds or ¦|e ¦ypes o¦
¦|e ¦.o ope·ards Jsed |r ¦|e ope·a¦|or |r ¦||s case. ¦|e ¹ ope·a¦o· Jses ¦.o |r¦ege·s \|er yoJ Jse ¦|e ¹
ope·a¦o· .|¦| ¦.o |r¦ege·s. ¦|e ·esJ|¦ |s a doubIe T|e ·esJ|¦ |s ¦|e ¦J|| oJo¦|er¦ p|Js ¦|e ·ena|rde·. o· ´
/¦ ¦|e sane |oca¦|or |r ¦|e |e|p sys¦en. yoJ'|| see ¦|a¦ ¦|e |r¦ege· d|v|s|or ¦|a¦ ¦||s ¦Jrc¦|or .as sJpposed
¦o Jse |s ac¦Ja||y ¦|e \ ope·a¦o·
/¦ ¦||s po|r¦. yoJ .oJ|d ro·na||y p·ess ¦|e S¦op DebJgg|rg bJ¦¦or. bJ¦ ¦|e·e |s a re. ¦ea¦J·e |r \|sJa|
|as|c ´JJ' ¦|a¦ a||o.s yoJ ¦o nod|¦y yoJ· code ard ve·|¦y |nned|a¦e|y |¦ ¦|e c|arge yoJ na|e so|ves ¦|e
p·ob|en T||s ¦ea¦J·e |s ca||ed fdìt and Contìnue /s |¦s rane |np||es. ¦|e |d|¦ ard Cor¦|rJe ¦ea¦J·e |e¦s
yoJ ed|¦ ar e|ener¦ |r ¦|e app||ca¦|or ard cor¦|rJe ¦|e e.ecJ¦|or |r ¦ac¦. ro¦ or|y car yoJ do ¦||s. bJ¦ yoJ
car a|so nod|¦y ¦|e re.¦ |rs¦·Jc¦|or ¦o e.ecJ¦e. c|arge ¦|e va|Je o¦ a va·|ab|e. ard ·ee.ecJ¦e ¦|e |rs¦·Jc
¦|or aga|r T||s |s a |Jge ¦|ne save· |r sone cases becaJse yoJ dor'¦ |ave ¦o s¦op ¦|e e.ecJ¦|or. na|e ¦|e
c|arge. ·ebJ||d. ard ·ee.ecJ¦e ¦|e re. code `oJ car see ¦|e c|arges ·|g|¦ a.ay
&
Co ¦o ¦|e |e¦¦|ard s|de. .|e·e ¦|e ye||o. a··o. |rd|ca¦es ¦|e re.¦ |rs¦·Jc¦|or ¦o be e.ecJ¦ed \|er
yoJ |ove· yoJ· noJse ove· ¦|e ye||o. a··o.. yoJ s|oJ|d see a ¦·arspa·er¦ a··o. |rd|ca¦|rg ¦|a¦ yoJ
car nove ¦|e a··o. C||c| ard |o|d ¦|e ye||o. a··o. ard s||de |¦ Jp ard bac| ove· ¦|e d|v|s|or |rs¦·Jc¦|or
'
C|arge ¦|e ¹ ope·a¦o· ¦o ¦|e \ ope·a¦o· ard ¦|er ·ee.ecJ¦e ¦|e |rs¦·Jc¦|or by p·ess|rg f8 o· c||c||rg
¦|e Step Into bJ¦¦or
(
|oo| |r ¦|e |oca|s ¦ab ard yoJ'|| see ¦|a¦ ¦|e D|v|de e|ener¦ ro. |as ¦|e va|Je 1. .||c| |s co··ec¦
`oJ've |Js¦ ¦|.ed yoJ· ¦|·s¦ ·ea| bJg. yoJ'·e a be¦¦e· deve|ope· ro.
)
S¦ep |r¦o ¦|e code Jr¦|| yoJ see a nessage bo. .|¦| & ¦o· ¦|e ¦|·s¦ d|v|s|or |¦ yoJ dor'¦ see ¦|e nes
sage bo.. yoJ n|g|¦ reed ¦o s.|¦c| ¦o |¦ or ¦|e \|rdo.s Tas|ba· C||c| OK |r ¦|e nessage bo.
Cor¦|rJe s¦epp|rg |r¦o ¦|e code Jr¦|| yoJ ·e¦J·r ¦o ¦|e D|v|de ne¦|od .|¦| a re. se¦ o¦ va|Jes ard yoJ'·e
po|r¦|rg a¦ ¦|e ¦|·s¦ |rs¦·Jc¦|or |r ¦|e ne¦|od
When you're back Lo Lhe Divide meLhod, you will noL re-execuLe every insLrucLion
because you know LhaL Lhe meLhod should now execuLe correcLly. lnsLead, you'll sLep ouL oí
Lhe code using Lhe SLep OuL íuncLion. SLepping ouL doesn'L mean LhaL you'll skip Lhe execu-
Lion, sLepping ouL simply means LhaL Lhe debugger will execuLe all insLrucLions oí Lhe currenL
meLhod and go back Lo Lhe calling poinL. lí you do iL on a single insLrucLion, iL will simply
execuLe iL. 1o see Lhis in acLion, íollow Lhese sLeps.
There are some Iimitations to the
£dit and Continue edits you can
make. To see a compIete Iist, sim-
pIy perform a search in the heIp
system with the foIIowing search
criteria. £dit and Continue [VisuaI
Basic], and then Iook for the two
unsupported sections that expIain
what can't be done.
ABG 8
C07622132.indd 116 10/18/05 7:31:37 PM
Chapter 7. lixing the Broken BIocks ll7
TO ST£P OUT Ol TH£ COD£
$
|r ¦|e ||b·a·yvb ¦||e. c||c| ¦|e ·ed do¦ |r ¦|e ¦|·s¦ b·ea|po|r¦ o¦ ¦|e D|v|de ne¦|od |y c||c||rg ¦|e ·ed
do¦. yoJ ac¦Ja||y ·enove ¦|e b·ea|po|r¦ T|e b·ea|po|r¦ s|oJ|d ro. be gore
% To d|sab|e ¦|e secord b·ea|po|r¦. yoJ car Jse ¦|·ee d|¦¦e·er¦ ne¦|ods
Q T|e ¦|·s¦ ore |s s|np|y ¦o ·|g|¦c||c| ¦|e ||re o¦ code ¦|a¦ |as ¦|e b·ea|po|r¦. ¦|er se|ec¦ ¦|e
ßreakpoìnt. . . nerJ c|o|ce. ard ¦|ra||y se|ec¦ ¦|e DeIete ßreakpoìnt c|o|ce |oo| a¦ ||gJ·e ¯¯ ¦o see
¦||s |r ac¦|or
Q T|e secord ne¦|od |s ¦o se|ec¦ ¦|e Debug nerJ ard ¦|er se|ec¦ 1oggIe ßreakpoìnt o· p·ess f9
Q T|e ¦||·d ne¦|od |s ¦o ·|g|¦c||c| ¦|e ·ed do¦ |rd|ca¦|rg ¦|e b·ea|po|r¦ ard se|ec¦ ¦|e or|y ava||ab|e
c|o|ce DeIete ßreakpoìnt
;^\jgZ,",
7X_XgXTUeXT^cb\agYeb`TVbagXkghT_
`Xah\ag[XVbWXXW\gbe
&
`oJ s|oJ|d be a¦ ¦|e ¦|·s¦ ||re o¦ code |r ¦|e D|v|de ne¦|od No. ¦|a¦ yoJ'·e |r ¦|e D|v|de ne¦|od.
yoJ car p·ess CtrI+Shìft+f8 ¦o s¦ep oJ¦ o¦ ¦|e D|v|de ne¦|od o· yoJ car p·ess ¦|e S¦ep CJ¦ bJ¦¦or
T||s .||| e.ecJ¦e a|| ¦|e |rs¦·Jc¦|ors |r ¦|a¦ ne¦|od ard go bac| ¦o ¦|e ca||e·
' |·ess f5 ¦o e.ecJ¦e a|| ne¦|ods Jp ¦o ¦|e re.¦ b·ea|po|r¦
`oJ s|oJ|d see aro¦|e· nessage bo. .|¦| ¦|e ·esJ|¦ & C||c| OK ard ¦|er yoJ s|oJ|d be s¦opped |r ¦|e
soJ·ce code o¦ ¦|e |ead|||e ne¦|od
(
T|e |eadTo|rd ne¦|od ·eads ¦|e cor¦er¦ o¦ ¦|e opered ¦||e ard pJ¦s |¦ |r a s¦·|rg va·|ab|e |·ess
CtrI+Shìft+f8 ard ¦|er f8 / nessage bo. s|oJ|d d|sp|ay ¦|e s¦·|rg va·|ab|e cor¦er¦ C||c| OK |r ¦|e
nessage bo. `oJ s|oJ|d ro. be bac| a¦ ¦|e ca||e·
The Using bIock guarantees that
you're going to dispose of the
resources you're using when you
exit the bIock deIimited by the
£nd Using. You can read more
about this by doing a search in the
HeIp system in the Look lor text
box using the Using statement as
keywords.
@BE8 < A9 B
C07622132.indd 117 10/18/05 7:31:37 PM
ll8 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
)
S¦ep |r¦o ¦|e code Jr¦|| yoJ ge¦ ¦|e s¦·|rg |e||o.o·|d |r a nessage bo. |ay a¦¦er¦|or ¦o ¦|e o·de· o¦
e.ecJ¦|or ard |oo| |r¦o ¦|e va·|ab|es ard cor¦er¦ |r eac| o¦ ¦|e ¦ab sec¦|ors
* S¦ep |r¦o ¦|e code aga|r ¦o ge¦ |r¦o ¦|e \ar|pJ|a¦eS¦·|rgs ne¦|od
As you can see in the source code, one of the ManipuIateStrings arguments,
myString, is passed with the Bykefkeyword. when you have an argument
that is passed to a method by reference, the caIIed method is receiving a
reference to the same memory Iocation used by the caIIer. Therefore, if the
method is modifying the content of that argument, it is modifying the con-
tent at this memory Iocation and thus modifying the variabIe from the caIIer.
ln this case, anything that is done to the myString argument wiII modify the
vaIue of the variabIe in the caIIing code. The other argument is myPosition,
and it is passed with the ByVaI keyword. when you have an argument that is
passed by vaIue, the method is receiving a copy of the variabIe from the caII-
ing code and thus can't modify the originaI vaIue from the caIIer. Therefore,
the content wiII get Iost when the method ends and execution fIow returns
to the caIIer.
@BE8 < A9 B
1he íirsL insLrucLion in Lhe ManipulaLeSLrings meLhod is Laking Lhe sLring received in
argumenL and converLing iL Lo an array oí characLers. 1he reason Lhis is done is because
sLrings are immuLable in .NF1, and Lhereíore you have Lo work wiLh Lhem in read-only mode
once Lhey're creaLed. MeLhods modiíying a sLring are acLually reLurning a new sLring objecL
LhaL conLains Lhe modiíicaLion applied Lo iL.
1hereíore, ií you wanL Lo modiíy a sLring characLer by characLer or ií you wanL Lo access
one single characLer in a sLring by using an index, you íirsL need Lo converL Lhe sLring inLo
an array oí characLers.
TO B£GlN ST£PPlNG OUT Ol TH£ MANlPULAT£STklNGS M£THOD
$
|·ess CtrI+Shìft+f8 ¦o s¦ep oJ¦ o¦ ¦|e \ar|pJ|a¦eS¦·|rgs ne¦|od. o· yoJ car p·ess ¦|e Step Out
bJ¦¦or
1he applicaLion sLops abrupLly. WhaL jusL happened is an unhandled excepLion. An
unhandled excepLion happens whenever an error occurs and LhaL is noL anLicipaLed or han-
dled expliciLly by your applicaLion. ln LhaL case, Lhe execuLion oí your applicaLion is halLed
because Lhere is no way Lhe applicaLion can conLinue in LhaL sLaLe wiLhouL poLenLially cor-
rupLing Lhe memory or opening securiLy holes. One oí Lhe .NF1 runLime (CLR) principles is Lo
C07622132.indd 118 10/18/05 7:31:38 PM
Chapter 7. lixing the Broken BIocks ll9
make sure LhaL neiLher ever happens. 1hereíore, Lhe CLR crashes your applicaLion Lo prevenL
your applicaLion írom conLinuing Lo execuLe in an unknown sLaLe. Fven Lhough Lhe CLR is
Laking Lhose precauLions, iL is less probable Lo have insecure code execuLing in .NF1, buL
sLill possible.
1o help you íind Lhe bug LhaL raised LhaL unhandled excepLion, visual SLudio includes
anoLher useíul Lool. Lhe £xception Assistant. 1his assisLanL replaces Lhe previous FxcepLion
dialog box, and iL's helpíul because, based on Lhe conLexL oí Lhe excepLion, iL provides more
iníormaLion Lhan beíore, such as. Lhe Lype oí excepLion, LroubleshooLing Lips, and correcLive
acLions LhaL may be applied Lhrough Lhe
FxcepLion AssisLanL. Look aL Figure 7-8 Lo see
whaL iL looks like íor Lhe currenL excepLion.
When you look aL Lhe excepLion name, Lhe
LroubleshooLing Lips, and Lhe daLa visualizers, iL
should be apparenL why an unhandled excep-
Lion was raised. 1he excepLion name alone is
selí-explanaLory. lndexOuLOíRangeFxcepLion.
1he íirsL LroubleshooLing Lip displayed asks you
Lo make sure Lhe maximum index on a lisL is less
Lhan Lhe lisL size. Arrays in .NF1 are 0 based, Lhis
means LhaL Lhe íirsL elemenL sLarLs aL index 0.
1he lengLh oí Lhe sLring received as argumenL is
í0, as shown in Figure 7-8.
1he inLenL oí Lhis meLhod was Lo modiíy Lhe lasL characLer oí Lhe sLring when Lhe posi-
Lion in Lhe array is equal Lo a posiLion passed by value Lo Lhe meLhod. ln Lhis parLicular case,
Lhe posiLion passed by value Lo Lhe meLhod is í.
1hereíore, in Lhe "íor loop" aL Lhe second characLer oí LhaL sLring, Lhe "ií" sLaLemenL will
reLurn Lrue and Lhen Lhe index ^ will geL Lhe value oí Lhe
sLring lengLh. 1his means LhaL ^ is now equal Lo í0. When
Lhe applicaLion Lries Lo modiíy Lhe characLer aL index í0,
an excepLion will be generaLed because index í0 is ouLside
oí Lhe range oí Lhe array. 1he array has í0 characLers wiLh
index írom 0 Lo 9. Figure 7-9 uses a new visualizer Lo look
aL Lhe char array conLenL.
;^\jgZ,"-
8kVXcg\ba4ff\fgTag
;^\jgZ,".
4eeTli\fhT_\mXe
C07622132.indd 119 10/18/05 7:31:39 PM
l20 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
When you move Lhe mouse over program elemenLs, you'll someLimes see a magniíier. lí
you click Lhe drop-down lisL, you will see a lisL oí visualizers LhaL display Lhe iníormaLion in a
way LhaL is meaningíul Lo Lhe daLa Lype you're looking aL. For insLance, ií you're working
wiLh FxLensible Markup Language (XML) or HyperLexL Markup Language (H1ML) conLenL,
Lhe XML or H1ML visualizer will allow you Lo see Lhe conLenL as ií you were using MicrosoíL
lnLerneL Fxplorer or any oLher XML/H1ML Lool. ¥ou'll use one oí Lhe visualizers soon when
you debug Lhe ReadFile meLhod.
TO llX TH£ OUT Ol kANG£ PkOBL£M
$
\od|¦y ¦|e \ar|pJ|a¦eS¦·|rgs ne¦|od SJb¦·ac¦ 1 ¦·on ¦|e s¦·|rg |erg¦| .|er yoJ ass|gr a re. va|Je
¦o | T|e ||re o¦ code s|oJ|d |oo| |||e ¦||s a¦¦e· yoJ nod|¦y |¦
1 = my5f¡1ng.Lengfh - 1
%
/¦¦e· nod|¦y|rg ¦||s ||re o¦ code. nove yoJ· re.¦ e.ecJ¦|or po|r¦e· ¦o ¦|e '|o·' s¦a¦ener¦ so ¦|a¦
|rde. | s¦a·¦s a¦ J S¦ep ¦|·oJg| ¦|e code o· s¦ep oJ¦ T||s ¦|ne ¦|e·e s|oJ|d be ro e.cep¦|or
Cor¦|rJe ¦o s¦ep ¦|·oJg| ¦|e code. ard yoJ s|oJ|d ro. see aro¦|e· nessage |e||o.o·|¯ T|e s¦·|rg |as
beer nod|¦|ed becaJse |¦ .as passed by ·e¦e·erce
ConLinue Lo sLep Lhrough Lhe code and soon you'll geL a second excepLion, which is a
Division by 0 error. Oí course, an excepLion is raised because Lhe Divide meLhod assigns 0 Lo
Lhe denominaLor when Lhe numeraLor is greaLer Lhan 5. Using a visualizer, you can see LhaL
Lhe numeraLor is 6, Lhereíore 0 will be assigned Lo Lhe denominaLor.
Again, Lhe íirsL displayed LroubleshooLing Lip helps by suggesLing LhaL you make sure Lhe
denominaLor is noL 0. 1o solve Lhe problem, you could add an "lí" sLaLemenL, buL beíore you
do LhaL, consider anoLher .NF1 principle.
A good pracLice in .NF1 is Lo use Lhe excepLion mechanism Lo caLch Lhose corner cases
insLead oí coding special condiLional insLrucLions which bloaL Lhe code. 1he excepLions are
an inLegral parL oí Lhe .NF1 íramework and Lhey're everywhere. LeL's see Lhe logic behind
Lhis decision.
lf at any time you use the £dit and
Continue feature and you see that
your data is odd-Iooking or seems
corrupted, stop the debugging
process and restart it.
6 4HG < BA
C07622132.indd 120 10/18/05 7:31:39 PM
Chapter 7. lixing the Broken BIocks l2l
ln a real applicaLion, your applicaLion would noL purposely assign 0 Lo Lhe denominaLor,
Lhereíore, mosL divisions would resulL in a correcL operaLion. Adding an "lí" sLaLemenL would
resulL in a condiLional insLrucLion execuLed íor every single division. And because mosL divi-
sions would be valid, you would auLomaLically slow down your applicaLion. Using an excep-
Lion-handling mechanism Lo caLch Lhose corner cases is a much beLLer soluLion because Lhe
excepLion-handling code will geL execuLed only when necessary, so your applicaLion should
be íasLer.
When you inserL excepLion-handling code in your applicaLion, iL is besL pracLice Lo
always caLch excepLions írom Lhe mosL precise Lo Lhe leasL precise. ln Lhis case, you know
LhaL Lhe Division8yZeroFxcepLion is Lhe one mosL likely Lo occur, Lhereíore, iL's Lhe íirsL one
you wanL Lo caLch.
When you caLch an excepLion, Lhe excepLion is "handled." ¥ou Lhen need Lo do some-
Lhing abouL iL, eiLher you handle iL by menLioning iL Lo Lhe user or you Lhrow Lhe excepLion
back. ln Lhis case, you wanL Lhe user Lo know LhaL an excepLion was raised, buL you don'L
wanL Lhe program Lo crash. Here's an example LhaL demonsLraLes Lhis íorm oí handling LhaL
l'm sure you already know. lí you Lry Lo divide by zero in MicrosoíL Oííice Fxcel®, Fxcel
won'L crash, iL will simply indicaLe LhaL your enLry resulLs in a division by zero and display Lhe
#Dlv/0! message in Lhe cell.
An older way oí doing Lhings was Lo make your meLhod reLurn an inLeger Lo indicaLe
success or íailure. And LhaL's where people meL wiLh Lrouble because beLween Lwo applica-
Lions, and someLimes beLween Lwo íuncLions, Lhe same inLeger code meanL Lwo diííerenL
Lhings. ¥ou received an inLeger LhaL was supposed Lo Lell you why your applicaLion íailed,
buL Lhe originaLing code had Lwo meanings, and iL was a nighLmare Lo íigure ouL which one
was Lhe valid error code. ln addiLion, when people used error codes, Lheir code was ugly
because Lhey eiLher had a swiLch case or a series oí nesLed iís.
ln .NF1, you should never design your meLhods Lo reLurn an inLeger Lo indicaLe success
or íailure, nor should you use a 8oolean íor Lhe same purpose. 1his is a bad pracLice LhaL
was used when excepLions did noL exisL or when people didn'L know or wanL Lo use Lhem
appropriaLely. Ndjh]djaYcZkZgYdi]^h# lnsLead, use excepLions.
C07622132.indd 121 10/18/05 7:31:39 PM
l22 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
TO ADD COD£ TO HANDL£ Dlll£k£NT £XC£PTlONS
$
C||c| ¦|e Stop Debuggìng bJ¦¦or o· p·ess CtrI+AIt+ßreak ¦o s¦op debJgg|rg node |r
Tes¦/pp||ca¦|orvb. nod|¦y ¦|e btn_CIìc| ne¦|od ¦o |oo| |||e ¦|e ¦o||o.|rg
D1m myOb1ecfL1b¡a¡y as NeW L1b¡a¡y1}
D1m my5f¡1ng As 5f¡1ng = "he]]oWo¡]d"
D1m myI1]e As 5f¡1ng = ""
1¡y
Message8ox.5hoW1myOb1ecfL1b¡a¡y.D1v1de1S. 3}.1o5f¡1ng1}}
Message8ox.5hoW1myOb1ecfL1b¡a¡y.D1v1de13. 3}.1o5f¡1ng1}}
Message8ox.5hoW1myOb1ecfL1b¡a¡y.D1v1de16. 4}.1o5f¡1ng1}}
Cafch ex As D1v1de8yZe¡oLxcepf1on
Message8ox.5hoW1ex.1o5f¡1ng1}}
Lnd 1¡y
1¡y
myI1]e = ¨MyLx1sf1ng1exfI1]e.fxf¨
Message8ox.5hoW1myOb1ecfL1b¡a¡y.ReadI1]e1myI1]e}}
myI1]e = ¨MyNofLx1sf1ng1exfI1]e.fxf¨
Message8ox.5hoW1myOb1ecfL1b¡a¡y.ReadI1]e1myI1]e}}
Cafch ex As I1]eNofIoundLxcepf1on
Message8ox.5hoW1myI1]e + ¨ doesn`f ex1sf!¨}
Lnd 1¡y
myOb1ecfL1b¡a¡y.Man1pu]afe5f¡1ngs1my5f¡1ng. 20}
Message8ox.5hoW1my5f¡1ng}
myOb1ecfL1b¡a¡y.Man1pu]afe5f¡1ngs1my5f¡1ng. 1}
Message8ox.5hoW1my5f¡1ng}
%
|enove a|| ¦|e b·ea|po|r¦s |r Tes¦/pp||ca¦|orvb ard ||b·a·yvb ard e.ecJ¦e ¦|e code |oo| a¦ ¦|e d|¦
¦e·er¦ nessage bo.es |¦ a D|v|de|y¯e·o|.cep¦|or o· |||eNo¦|oJrd|.cep¦|or occJ·s. a nessage bo.
.||| be d|sp|ayed
C07622132.indd 122 10/18/05 7:31:40 PM
Chapter 7. lixing the Broken BIocks l23
1here are some useíul Labs LhaL you didn'L use in Lhis debugging session. For insLance,
Lhe WaLch Lab is imporLanL because you can enLer variables and expressions LhaL you wanL
Lo íollow and moniLor during Lhe execuLion oí Lhe applicaLion.
AnoLher useíul Lab is Lhe lmmediaLe window, where you can Lype anyLhing and Lhe
compiler veriíies, compiles, and execuLes iL on Lhe íly! Any eííecL on Lhe applicaLion under
debug is immediaLe. Any piece oí code LhaL can be evaluaLed by Lhe compiler and does noL
require a block oí code can be enLered inLo Lhe lmmediaLe window. ¥ou could enLer a loop,
íor insLance. ¥ou also have íull access Lo lnLelliSense in Lhis window jusL as ií you were in Lhe
code ediLor. LeL's look aL a simple example.
TO US£ TH£ lMM£DlAT£ wlNDOw
$

|J¦ a b·ea|po|r¦ a¦ ¦|e ¦|·s¦ |rs¦·Jc¦|or |r yoJ· app||ca¦|or ard ·Jr ¦|e app||ca¦|or by p·ess|rg f5
%
|¦ yoJ dor'¦ see ¦|e |nned|a¦e .|rdo.. |Js¦ se|ec¦ ¦|e Debug nerJ ard ¦|er se|ec¦ Wìndows ard
Immedìate `oJ s|oJ|d |ave ar enp¦y |nned|a¦e .|rdo. |r ¦|e bo¦¦on o¦ yoJ· sc·eer
& Type ¦|e ¦o||o.|rg ||re ard p·ess |r¦e· ì = 5
' No. ¦ype ¦||s ||re ard p·ess |r¦e· ìf ì > 2 then Messageßox.Show(ì)
T|e nessage bo. ¦|a¦ appea·s s|oJ|d s|o. a ' `oJ car ¦es¦ code |r ·ea| ¦|ne dJ·|rg ¦|e e.ecJ¦|or .|¦|
oJ¦ e.ecJ¦|rg a s|rg|e ||re o¦ code ¦·on yoJ· app||ca¦|or |J¦ be.a·e |¦ yoJ Jse va·|ab|es ¦|a¦ a·e |r yoJ·
app||ca¦|or. ·enenbe· ¦|a¦ |¦ yoJ nod|¦y ¦|en |r ¦|e |nned|a¦e .|rdo.. yoJ nod|¦y ¦|en ¦o· ¦|e app||ca
¦|or as .e||
( C||c| ¦|e Stop Debuggìng bJ¦¦or ¦o s¦op e.ecJ¦|rg ¦|e app||ca¦|or
¥ou're now noL only able Lo build new applicaLions, buL you're aware oí Lhe Lechniques
and Lools available Lo debug Lhem.
C07622132.indd 123 10/18/05 7:31:40 PM
l24 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
ln Lhis chapLer, you learned how Lo use breakpoinLs, abouL diííerenL Lechniques Lo sLep inLo,
sLep over, and sLep ouL oí Lhe source code, and abouL daLa visualizers Lo see Lhe daLa in Lhe
mosL perLinenL way based on iLs conLenL or conLexL. ¥ou also learned how Lo work wiLh a DLL.
¥ou discovered LhaL you can use Lhe FdiL and ConLinue íeaLure Lo modiíy variables aL
run-Lime and conLinue Lhe execuLion.
¥ou learned how you can move Lhe nexL insLrucLion poinLer Lo re-execuLe some
lines oí code. ¥ou also sLarLed Lo deal wiLh excepLions and learned Lhe dos and don'Ls oí
debugging. ¥ou've seen how subLle bugs can íind Lheir way in-usually due Lo disLracLions
and someLimes simply because you don'L possess all oí Lhe knowledge and experience-buL
LhaL's okay. Don'L worry, you're in a process called learning.
ln Lhe nexL chapLer, you'll learn abouL daLabases, ADO.NF1, and manipulaLing daLa Lo
and írom a MicrosoíL SOL Server" Fxpress daLabase. ¥ou'll learn how Lo use Lhis daLa Lo
populaLe conLrols on a Windows íorm. ¥ou'll also learn Lo creaLe an applicaLion Lo add,
modiíy, deleLe, and visualize rows in Lhe car Lracker applicaLion.
ln Summary...
C07622132.indd 124 10/18/05 7:31:40 PM
l25
J[Tg<fT7TgTUTfX2$%)
FD?FXeiXe%##(8kceXff
\aI\fhT_5Tf\V%##(
8kceXff8W\g\ba$&'
J[Tg4eX47B!A8GTaW
7TgTU\aW\aZ2$')
6[TcgXe+
@TaTZ\aZ
g[X7TgT
-
So íar, you've seen how Lo build a Windows Forms applicaLion and whaL
characLerisLics Lhose applicaLions conLained, buL you have noL managed a
greaL deal oí daLa. Managing daLa is always a concern, wheLher aL home, aL
Lhe oííice, aL school, or even íor recreaLion. For insLance, l have many recipes
and ideas íor greaL dinners, buL when l wanL Lo prepare a nice meal, iL Lakes
me so much Lime Lo íind Lhem LhaL usually l change my mind. lí l had Lhis
iníormaLion in my compuLer, iL would be easy Lo quickly access my recipe íor
"rack oí lamb wiLh herb crusL" and prepare a íabulous meal. l could also add
oLher perLinenL iníormaLion Lo Lhe recipe íile, such as whaL side dishes were
served wiLh iL or whaL wines wenL well wiLh Lhis recipe. l could even add a
picLure oí Lhe íinished meal.
¥ou could manage daLa using a word processing program, such as
MicrosoíL Word, buL iL could become unmanageable as soon as you collecL
a loL oí recipes and need Lo search íor iníormaLion wiLhin LhaL íile. Using
a spreadsheeL, such as MicrosoíL Fxcel, is also problemaLic. 1he íacL is LhaL
Lrying Lo íind iníormaLion quickly when using more Lhan one variable is
close Lo impossible. Using Lhe recipe example, suppose you wanL Lo reLrieve
all oí Lhe recipes LhaL can serve aL leasL six people and LhaL have lamb sLew
meaL buL no minL in Lhe ingredienLs because one oí your guesLs is allergic
Lo minL. lmagine Lhe Lime iL would Lake Lo íind LhaL iníormaLion in eiLher
a Word íile or an Fxcel spreadsheeL. 1haL's where daLabases come Lo Lhe
rescue.
C08622132.indd 125 10/18/05 8:47:56 PM
l26 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
ln Lhis chapLer, you'll learn whaL a daLabase is, how Lo creaLe a daLabase, how Lo add,
deleLe, and updaLe daLa, how Lo search or query a daLabase, and how Lo use a daLabase in
a Windows Forms applicaLion. Accompanying visual 8asic 2005 Fxpress FdiLion is SOL Server
2005 Fxpress FdiLion, which is a íully workable version oí iLs bigger broLher, SOL Server 2005,
buL wiLh íewer íeaLures. SOL Server 2005 Fxpress FdiLion is íree, easy Lo use, and geared
Loward building simple and dynamic applicaLions.
what ls a Database?
A daLabase is a collecLion oí daLa LhaL is sLored in íiles on disks using a sysLemaLic sLrucLure.
1he sysLemaLic sLrucLure enables users Lo query Lhe daLa using managemenL soíLware
called DaLabase ManagemenL SysLem (D8MS). SOL Server 2005 is a RelaLional DaLabase
ManagemenL SysLem (RD8MS). lL is based on a relaLional model because iLs daLa is sLruc-
Lured using seLs (Lhe seLs Lheory in maLhemaLics) and logical relaLions (predicaLes). MosL
commercial daLabase producLs are based on Lhe relaLional model. ln íacL, iL's been one oí Lhe
mosL popular models íor Lhe lasL 20 years. AparL írom MicrosoíL SOL Server, you may have
also heard oí Lhe íollowing producL names. Oracle or l8M D82.
what's ln a Database?
A relaLional daLabase, such as SOL Server 2005, conLains mulLiple Lables LhaL are relaLed
LogeLher. A daLabase can also conLain views, sLored procedures, íuncLions, indexes, securiLy
iníormaLion, and oLher elemenLs. ln Lhis secLion, you'll learn abouL Lhe basic elemenL oí a
relaLional daLabase, which is a Lable and iLs componenLs.
A Lable conLains columns and rows. A column deíines Lhe Lype oí daLa, and a row
conLains Lhe acLual daLa. 8ecause Lhe relaLional model has sLricL rules, a RD8MS LhaL uses
Lhe relaLion model musL implemenL Lhem.
You'II Iearn about some of the
other eIements contained in a
reIationaI database Iater in this
chapter.
ABG 8
ln reaIity, no popuIar kDBMS is fuIIy impIementing the pure reIationaI modeI
as it was first created in the l970s.
@BE8 < A9 B
C08622132.indd 126 10/18/05 8:47:56 PM
Chapter 8. Managing the Data l27
Data NormaIization and Data lntegrity
1he rules deíining Lhe relaLional model are called normalizaLion rules. NormalizaLion is a
process LhaL daLa archiLecLs musL apply whenever Lhey are aL Lhe design phase.
NormalizaLion rules exisL Lo reduce Lhe chance oí having Lhe same daLa sLored in more Lhan
one Lable, in oLher words, Lhey exisL Lo reduce Lhe level oí redundancy and also Lo preserve
daLa inLegriLy in Lhe daLabase. Logically, Lhe normalizaLion process exisLs Lo help spliL Lhe
daLa inLo iLs own Lable so LhaL Lhere is no duplicaLion oí iníormaLion in more Lhan one Lable.
For example, having an applicaLion in which Lhe cusLomer's address, ciLy, sLaLe or province,
zip or posLal code, and counLry are duplicaLed in Lwo diííerenL Lables is a bad idea. 1here
should be only one link írom Lhe cusLomer Lable Lo Lhe oLher Lable reíerencing addiLional
cusLomer iníormaLion. Having duplicaLe daLa would make updaLes and deleLions more prob-
lemaLic and would also pose Lhe risk oí having modiíied daLa in one Lable and noL Lhe oLher.
1his example demonsLraLes a daLa inLegriLy problem.
LeL's look aL anoLher daLa inLegriLy problem. Suppose you have boLh a producL Lable and
a Lable conLaining cusLomer order deLails. AlLhough you normalized your daLa, daLa inLegriLy
does noL exisL (íor Lhis example). Now leL's say you decide Lo deleLe producLí, which means
removing a row írom Lhe producL Lable LhaL corresponds Lo producLí. lí Lhe RD8MS would
leL you do Lhis, iL would mean LhaL suddenly all rows in Lhe cusLomer order deLails Lable LhaL
conLained Lhis producL would noL be able Lo show which producL was ordered because Lhe
producL would no longer exisL. 1hose rows would be orphaned, which could have disasLrous
resulLs íor Lhe company.
As you can see, daLa inLegriLy is a very imporLanL concepL LhaL is relaLed Lo Lhe accuracy,
validiLy, and correcLness oí Lhe daLa. 1o beLLer undersLand some oí Lhese concepLs, leL's look
aL anoLher example.
Suppose you are Lhe owner oí an online sLore and wanL Lo manage your company using
a soíLware applicaLion. 1o use a soíLware applicaLion, you musL sLarL Lhinking abouL using a
daLabase. Any company, boLh small or large, Lypically has a greaL deal oí daLa Lo sLore. Also,
because daLa is all around us, people wanL more access Lo Lhis daLa so as Lo creaLe reporLs
and conducL analysis. 1haL is why daLabases are so useíul. ReLurning Lo your online sLore, aL
a minimum you would like Lo sLore iníormaLion abouL your cusLomers, producLs, invoices,
purchasing, and invenLory. 1o summarize all oí Lhose areas, leL's Lake a look aL Lhe ProducL,
OrderHeader, and OrderDeLail Lables.
The foIIowing tabIes have pur-
poseIy been kept simpIe {some
coIumns are missing) and are used
to iIIustrate the concepts you've
just Iearned.
ABG 8
C08622132.indd 127 10/18/05 8:47:57 PM
l28 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
CoIumn Name Data Type AIIow NuIIs?
ProducLlD (PK) inLeger NoL Null
ProducLNumber nvarchar(í0) NoL Null
Name nvarchar(50) NoL Null
DescripLion nvarchar(200) Null
PhoLo image Null
Price money NoL Null
1axable biL NoL Null
CoIumn Name Data Type AIIow NuIIs?
OrderlD (PK) inLeger NoL Null
OrderDaLe daLeLime NoL Null
DueDaLe daLeLime NoL Null
CusLomerlD (FK) inLeger NoL Null
1axAmounL money NoL Null
1oLal money NoL Null
IVWaZ-"&
CebWhVgGTU_X
IVWaZ-"'
BeWXe;XTWXeGTU_X
C08622132.indd 128 10/18/05 8:47:57 PM
Chapter 8. Managing the Data l29
CoIumn Name Data Type AIIow NuIIs?
OrderlD (PK) (FK) inLeger NoL Null
LineDeLaillD (PK) inLeger NoL Null
ProducLlD (FK) inLeger NoL Null
OuanLiLy inLeger NoL Null
Line1oLal numeric(88,6) NoL Null
¥our ProducL, OrderHeader, and OrderDeLail Lables could also be represenLed graphically,
as shown in Figure 8-í. 1his is a common way oí looking aL daLabases.
;^\jgZ-"&
CTeg\T_WTgTUTfXW\TZeT`bYTf`T__ba_\aXVb`cTal
IVWaZ-"(
BeWXe7XgT\_GTU_X
C08622132.indd 129 10/18/05 8:47:58 PM
l30 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
what ls NuII?
One oí your íirsL observaLions is LhaL Lhere's a column in Lhe Lables LiLled Allow Nulls¯, which
is also reílecLed in Figure 8-í. When designing a Lable, you need Lo consider whaL's abso-
luLely necessary (NoL Null) and whaL's noL (Null). For insLance, when you inserL a new row
inLo Lhe ProducL Lable, iL may noL maLLer wheLher Lhe producL has a phoLo, buL iL mighL be a
problem Lo have a producL wiLhouL a producL number. Now leL's correlaLe how allowing null
is relaLed Lo daLa inLegriLy. Whenever a Lable is designed wiLh a column LhaL doesn'L allow
null, Lhe RD8MS will rejecL any inserLion oí a new row LhaL has a column seL Lo null when iL
is noL supposed Lo. When you pay aLLenLion Lo Lhose columns LhaL cannoL conLain null when
designing your Lables, you auLomaLically add anoLher daLa inLegriLy layer by making sure
LhaL all necessary daLa is presenL beíore Lhe record is inserLed inLo Lhe daLabase.
what Are Primary Keys and loreign Keys?
¥ou'll see in Figure 8-í LhaL some columns have Lhe leLLers (PK) íor primary key in Lhe Lables
or a yellow keyíor primary keys. Some columns also conLain (FK) íor íoreign key. LeL's sLarL
by Lalking abouL Lhe primary key.
Prìmary Key
A primary key is a value LhaL is used Lo uniquely idenLiíy a speciíic row in a Lable. A
primary key.
N Can be composed oí one or more column names. When iL's composed oí more Lhan one
column, iL's called a composiLe key.
N ls oíLen a numeric íield.
N ls oíLen generaLed by Lhe RD8MS, in which case iL's called a surrogaLe key. A surrogaLe
key is írequenLly (buL noL always) a sequenLial number. A surrogaLe key is also called an
idenLiLy in SOL Server 2005. An idenLiLy sLarLs aL a seL number, called Lhe idenLiLy seed,
and incremenLs by anoLher seL number, called Lhe idenLiLy incremenL. For example, ií you
creaLe a Lable named ProducL, you can have a column named ProducLlD LhaL is seL as an
idenLiLy, and you can seL Lhe idenLiLy seed Lo í wiLh an idenLiLy incremenL Lo í. When Lhe
íirsL row is creaLed in Lhe ProducL Lable, Lhe ProducLlD will be generaLed by Lhe RD8MS
and seL Lo í. 1he íollowing row will have a ProducLlD LhaL is seL Lo 2 and so íorLh.
C08622132.indd 130 10/18/05 8:47:58 PM
Chapter 8. Managing the Data l3l
N Should be as shorL as possible, buL long enough Lo supporL Lhe number oí rows iL will
represenL.
N ls immuLable, meaning iLs value should never change.
N ls also a naLural key when Lhe key has a logical relaLionship wiLh Lhe resL oí Lhe columns
in Lhe Lable. For example, ií you had a book Lable, Lhe lS8N number could be used as a
primary key because iL uniquely idenLiíies only one book. lL would be an advanLage
compared wiLh a generaLed key because iL would Lake less space and has Lo exisL any-
way!
N ls also used Lo relaLe Lwo Lables LogeLher.
ln our ProducL Lable example, Lhe ProducLlD is Lhe primary key. AL design Lime, iL will
also be an idenLiLy. ¥ou can claim LhaL Lhe producL number could be a primary key and you
could be righL, buL in cerLain scenarios a producL number could be used Lwice. For example,
suppose you have producL #FC-00í wiLh a revision í.0. ln Lime, you change Lhe producL
because oí cusLomer complainLs and give iL a revision 2.0. ¥ou wanL your cusLomers Lo
conLinue Lo order Lhe same producL number íor many business reasons. ln your daLabase,
you would reLire Lhe producL revision í.0 by perhaps changing a column named AcLive, Lhen
add anoLher row in your Lable wiLh Lhe new producL deLails, including revision 2.0, and seL
iL Lo AcLive. Why can'L you use Lhe same row¯ LeL's assume LhaL six monLhs aíLer creaLing
Lhe new producL revision, you wanL Lo creaLe a graph Lo deLermine wheLher your changes Lo
Lhe producL meanL LhaL you had íewer reLurns írom your cusLomers. lL would be diííiculL Lo
come up wiLh good daLa ií you had only one row íor Lhe producL, buL iL would be íairly easy
Lo do ií you have Lwo rows because Lhey would be unique in Lhe daLabase, wiLh each one
having a diííerenL ProducLlD.
ln Lhe OrderDeLail Lable, you have a composiLe primary key LhaL is a combinaLion oí Lhe
OrderlD and OrderDeLaillD. 1his means LhaL Lhese Lwo columns would ensure Lhe uniqueness
oí a row in Lhe OrderDeLail Lable.
ln Lhe OrderHeader Lable, Lhe OrderlD is Lhe primary key.
C08622132.indd 131 10/18/05 8:47:58 PM
l32 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
foreìgn Key
A íoreign key is a column in a Lable LhaL relaLes Lo a column in anoLher Lable. lL also
enables you Lo creaLe relaLions beLween Lables. A íoreign key in a Lable is always a primary
key in anoLher Lable. Foreign keys are used Lo eníorce daLa inLegriLy by being parL oí íoreign
key consLrainLs. Foreign key consLrainLs are creaLed Lo make sure reíerenLial inLegriLy is
preserved and noL violaLed. 1here are Lwo íoreign keys in Lhe order deLails. 1he íirsL is Lhe
ProducLlD íoreign key in Lhe OrderDeLail Lable, and iL's relaLed Lo Lhe primary key named
ProducLlD in Lhe ProducL Lable. 1he second is Lhe OrderlD íoreign key in Lhe OrderDeLail
Lable, and iL's relaLed Lo Lhe primary key named OrderlD in Lhe OrderHeader Lable.
Concerning Lhe naming oí íoreign keys, iL's a good pracLice Lo deíine Lhem using Lhe same
name as Lheir primary key counLerparL, oLherwise, iL may lead Lo problems íor Lhose looking
aL your logical daLa model.
l inLroduced you Lo daLa inLegriLy aL Lhe beginning oí Lhis chapLer. ln doing so, l ciLed an
example LhaL could creaLe similar problems Lo Lhe one in Lhe ProducL and OrderDeLail Lable
example. Adding a íoreign key consLrainL beLween Lhese Lwo Lables would prevenL a user
írom deleLing a producL in Lhe ProducL Lable LhaL could poLenLially creaLe a large number oí
orphaned rows in Lhe OrderDeLail Lable. lí you look aL Figure 8-í, Lhe íoreign key consLrainL
beLween ProducL and OrderDeLail is shown as a line beLween Lhe Lwo Lables LhaL can be
íound by looking aL Lhe name FK_ProducL_OrderDeLail. Naming consLrainLs is an easy way
Lo undersLand whaL Lhey are íor. We only have Lhree Lables in our example, buL you can
imagine LhaL consLrainLs wiLhouL names LhaL exisL beLween numerous Lables would quickly
become unclear.
AnoLher íoreign key consLrainL exisLs here, which is Lhe one beLween Lhe OrderHeader
and OrderDeLail Lables LhaL would prevenL an order írom being deleLed beíore all oí iLs
maLching OrderDeLails have been deleLed. ¥ou can see in Figure 8-í LhaL Lhe OrderHeader
Lable has anoLher íoreign key called CusLomerlD. 1hereíore, anoLher íoreign key consLrainL
would exisL beLween Lhe CusLomer and OrderHeader Lables. Following Lhe same principles
íound wiLh oLher íoreign key consLrainLs, Lhis would prevenL a cusLomer írom Lhe CusLomer
Lable írom being deleLed beíore all oí iLs maLching orders in Lhe OrderHeader Lable and all
deLail rows in Lhe OrderDeLails Lable LhaL maLch Lhe orders have been deleLed.
lí Lhere were no íoreign key consLrainLs in Lhis daLabase, daLa inLegriLy would be easily
violaLed. 1he daLabase would be leíL wiLh a big problem. a Lime bomb oí orphaned rows
C08622132.indd 132 10/18/05 8:47:59 PM
Chapter 8. Managing the Data l33
LhaL Lake up space and slow down all queries. 8y adding Lhis íoreign key consLrainL, Lhe
RD8MS would ensure LhaL all rows in Lhe OrderDeLail Lable LhaL reíerence Lhis producL have
been deleLed beíore Lhe producL row could be deleLed in Lhe ProducL Lable.
How Do You lnteract with a keIationaI Database?
So íar, l've Lalked abouL Lables in which you can updaLe, add, or deleLe rows or query Lhe
daLabase Lo geL parLicular resulLs. Perhaps you've been asking yourselí. 8uL how do l Lalk or
inLeracL wiLh Lhe daLabase¯ How does iL reLurn Lhe answers Lo my queries¯ And how do you
creaLe Lhose Lables¯ l'm sure you've been asking yourselí many oLher quesLions as well. 1he
answer Lo all oí Lhese quesLions is SOL Server 2005 Fxpress FdiLion.
SOL sLands íor SLrucLured Ouery Language and was invenLed in Lhe í970s. 1he acronym
is pronounced SFOUFL and was also inLroduced using LhaL same spelling, buL because oí a
Lrademark dispuLe in Lhe UK in Lhe í970s, Lhe name was shorLened Lo Lhe now well-known
SOL acronym. 8ack Lhen, Lhe SFOUFL acronym meanL SLrucLured £nglish Ouery Language.
SOL is an Fnglish-based language and is very similar Lo human language quesLions. 1haL's
why iL's easy and íasL Lo learn basic SOL programming. LeL's look aL Lwo examples.
l. SFLFC1 * FROM CUS1OMFR
2. SFLFC1 COUN1(*) FROM PRODUC1
1he íirsL example can be LranslaLed in Fnglish Lo give me all (*) rows in Lhe CusLomer
Lable or give me Lhe lisL oí cusLomers in Fnglish. 1he second example can be LranslaLed as
a requesL Lo give me Lhe LoLal oí all rows conLained in Lhe ProducL Lable or Lo counL how
many producLs Lhis company has.
When you issue an SOL query Lo a relaLional daLabase, Lhe daLabase reLurns a resulL seL
LhaL simply conLains Lhe rows wiLh Lhe answers Lo your query. Using SOL, you can also group
or aggregaLe Lhe resulLs oí a query. ¥ou also use SOL Lo creaLe Lables or deleLe (drop) Lables.
¥ou've learned abouL primary keys, íoreign keys, and consLrainLs, buL you probably didn'L
know LhaL Lhey're also creaLed using SOL.
lL's also good Lo know LhaL SOL is an ANSl/lSO sLandard, Lhereíore, any RD8MS producer
needs Lo obey a seL oí rules. 8asic SOL is a base programming language and as such is
usually noL suííicienL Lo solve all possible problems or analysis needs LhaL an applicaLion may
demand. lL has a raLher limiLed seL oí keywords. 8ecause iLs íirsL goal is Lo query daLa írom a
C08622132.indd 133 10/18/05 8:47:59 PM
l34 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
daLabase, Lhe mosL popular RD8MSs on Lhe markeL have added exLensions Lo SOL Lo permiL
Lhe addiLion oí procedural code. 1hese addiLions Lurn SOL inLo a íull-íledged programming
language LhaL helps solve more complex problems. 1he íollowing is a lisL oí popular
exLensions and Lheir manuíacLurers. MicrosoíL 1ransacL-SOL (or 1-SOL íor shorL), Oracle PL/
SOL, and l8M SOL PL. RecenLly, in addiLion Lo Lhese exLensions, RD8MS manuíacLurers have
added Lhe supporL oí oLher programming languages. MicrosoíL is adding .NF1 language
supporL inLo Lhe daLabase wiLh all SOL Server 2005 FdiLions, while Oracle and l8M have
added Java supporL.
1here are more daLabase concepLs and Lheories Lhan Lhose lisLed and explained here,
buL we have covered Lhe immediaLe daLabase needs oí Lhis book. ¥ou'll now apply Lhose
concepLs concreLely in a Windows Forms applicaLion LhaL will use a SOL Server Fxpress 2005
daLabase.
SOL Server 2005 £xpress in VisuaI Basic 2005 £xpress £dition
ln Lhis secLion, you will develop a Windows Forms applicaLion. 1his will be a car Lracker
applicaLion LhaL will enable Lhe user Lo Lrack Lhe prices oí cars over Lime and deLermine
where Lhe lisLing was observed. ¥ou will íirsL use visual SLudio Lo creaLe Lhe daLabase and
Lhe Lables, Lhen add some daLa and validaLe some oí Lhe concepLs you've learned in Lhe íirsL
parL oí Lhis chapLer. ¥ou will Lhen creaLe a Windows applicaLion LhaL will use your daLa and
build a daLa-cenLric applicaLion LhaL will allow Lhe user Lo sLore any amounL oí daLa.
Reíer Lo Figure 8-2 íor Lhe daLabase diagram perLaining Lo Lhis secLion's example.
C08622132.indd 134 10/18/05 8:47:59 PM
Chapter 8. Managing the Data l35
Creating a Database Using VisuaI Basic 2005 £xpress £dition
8eíore using daLa, we need a place Lo sLore Lhe daLa. ¥ou'll learn how Lo creaLe a daLabase in
visual 8asic 2005 Fxpress FdiLion. ¥ou'll also see how easy iL is íor you Lo creaLe all oí Lhe
Lables we need Lo saLisíy Lhe needs oí our car Lracker applicaLion because Lhe SOL Server
Leam did a wonderíul job oí inLegraLing Lhe Lools inLo visual SLudio.
TO Ck£AT£ A DATABAS£ USlNG VlSUAL BASlC 2005 £XPk£SS £DlTlON
;^\jgZ-"'
6TegeTV^XeTcc_\VTg\baWTgTUTfXW\TZeT`
SOL Server is weII integrated
because VisuaI Studio provides a
great SDK for other components
to pIug into the lD£.
@BE8 < A9 B
$

S¦a·¦ VìsuaI ßasìc 2005 fxpress fdìtìon
%
C·ea¦e a re. \|rdo.s app||ca¦|or ard rane
|¦ Car1racker
&
`oJ .||| ro. c·ea¦e ¦|e da¦abase ¦|a¦ .||| |o|d
a|| ¦|e ¦ab|es ¦o· ¦|e app||ca¦|or |r ¦|e
So|J¦|or |.p|o·e·. ·|g|¦c||c| ¦|e Car1racker p·o|ec¦.
se|ec¦ Add. ard ¦|er se|ec¦ New Item
'
|r ¦|e /dd Ne. |¦en d|a|og bo.. se|ec¦ SOL
Database ¦·on ¦|e \|sJa| S¦Jd|o |rs¦a||ed
Tenp|a¦es Type |r ¦|e ¦||erane Car1racker.mdf
ard c||c| ¦|e Add bJ¦¦or |y do|rg so. yoJ'|| c·ea¦e
a da¦abase ard a¦¦ac| ¦|e da¦abase ¦||e (Car1racker.
mdf) ¦o yoJ· Ca·T·ac|e· p·o|ec¦
(
`oJ .||| ¦|er see a Da¦a SoJ·ce Cor¦|gJ·a¦|or
\|.a·d Dor'¦ pay a¦¦er¦|or ¦o ¦||s d|a|og bo.
|Js¦ ye¦. yoJ'|| |ea·r aboJ¦ |¦ soor ¹Js¦ c||c| CanceI
¦o· ro.
The .mdf fiIe extension is used
by the SOL Server famiIy of prod-
ucts. The .mdf contains the entire
database, this means aII tabIes and
other eIements that can exist in
the database are Iocated in that
one fiIe. The onIy thing that is
not part of the .mdf fiIe is the Iog
information, which is in a .Idf fiIe.
lt is created whenever you create
the database. The fiIe has an .Idf
extension and is used to store the
database Iog information. You
can see this fiIe by cIicking the
Show AII liIes icon in the SoIution
£xpIorer.
@BE8 < A9 B
C08622132.indd 135 10/18/05 8:48:00 PM
l36 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
)
T|e So|J¦|or |.p|o·e· s|oJ|d ro. cor¦a|r a re. |¦en .|¦||r yoJ· p·o|ec¦ ¦|e re. da¦abase ¦||e ca||ed
Car1racker.mdf. as s|o.r |r ||gJ·e 8`
*
`oJ .||| ro. s¦a·¦ ¦o add ¦ab|es ¦o yoJ· da¦abase To do ¦||s. yoJ car e|¦|e· doJb|ec||c| ¦|e
Car1racker.mdf ¦||e o· ·|g|¦c||c| Car1racker.mdf ard ¦|er se|ec¦ Open T||s .||| caJse \|sJa|
S¦Jd|o ¦o correc¦ ¦o ¦|e SO| Se·ve· ´JJ' |.p·ess |rs¦arce |rs¦a||ed or yoJ· nac||re
T|e Da¦abase |.p|o·e· s|oJ|d appea· or ¦|e |e¦¦ s|de o¦ ¦|e sc·eer .|e·e ¦|e ¦oo|bo. JsJa||y opers Jp. as
s|o.r |r ||gJ·e 84 |¦ yoJ do ro¦ see ¦|e Da¦abase |.p|o·e·. go ¦o ¦|e \|e. nerJ ard se|ec¦ Database
fxpIorer
|rde· ¦|e da¦abase rane. yoJ s|oJ|d see a ||s¦ o¦ da¦abase e|ener¦s ·ep·eser¦ed by ¦o|de· |cors
/|¦|oJg| yoJ .||| ro¦ ·ecogr|.e nos¦ o¦ ¦|en. yoJ .||| see ¦.o e|ener¦s ¦|a¦ a·e a|·eady ¦an|||a· ¦o yoJ
¦|e da¦abase d|ag·an ard ¦|e ¦ab|es `oJ .||| Jse bo¦| o¦ ¦|ese e|ener¦s s|o·¦|y
`oJ'|| |ro. ¦|a¦ yoJ'·e correc¦ed ¦o ¦|e da¦abase .|er yoJ see ¦|e da¦abase |cor .|¦| ar e|ec¦·|c co·d
|oo|a|||e \|er yoJ'·e d|scorrec¦ed. yoJ .||| see ¦|e da¦abase |cor .|¦| a ·ed ` |o.eve·. see|rg a ·ed `
does ro¦ recessa·||y near ¦|a¦ yoJ'·e d|scorrec¦ed `oJ n|g|¦ |ave beer d|scorrec¦ed ea·||e·. bJ¦ ¦|e
Da¦abase |.p|o·e· .as reve· ·e¦·es|ed To ve·|¦y ¦|e s¦a¦e o¦ ¦|e correc¦|or ¦o yoJ· da¦abase. yoJ s|oJ|d
c||c| ¦|e kefresh bJ¦¦or |r ¦|e Da¦abase |.p|o·e· ¦oo|ba·
+
||g|¦c||c| or yoJ· da¦abase raned Car1racker.mdf |r ¦|e Da¦abase |.p|o·e· ard se|ec¦ CIose
Connectìon `oJ s|oJ|d ro. see ¦|e ·ed ` rea· yoJ· da¦abase rane
`oJ'·e ro. d|scorrec¦ed To ·ecorrec¦. yoJ car do ¦|·ee d|¦
¦e·er¦ ¦||rgs `oJ car doJb|ec||c| yoJ· da¦abase rane (eg.
Car1racker.mdf) |r ¦|e Da¦abase |.p|o·e·. yoJ car c||c| ¦|e
|e¦·es| bJ¦¦or. o· yoJ car ·|g|¦c||c| or ¦|e ¦||erane |r ¦|e
Da¦abase |.p|o·e· ard se|ec¦ Modìfy Connectìon. . . |¦ yoJ
se|ec¦ ¦|e \od|¦y Correc¦|or ·oJ¦e. yoJ .||| see a d|a|og bo.
|||e ¦|e ore s|o.r |r ||gJ·e 8'
;^\jgZ-"(
Fb_hg\ba8kc_beXej\g[g[XaXj_lVeX
TgXWCa·T·ac|e·nd¦ WTgTUTfXY\_X
;^\jgZ-")
7TgTUTfX8kc_beXej\g[g[X6TeGeTV^Xe
WTgTUTfXVbaaXVgXW
;^\jgZ-"*
G[X@bW\Yl6baaXVg\baW\T_bZUbkj\___Xglbh
eXVbaaXVggblbhe6TeGeTV^XeWTgTUTfX
C08622132.indd 136 10/18/05 8:48:01 PM
Chapter 8. Managing the Data l37
,
|ecaJse |¦'s a good p·ac¦|ce ¦o ¦es¦ yoJ· correc¦|or. yoJ car c||c| ¦|e Tes¦ Correc¦|or bJ¦¦or ard |¦
.||| ve·|¦y ¦|e correc¦|or cJ··er¦|y spec|¦|ed |¦'s a|so ve·|¦y|rg ¦|a¦ SO| Se·ve· ´JJ' |.p·ess |d|¦|or |s
·eady ard ab|e ¦o ·ece|ve correc¦|ors ¦·on yoJ·
app||ca¦|ors
$#

C||c| OK ¦o ·ecorrec¦ ¦o yoJ· da¦abase
Creating TabIes in Your Database
Now you'll creaLe all Lables and relaLionships needed íor Lhe Car1racker applicaLion. Using
Lhe iníormaLion íound in Figure 8-2, you'll creaLe Lables, primary keys, idenLiLies, and
íoreign key relaLionships in Lhe Car1racker daLabase, and you'll do all oí Lhis wiLhouL leaving
visual SLudio.
TO Ck£AT£ TABL£S lN A DATABAS£
$
|e¦'s s¦a·¦ .|¦| ¦|e Co|o· ¦ab|e |r ¦|e Da¦abase |.p|o·e·. ·|g|¦c||c| or ¦|e ¦ab|e's ¦o|de· |cor ard
se|ec¦ Add New 1abIe `oJ s|oJ|d ro. see ar enp¦y g·|d or ¦|e des|gre· sJ·¦ace. .||c| |s ca||ed
¦|e Tab|e Des|gre· `oJ .||| a|so see ¦|a¦ a re. ¦oo|ba· |as appea·ed. .||c| |s ca||ed ¦|e Tab|e Des|gre·
¦oo|ba· T||s ¦oo|ba· |as a|| ¦|e ¦oo|s recessa·y ¦o |e|p yoJ c·ea¦e a ¦ab|e .|¦|oJ¦ .·|¦|rg a s|rg|e SO|
oJe·y
%
`oJ'|| ro. add a co|Jnr ¦o ¦|e Co|o· ¦ab|e Type CoIorID |r ¦|e Co|Jnr Nane ¦|e|d o¦ ¦|e Tab|e
Des|gre· Se|ec¦ ìnt as ¦|e Da¦a Type ard Jrc|ec| ¦|e AIIow NuIIs c|ec| bo. becaJse ¦||s co|Jnr
.||| be ¦|e p·|na·y |ey |r ¦||s ¦ab|e / p·|na·y |ey carro¦ be rJ|| s|rce |¦ |s pa·¦ o¦ ¦|e Jr|oJeress o¦ a ·o.
|r ¦|e ¦ab|e
&
|e¦o·e yoJ add ¦|e secord co|Jnr |r ¦|e Co|o· ¦ab|e. yoJ'|| se¦ ¦||s co|Jnr as ¦|e p·|na·y |ey To do
so. yoJ reed ¦o c||c| ¦|e Set Prìmary Key |cor |r ¦|e Tab|e Des|gre· ¦oo|ba·
T|e da¦abase d|ag·an s|o.r |r ||gJ·e 8´ |||Js¦·a¦es ¦|a¦ yoJ a|so reed ¦||s co|Jnr ¦o be ar |der¦|¦y.
¦|e·e¦o·e. yoJ reed ¦o nod|¦y ¦|a¦ p·ope·¦y |r ¦|e Co|Jnr |·ope·¦|es .|rdo. ·|g|¦ be|o. ¦|e Tab|e
Des|gre· Sc·o|| do.r Jr¦|| yoJ see ¦|e |der¦|¦y Spec|¦|ca¦|or g·oJp C||c| or ¦|e ÷ s|gr |oca¦ed ¦o ¦|e |e¦¦ o¦
¦|e .o·ds lJentitv Sµecificcticn ¦o e.pard ¦||s g·oJp No. c||c| |r ¦|e (Is Identìty) ¦|e|d ard se¦ |¦ ¦o Yes
|eave bo¦| ¦|e |der¦|¦y Seed ard |der¦|¦y |rc·ener¦ se¦ a¦ 1 ¦o· ro.
CurrentIy, you have onIy one database in your projects,
but it's not unusuaI to need to connect to and get
information from two or more databases. That's why
Database Connections in the Database £xpIorer is there
as a tree, for it's representing each database as a node
in that tree. You have onIy one node in the tree, which
is your CarTracker database.
ABG 8 One thinç vcµ shcµlJ µcv
cttenticn tc in this Jiclcç bcx
is the Dctcbcse lilencme.
3eccµse vcµ JiJn´t scve cnv
files. evervthinç is still lcccteJ
in c temµcrcrv fclJer iJentifieJ
bv the ccntent cf the text bcx.
As sccn cs vcµ scve cll cf the
files in vcµr µrcject. the Jctc-
bcse will be scveJ clcnç with
the cther µrcject files. wherever
thev cre lcccteJ. Ycµ ccn lcter
verifv thct lcccticn bv çcinç
tc the 1ccls menµ. selectinç
Oµticns. . . . cnJ then lcclinç
ct the Prcjects cnJ Sclµticns
ncJe in the tree. On the riçht
µcnel in this Jiclcç bcx. vcµ
ccn Jetermine where vcµr µrcj-
ects cre stcreJ bv lcclinç ct
the first text bcx cclleJ visµcl
StµJic Prcject lcccticns.
lrom this point onward, for every
tree controI and every controI that
is a group {i.e., has a + sign), the
word ZmeVcY wiII be used instead
of repeating the words Xa^X`dci]Z
h^\c.
ABG 8
C08622132.indd 137 10/18/05 8:48:02 PM
l38 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
'
To add aro¦|e· co|Jnr. c||c| |r ¦|e ·o. Jrde· ¦|e Co|o·|D co|Jnr rane /dd ¦|e ¦.o ·ena|r|rg co|
Jnrs based or ¦|e d|ag·an s|o.r |r ||gJ·e 8´ `oJ car se¦ ¦|e s|.e o¦ ¦|e Co|o·Nane rva·c|a· ¦o
30 by ¦yp|rg |r ¦|e Da¦a Type ¦|e|d \|er dore. yoJ· ¦ab|e s|oJ|d |oo| |||e ¦|e ore s|o.r |r ||gJ·e 8b
ln the TabIe Designer, the IittIe
bIack triangIe indicates the cur-
rent row.
ABG 8
As a reminder, when a coIumn is
an identity, SOL Server automati-
caIIy generates a new number each
time a row is created in a tabIe.
lt starts at the vaIue indicated by
the ldentity Seed property and
increases in increments by the
vaIue indicated by the ldentity
lncrement property.
ABG 8
;^\jgZ-"+
GTU_X7Xf\ZaXej\g[T__bYg[XVb_h`afYbeg[X6b_begTU_X
(
No. ¦|a¦ yoJ'·e dore .|¦| ¦|e des|gr. yoJ reed ¦o add ¦|e ¦ab|e ¦o ¦|e da¦abase To do ¦||s. yoJ
reed ¦o save ¦|e ¦ab|e C||c| ¦|e Save |cor o· p·ess CtrI+S \|er ¦|e C|oose Nane d|a|og bo.
appea·s. as s|o.r |r ||gJ·e 8¯. rane yoJ· ¦ab|e CoIor ard ¦|er c||c| OK
;^\jgZ-",
G[X6[bbfXAT`XW\T_bZUbkf[bj\aZg[X6b_begTU_XaT`X
)
|.pard ¦|e 1abIes ¦o|de· |r ¦|e Da¦abase |.p|o·e· ¦o v|e. ¦|e ||s¦ o¦ e.|s¦|rg ¦ab|es |r ¦|e da¦abase.
¦|e re. Co|o· ¦ab|e s|oJ|d appea· \|er yoJ e.pard ¦|e CoIor ¦ab|e ¦o v|e. ¦|e ||s¦ o¦ co|Jnrs. a||
¦|·ee co|Jnrs ¦|a¦ yoJ |Js¦ c·ea¦ed s|oJ|d appea·. as s|o.r |r ||gJ·e 88
* C|ose ¦|e Co|o· ¦ab|e |r ¦|e Tab|e Des|gre· by c||c||rg ¦|e X rea· ¦|e So|J¦|or |.p|o·e·
+
C||c| ¦|e Save AII |cor |r ¦|e ¦oo|ba· ¦o save yoJ· p·o|ec¦ or d|s| \a|e sJ·e ¦|e p·o|ec¦ rane |s
Ca·T·ac|e· ard c||c| ¦|e Save bJ¦¦or
,
|e¦o·e c·ea¦|rg o¦|e· ¦ab|es. ·ead ¦||s s¦ep conp|e¦e|y No. ¦|a¦ yoJ |ave ¦|e
|ro.|edge ¦o c·ea¦e a ¦ab|e. c·ea¦e a|| ·ena|r|rg ¦ab|es (Co|o·Type. \a|e. ard ||s¦|rg) Js|rg ¦|e sane
¦ec|r|oJes yoJ've |Js¦ |ea·red \a|e sJ·e ¦|a¦ a|| ¦ab|es ard cll oí ¦|e|· co|Jnrs a·e ·ec·ea¦ed e.ac¦|y ¦|e
sane .ay |r yoJ· ¦ab|es as s|o.r |r ||gJ·e 8´ Dor'¦ .o··y aboJ¦ es¦ab||s||rg ¦|e ·e|a¦|ors||ps. ¦o· yoJ'||
c·ea¦e ¦|ose |r ¦|e ¦o||o.|rg e.e·c|ses |e¦.eer eac| ¦ab|e c·ea¦|or. save yoJ· re. ¦ab|e |nned|a¦e|y ard
na|e sJ·e |¦ appea·s |r ¦|e Da¦abase |.p|o·e· T|er c|ose ¦|e ¦ab|e |r ¦|e des|gre· sJ·¦ace as s|o.r ea·||e·
|r s¦ep ¯ o¦ ¦||s sec¦|or
;^\jgZ-"-
7TgTUTfX8kc_beXej\g[g[XGTU_Xf
Yb_WXeTaW6b_begTU_XXkcTaWXW
whenever you cIick on a coIumn
name in the Database £xpIorer,
you'II see the properties Iisted
in the Properties window. This is
the same Properties window that
you've been using with one minor
difference. it is a read-onIy view
and therefore does not Iet you
modify information.
G < C
C08622132.indd 138 10/18/05 8:48:03 PM
Chapter 8. Managing the Data l39
Creating keIationships Between the TabIes
¥ou have creaLed Lables, buL Lhey don'L have any relaLionships. ¥ou'll now add Lhose rela-
Lionships and make sure your daLabase has daLa inLegriLy Lo cover Lhe basis oí orphaned
rows. Like many oLher elemenLs in visual 8asic 2005 Fxpress FdiLion, Lhere's more Lhan one
way Lo creaLe Lhose relaLionships. One is more visual Lhan Lhe oLher, and you'll sLarL wiLh Lhis
more visual approach so as Lo sLay íocused on Lhe main idea oí Lhe book, which is being
producLive.
8eíore you're able Lo creaLe Lhe relaLionships visually, Lhere is a prerequisiLe Lo add Lo
your projecL. a daLabase diagram. lL mighL noL look exacLly as Lhe one shown in Figure 8-2,
buL iL will be similar.
TO Ck£AT£ k£LATlONSHlPS B£Tw££N TABL£S
$
Co ¦o ¦|e Da¦abase |.p|o·e· ard ·|g|¦c||c| ¦|e Database Dìagrams rode |oca¦ed above ¦|e Tab|es
rode Se|ec¦ Add New Dìagram / d|a|og bo. .||| appea· |rd|ca¦|rg ¦|a¦ SO| Se·ve· ´JJ' |.p·ess
|d|¦|or doesr'¦ |ave a|| o¦ ¦|e da¦abase ob|ec¦s |¦ reeds |¦ yoJ .ar¦ ¦o c·ea¦e da¦abase d|ag·ans
%
C||c| Yes ¦o |ave SO| Se·ve· c·ea¦e ¦|e conporer¦s |¦ reeds ¦o ob¦a|r a da¦abase d|ag·an \|er |¦'s
dore c·ea¦|rg. yoJ s|oJ|d be as|ed .||c| ¦ab|es yoJ .ar¦ ¦o add ¦o yoJ· d|ag·an |r ¦|e /dd Tab|e
d|a|og bo.
&
Se|ec¦ a|| o¦ ¦|e ¦ab|es yoJ c·ea¦ed ard ¦|er c||c| Add |¦ s|oJ|d ¦a|e |ess ¦|ar a n|rJ¦e ¦o· yoJ· d|a
g·an ¦o appea· C||c| ¦|e CIose bJ¦¦or ¦o |rd|ca¦e ¦o \|sJa| S¦Jd|o ¦|a¦ yoJ |ave a|| ¦|e ¦ab|es yoJ reed
'
C||c| ¦|e Save AII bJ¦¦or o· p·ess CtrI+Shìft+S `oJ'|| be as|ed ¦o save yoJ· d|ag·an ard c|oose a
rane Nane yoJ· d|ag·an Car1rackerDìagram
(
|¦ yoJ dor'¦ see yoJ· da¦abase d|ag·an. ¦|·s¦ go ¦o yoJ· Database Dìagrams rode. e.pard |¦. ard ¦|er
oper ¦|e d|ag·an by doJb|ec||c||rg or |¦ `oJ s|oJ|d see ¦|e des|gre· sJ·¦ace .|¦| a|| o¦ yoJ· ¦ab|es
Depending on your resoIution, the
view might be tight. lf you want
to view more of the diagram, you
might need to unpin or cIose some
windows, such as the SoIution
£xpIorer or the Properties window,
you can return these items to your
screen by going to the View menu
and seIecting SoIution £xpIorer or
the Properties window. You can
aIso change the zoom vaIue by
changing the vaIue in the Zoom
drop-down Iist.
G < C
C08622132.indd 139 10/18/05 8:48:04 PM
l40 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
|e¦'s ¦ocJs or ore ·e|a¦|ors||p ¦|a¦ .e reed ¦o c·ea¦e \|er yoJ |oo| a¦ ||gJ·e 8´. yoJ'|| see ¦|a¦ ¦|e
Co|o·|D co|Jnr |s p·eser¦ |r ¦|e ||s¦|rg ¦ab|e becaJse ¦|e·e's a ·e|a¦|or ¦o ¦|e Co|o· ¦ab|e T|e ||re be¦.eer
bo¦| ¦ab|es |s a ¦o·e|gr |ey (||) ·e|a¦|ors||p `oJ reed ¦o |ave ¦||s ·e|a¦|ors||p es¦ab||s|ed o· o¦|e·.|se
yoJ'|| |ave o·p|ared rodes |r ¦|e ||s¦|rg ¦ab|e .|ereve· a Co|o· ·o. |s de|e¦ed T||s nears ¦|a¦ yoJ |ave
¦o es¦ab||s| a ·e|a¦|ors||p be¦.eer ¦|e p·|na·y |ey ¦ab|e ard ¦|e ¦o·e|gr |ey ¦ab|e |r ¦||s case. |¦ nears
yoJ reed ¦o c·ea¦e a ·e|a¦|ors||p ¦·on ¦|e Co|o· ¦ab|e ¦o.a·d ¦|e ||s¦|rg ¦ab|e
)

|r ¦|e da¦abase d|ag·an. c||c| or CoIorID |r ¦|e Co|o· ¦ab|e .|e·e yoJ see ¦|e sna|| ye||o. |ey
*
|oo| a¦ ||gJ·e 8º ¦o see .|e·e yoJ s|oJ|d be a¦ ¦|e erd o¦ ¦||s nar|pJ|a¦|or |o|d ¦|e |e¦¦ bJ¦¦or
do.r ard d·ag CoIorID ¦o.a·d ¦|e ||s¦|rg ¦ab|e. yoJ s|oJ|d see a ||re appea· as yoJ d·ag /||gr
yoJ· noJse cJ·so· so ¦|a¦ |¦'s ove· ¦|e co|Jnr .|¦| .||c| yoJ .ar¦ ¦o c·ea¦e ¦|e ·e|a¦|ors||p¬|r yoJ·
case. ove· ¦|e CoIorID ¦|e|d |r ¦|e ||s¦|rg ¦ab|e \|er yoJ see a sna|| ÷ appea·. ¦|er d·op |¦
+
|¦ yoJ co··ec¦|y se|ec¦ed ard ·e|eased ¦|e noJse orce yoJ .e·e ove· Co|o·|D
|r ¦|e Co|o· ¦ab|e. yoJ s|oJ|d see a Tab|es /rd Co|Jnrs d|a|og bo. ¦|a¦ as|s
yoJ ¦o cor¦|·n ¦|e c·ea¦|or o¦ ¦|e || ·e|a¦|ors||p |¦'s |npo·¦ar¦ ¦o· eac| ¦ab|e ¦|a¦
Co|o·|D |s ¦|e co|Jnr rane ¦|a¦ appea·s ¦o ||r| bo¦| ¦ab|es |r ¦|a¦ d|a|og bo. |¦
¦|e p·|na·y |ey ard ¦o·e|gr |ey ¦ab|es a·e co··ec¦ ard ¦|e se|ec¦ed co|Jnr ranes
a·e co··ec¦. ¦|er c||c| ¦|e OK bJ¦¦or
,
`oJ s|oJ|d ¦|er see ¦|e |o·e|gr |ey |e|a¦|ors||p d|a|og bo. s|o.r |r
||gJ·e 81J
;^\jgZ-".
6eXTg\aZg[XYbeX\Za^XleX_Tg\baf[\c
UXgjXXag[X6TeGlcXTaW6TegTU_Xf
;^\jgZ-"&%
9beX\Za>XlEX_Tg\baf[\cW\T_bZUbkYbe
g[X?\fg\aZgb6b_begTU_Xf
C08622132.indd 140 10/18/05 8:48:05 PM
Chapter 8. Managing the Data l4l
$#
/|¦|oJg| yoJ car c|arge sone p·ope·¦|es .|¦||r ¦||s d|a|og bo.. |Js¦ c||c| OK ¦o· ro. See
||gJ·e 811 ¦o v|e. ¦|e d|ag·an .|¦| ¦|e re. ·e|a¦|ors||p c·ea¦ed
;^\jgZ-"&&
@bW\Y\XWW\TZeT`f[bj\aZg[XaXj9>eX_Tg\baf[\c
UXgjXXag[X?\fg\aZTaW6b_begTU_Xf
To reinforce the concept of estab-
Iishing reIationships between
tabIes, Iet me give you another
way of Iooking at the reIationship
in this exercise. There are two
reasons why the CoIorlD coIumn is
in the Listing tabIe as an lK. The
first reason is that it is used for a
normaIization and design principIe
because you don't want to have
dupIicate data. The second reason
is that it is used for data integrity
reasons and, more specificaIIy,
for the orphaned rows probIem.
Let's Iook at it with some sampIe
data. Suppose there is a CoIor row
caIIed Dark BIue and the Listing
tabIe contains six different ad def-
initions that are Dark BIue. lf you
remove the Dark BIue coIor from
the CoIor tabIe, it wouId mean
that those six ads wouId have
orphaned data. That is why you
created a foreign key reIationship.
to make sure that if an appIication
or a user tries to remove data in
the CoIor tabIe, a process within
SOL Server 2005 wouId prevent
this by vaIidating that there are
no "kids¨ Ieft behind in the Listing
tabIe before aIIowing the deIetion
to occur in the CoIor tabIe.
@BE8 < A9 B
T|e ¦|·s¦ ¦||rg ¦o ro¦e or ¦|e d|ag·an |s ¦|e |r¦|r|¦y synbo| |oca¦ed c|ose ¦o ¦|e ||s¦|rg ¦ab|e ard ¦|e
ye||o. |ey |oca¦ed c|ose ¦o ¦|e Co|o· ¦ab|e T|e |r¦|r|¦y synbo| or ¦|e ||s¦|rg ¦ab|e |rd|ca¦es ¦|e ¦ab|e's ca·
d|ra||¦y |¦ |rd|ca¦es ¦|a¦. |r ¦||s ·e|a¦|ors||p. ¦|e Ca· ¦ab|e car cor¦a|r nary ·o.s .|¦| |r¦o·na¦|or con|rg
¦·on ¦|e na¦c||rg p·|na·y |ey ¦ab|e T|e ye||o. |ey |rd|ca¦es ¦·on .||c| ¦ab|e ¦|e p·|na·y |ey |s be|rg
¦a|er
| ·ea··arged ¦|e d|ag·an so ¦|a¦ ¦|e ¦.o ¦ab|es a·e c|ose ¦oge¦|e· `oJ car ·ea··arge yoJ· ¦ab|es ary .ay
yoJ .ar¦ by d·agg|rg ¦|en by ¦|e ¦|¦|e ba· (|e. .|e·e ¦|e ¦ab|e rane |s d|sp|ayed) T||s |s sone¦|nes rec
essa·y ¦o do .|er yoJ c·ea¦e ·e|a¦|ors||ps so ¦|a¦ yoJ do ro¦ erd Jp .|¦| ar JrJsJa||oo||rg d|ag·an |
sJgges¦ ¦|a¦ yoJ pJ¦ yoJ· ||s¦|rg ¦ab|e |r ¦|e n|dd|e o¦ yoJ· o¦|e· ¦ab|es becaJse |¦ .||| be eas|e· ¦o c·ea¦e
·e|a¦|ors||ps ¦||s .ay `oJ car a|so ·ea··arge ¦|e ¦ab|es or yoJ· d|ag·an a¦ ary ¦|ne by ·|g|¦c||c||rg ary
.|e·e e.cep¦ or a ¦ab|e or ¦|e d|ag·an's des|gre· sJ·¦ace ard se|ec¦|rg Arrange 1abIes `oJ car a|so
|ave ¦|e |abe|s ¦o· eve·y ·e|a¦|ors||p appea· or ¦|e d|ag·an by ·|g|¦c||c||rg ¦|e d|ag·an's des|gre· sJ·
¦ace ard se|ec¦|rg Show ReIatìonshìp LabeIs. as s|o.r |r ||gJ·e 81´
C08622132.indd 141 10/18/05 8:48:06 PM
l42 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
$$ No. c·ea¦e ¦|e o¦|e· || ·e|a¦|ors||ps by Js|rg e|¦|e· ||gJ·e 8´ o· ¦|e ¦o||o.|rg ¦ab|e
CoIumn Primary Key TabIe loreign Key TabIe
MakelD Make LisLing
Car1ypelD Car1ype LisLing
\|er ¦|r|s|ed. ¦|e cor¦er¦ o¦ yoJ· d|ag·an s|oJ|d ·esenb|e ¦|e cor¦er¦ s|o.r |r ||gJ·e 81´ \a|e sJ·e
yoJ· ·e|a¦|ors||ps a·e a··arged p·ope·|y by |oo||rg a¦ .|e·e ¦|e |r¦|r|¦y synbo|s ard ye||o. |eys a·e |oca¦
ed ard a|so by |oo||rg a¦ ¦|e p·ev|oJs ¦ab|e ¦o· ve·|¦|ca¦|or
$%
C||c| ¦|e Save AII bJ¦¦or o· p·ess CtrI+Shìft+S ¦o conn|¦ ¦|e c|arges ¦o ¦|e da¦abase C||c| `es
.|er as|ed |¦ yoJ .ar¦ ¦o save
;^\jgZ-"&'
6b`c_XgXW6TeGeTV^XeWTgTUTfX
W\TZeT`
IVWaZ-")
?\fgbYYbeX\Za^XleX_Tg\baf[\cfgbVeXTgX
You can aIways go back and review
the properties of any reIationship
by doubIe-cIicking on the Iine or
by right-cIicking and seIecting
ProperLies from the context-sensi-
tive menu.
@BE8 < A9 B
C08622132.indd 142 10/18/05 8:48:07 PM
Chapter 8. Managing the Data l43
£ntering Data in SOL Server TabIes Using VisuaI Studio
Now LhaL you have creaLed all oí your Lables and relaLionships, you'll sLarL inserLing daLa in
your Lables and veriíying LhaL your consLrainLs ensure Lhe daLa inLegriLy oí your daLabase.
LeL's sLarL by adding daLa in all Lables. ¥ou'll íirsL add rows inLo Lhe Color Lable.
TO £NT£k DATA lN SOL S£kV£k TABL£S USlNG VlSUAL STUDlO
$
To s¦a·¦ er¦e·|rg ·o.s |r ¦|e Co|o· ¦ab|e. ·|g|¦c||c| ¦|e CoIor ¦ab|e |r ¦|e Da¦abase |.p|o·e· ard
se|ec¦ Show 1abIe Data `oJ· des|gre· sJ·¦ace s|oJ|d |ave a g·|d |||e ¦|e ore s|o.r |r ||gJ·e 81`
;^\jgZ-"&(
8`cgl6b_begTU_X\ag[XgTU_XWTgTZe\W
%
|e¦'s add ¦|e ¦|·s¦ co|o· C||c| |r ¦|e CoIor Name ¦|e|d. ¦ype Dark ßIue, ard ¦|er p·ess ¦|e 1ab |ey ¦o
go ¦o ¦|e re.¦ co|Jnr Type true |r ¦|e MetaIIìc ¦|e|d |ecaJse ¦|a¦ co|Jnr ¦ype |s a b|¦. |¦s va|Jes
car or|y be e|¦|e· ¦·Je o· ¦a|se becaJse a b|¦ ¦ype |s a b|ra·y ¦ype \|er yoJ'·e dore. p·ess ¦|e 1ab |ey ¦o
go ¦o ¦|e re.¦ ·o.
While tvµinç vcµr Jctc.
lccl tc the tcble´s left-mcst
crec in the tcbleJctc çriJ cnJ
vcµ´ll see c smcll blccl tricnçle
. c µencil iccn
bcth c stcr cnJ c smcll blccl
tricnçle cr c stcr
1he µencil inJicctes
thct vcµ´re mclinç c mcJificc-
ticn tc the rcw.1he stcr inJi-
cctes c new rcw. cnJ the smcll
blccl tricnçle inJicctes the cµr-
rent rcw.
C08622132.indd 143 10/18/05 8:48:08 PM
l44 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
&
/dd ¦|·ee no·e ca· co|o·s¬Red. SìIver. ard ßIack¬ard se¦ |ed as MetaIIìc ard ¦|e o¦|e·
¦.o co|o·s as Non-metaIIìc (|e. ¦a|se) \|er dore. ¦|e ¦ab|e s|oJ|d |oo| |||e ¦|e ore
s|o.r |r ||gJ·e 814
' /dd ¦|e ¦o||o.|rg da¦a ¦o ¦|e \a|e ard ¦|e Ca·Type ¦ab|es
Make TabIe
MakeName Country
CoodRoadsLer Cermany
SmallCar France
8igSUv USA
ReliableCar Japan
CarType TabIe
CarTypeName NumberOfDoors
RoadsLer 2
SUv 5
HaLchback 5
Sedan 4
Coupe 2
`oJ n|g|¦ ro¦ |ave ·ea||.ed ¦|a¦ by g|v|rg a ¦ype ¦o yoJ· da¦a. yoJ ac¦Ja||y added da¦a |r¦eg·|¦y ve·|¦|ca¦|or
¦o yoJ· da¦abase T·y nod|¦y|rg ore o¦ ¦|e Co|o· ·o.s by c|arg|rg ¦|e \e¦a|||c co|Jnr ¦o ·ead HellcwcrlJ
|rs¦ead o¦ ¦·Je o· ¦a|se `oJ'|| ge¦ ar e··o· nessage ¦e|||rg yoJ ¦|a¦ ¦|e \e¦a|||c ¦|e|d |s o¦ ¦ype |oo|ear
To s|o. |o. da¦a |r¦eg·|¦y |s p·ese·ved Js|rg ¦|e ¦o·e|gr |ey cors¦·a|r¦s. yoJ'|| add ¦.o ||s¦|rg ·o.s `oJ
.||| er¦e· no·e ·o.s .|er Js|rg yoJ· \|rdo.s |o·n app||ca¦|or
(

||g|¦c||c| or ¦|e Lìstìng ¦ab|e. se|ec¦ Show 1abIe Data. ard add ¦|e ¦o||o.|rg ¦.o ·o.s
;^\jgZ-"&)
6b_begTU_Xj\g[YbheaXjebjfbYWTgT
lcr cll cclµmns thct vcµ
crecteJ cs cn iJentitv. Jcn´t
tvµe the Jctc beccµse the fielJ
will cµtcmcticcllv be çenercteJ
bv SOl Server 200S lxµress
lJiticn whenever the rcw is
crecteJ in the tcble. lf vcµ trv
tc tvµe Jctc in cn lJentitv ccl-
µmn. vcµ will nct be cllcweJ
tc Jc sc. When in cn lJentitv
cclµmn. it stctes thct the cell is
recJ-cnlv necr the ncviçcticn
bcr ct the bcttcm cf the 1cble
Desiçner.
C08622132.indd 144 10/18/05 8:48:08 PM
Chapter 8. Managing the Data l45
Listing TabIe
CoIorlD MakelD CarTypelD DateSeen Year Price CyIinder HP UkL £PGCity £PGHighway Notes
í í í 08/íí/2005 2005 42500 6 240
hLLp.//www.
liLwareinc.com/
20 28
1his is my dream car,
íollow regularly.
4 8 2 07/80/2005 2008 89775 8 840
hLLp.//www.
cpandl.com/
í0 í5 1oo much gas
)
`oJ'|| ro. ve·|¦y ¦|a¦ ore o¦ yoJ· ¦o·e|gr |ey cors¦·a|r¦s |s .o·||rg co··ec¦|y Cper ¦|e \a|e ¦ab|e by
·|g|¦c||c||rg or ¦|e Make ¦ab|e ard se|ec¦|rg Show 1abIe Data
*
|e¦'s ¦·y ¦o de|e¦e ¦|e ¦|·s¦ ·o. by c||c||rg or ¦|e |e¦¦nos¦ ¦|e|d .|e·e ¦|e perc|| JsJa||y appea·s T|e
·o. s|oJ|d be se|ec¦ed ard a|| ¦|e|ds s|oJ|d be b|Je ||g|¦c||c| ard se|ec¦ DeIete
+ / d|a|og bo. s|oJ|d appea· or yoJ· sc·eer |roJ|·|rg .|e¦|e· yoJ ·ea||y .ar¦ ¦o de|e¦e ¦|e ·o. C||c| Yes
,
`oJ s|oJ|d ·ece|ve a d|a|og bo. e··o· nessage s¦a¦|rg ¦|a¦ ¦|e ·o. .as ro¦ de|e¦ed becaJse o¦ ¦|e
¦o·e|gr |ey cors¦·a|r¦ ¦|a¦ ·eads as ¦o||o.s frror Message: 1he DfLf1f statement confIìcted wìth
the RfffRfNCf constraìnt ¨fK_Lìstìng_Make." T||s s¦a¦ener¦ a¦¦|·ns .|y ¦|e ¦o·e|gr |ey cors¦·a|r¦ .as
c·ea¦ed. .||c| .as ¦o avo|d o·p|ared ·o.s ||gJ·e 81' dep|c¦s .|a¦ ¦|e e··o· d|a|og bo. |oo|s |||e ard
.|a¦ ||rd o¦ |r¦o·na¦|or |s p·ov|ded ¦o |e|p yoJ debJg ¦|e p·ob|en. |¦ recessa·y |r ¦||s case. |¦'s ro¦ a
p·ob|en bJ¦ a ¦ea¦J·e o¦ yoJ· c·ea¦|or¦
$#

C||c| OK ¦o e.|¦ ¦||s d|a|og bo.
$$
Tes¦ yoJ· o¦|e· cors¦·a|r¦s ·e|a¦ed ¦o
¦|e ||s¦|rg ¦ab|e by ¦·y|rg ¦o de|e¦e
¦|e ¦|·s¦ ·o. o¦ ¦|e Ca·Type ¦ab|e `oJ s|oJ|d
·ece|ve ¦|e sane e··o· nessage
You can navigate through the
tabIe by using the navigation
controIs at the bottom of the grid.
These controIs wiII aIIow you to
do things such as move to the first
and Iast row, move to the previ-
ous and next entry, move to a new
record, or directIy type in the row
number.
@BE8 < A9 B
;^\jgZ-"&*
8eebeW\T_bZUbkf[bj\aZg[XYbeX\Za
^XleX_Tg\baf[\cceXiXag\aZg[XWX_X
g\babYTebjYeb`g[X@T^XgTU_X
C08622132.indd 145 10/18/05 8:48:09 PM
l46 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
Now LhaL you have all oí your domain Lables loaded wiLh some daLa, you'll now learn Lo
use Lhe daLabase in a Windows Forms applicaLion. ¥ou'll learn abouL ADO.NF1 and abouL
daLabindings wiLh Windows Form conLrols.
what Are ADO.N£T and Databinding?
¥ou rarely enLer all daLa manually using visual SLudio. ¥ou Lypically leL Lhe user do iL or you
do iL Lhrough an applicaLion. ¥ou can also eiLher imporL daLa írom anoLher source or creaLe
Lhe new daLa using SOL scripLs, buL Lhis is a more advanced concepL LhaL will noL be covered
in Lhis book.
1his secLion will íocus on how Lo build Windows applicaLions LhaL can connecL Lo and
receive daLa írom a SOL Server 2005 Fxpress FdiLion using ADO.NF1. 1he íollowing is a
íormal, oííicial deíiniLion oí ADO.NF1 írom Lhe MSDN online library.
ADO.Nl1 µrcviJes ccnsistent cccess tc Jctc scµrces. sµch cs Micrcscft SOl Server. cs well
cs Jctc scµrces exµcseJ thrcµçh Oll D3 cnJ XMl. Dctc-shcrinç ccnsµmer cµµliccticns ccn
µse ADO.Nl1 tc ccnnect tc these Jctc scµrces cnJ retrieve. mcniµµlcte. cnJ µµJcte Jctc.
ADO.Nl1 clecnlv fcctcrs Jctc cccess frcm Jctc mcniµµlcticn intc Jiscrete ccmµcnents
thct ccn be µseJ seµcrctelv cr in tcnJem. ADO.Nl1 inclµJes .Nl1 lrcmewcrl Jctc µrcviJers
fcr ccnnectinç tc c Jctcbcse. execµtinç ccmmcnJs. cnJ retrievinç resµlts. 1hcse resµlts cre
either µrccesseJ Jirectlv cr µlcceJ in cn ADO.Nl1 DctcSet cbject in crJer tc be exµcseJ tc
the µser in cn cJ-hcc mcnner. ccmbineJ with Jctc frcm mµltiµle scµrces. cr remcteJ between
tiers. 1he ADO.Nl1 DctcSet cbject ccn clsc be µseJ inJeµenJentlv cf c .Nl1 lrcmewcrl Jctc
µrcviJer tc mcncçe Jctc lcccl tc the cµµliccticn cr scµrceJ frcm XMl.
1he ADO.Nl1 clcsses cre fcµnJ in Svstem.Dctc.Jll cnJ cre inteçrcteJ with the XMl
clcsses fcµnJ in Svstem.Xml.Jll. When ccmµilinç ccJe thct µses the Svstem.Dctc ncmesµcce.
reference bcth Svstem.Dctc.Jll cnJ Svstem.Xml.Jll.
l wanLed Lo presenL Lhe long and íormal deíiniLion oí ADO.NF1 because iL conLains
elemenLs LhaL you'll learn abouL while working wiLh Lhe Car 1racker applicaLion. l also chose
iL because l would like you Lo reíer back Lo iL whenever you're working wiLh ADO.NF1. Here
is a less íormal deíiniLion LhaL l Lhink summarizes whaL ADO.NF1 is all abouL.
lf vcµ wcnt mcre infcrmc-
ticn cbcµt SOl cnJ 1rcnscct-
SOl. vcµ shcµlJ JcwnlccJ
the SOl Server 200S lxµress
Jccµmentcticn. Ycµ ccn finJ
this infcrmcticn ct the fcllcw-
inç linl. httµ.//çc.micrcscft.
ccm/fwlinl/?linllJ=S1842.
SOl Server 200S lxµress
Jccµmentcticn is JesiçneJ tc
helµ vcµ cnswer mcst qµes-
ticns vcµ miçht hcve. bµt it
miçht clsc refer vcµ tc the
SOl Server 200S Jccµmentc-
ticn. Ycµ ccn JcwnlccJ it ct
httµ.//çc.micrcscft.ccm/fwlinl/
?linllJ=S184J.
C08622132.indd 146 10/18/05 8:48:10 PM
Chapter 8. Managing the Data l47
¥ou can say LhaL ADO.NF1 is Lhe .NF1 Framework way oí accessing and programmaLically
manipulaLing daLabases or daLa using XML sources.
New Lo ADO.NF1 2.0 are new ways oí accessing daLa írom diííerenL sources. ln visual
8asic 2005 Fxpress FdiLion, you are limiLed Lo Lhe íollowing daLa sources. daLabases (SOL
Server Fxpress and MicrosoíL Access daLabases), Web services, and cusLom objecLs. lL is much
easier (i.e., Lhere is less code) Lo manipulaLe daLa in ADO.NF1 2.0, especially when using all
oí Lhe Lools included in visual SLudio 2005. 1here are many new wizards and oLher Lools LhaL
make Lhe experience oí working wiLh daLabases a pleasanL one. visual SLudio 2005 covers
numerous common scenarios wiLh iLs Lools and wizards, buL iL's also very poweríul when
used programmaLically wiLhouL Lhe use oí Lhe visual Lools. ¥ou will learn Lhe basics in Lhis
book, buL Lhere's noLhing prevenLing you írom learning more abouL daLabinding and ADO.
NF1 and írom unleashing poweríul applicaLions.
8eíore proceeding any íurLher, leL's Lalk abouL Lhe Car 1racker applicaLion. 1he main goal
oí Lhe applicaLion is Lo Lrack car ads over Lhe lnLerneL. As you have your daLabases ready Lo go,
you now need Lo consider whaL will be included in Lhis applicaLion. ln realiLy, whaL you need
is simply a way oí displaying Lhe ads, adding new ads, modiíying/deleLing exisLing ads, and
searching Lhrough Lhe ads using a series oí drop-down boxes LhaL allow you Lo narrow your
search based on cerLain criLeria. 1hese search criLeria will come direcLly írom Lhe domain Lables
(i.e., separaLe drop-down conLrols íor Lhe car Lype, color, make, and so íorLh).
When using drop-down conLrols or any oLher conLrols wiLh daLa LhaL you know exisL in
your daLabase, you don'L wanL Lo populaLe Lhe daLa by hand. ¥ou wanL Lo use Lhe daLabinding
capabiliLies oí a conLrol. Databinding is an easy and LransparenL way Lo read/wriLe daLa and a
link beLween a conLrol on a Windows Form and a daLa source írom your applicaLion.
ADO.NF1 Lakes care oí a greaL deal oí acLiviLy behind Lhe scenes (iL's even beLLer in
.NF1 Framework 2.0), as well as managing Lhe connecLion Lo Lhe daLabase. Managing
Lhe connecLion doesn'L sLop aL opening and closing Lhe connecLion, buL also concerns
iLselí wiLh íinding Lhe daLabase wiLh which you're Lrying Lo connecL. When a connecLion
is opened, iL means your applicaLion can Lalk Lo Lhe daLabase Lhrough ADO.NF1 meLhod
calls. All exchanges (send/receive) oí daLa beLween your applicaLion and Lhe daLabase are
managed íor you by ADO.NF1. 1he daLa iLselí is also managed by ADO.NF1 Lhrough diverse
mechanisms. read-only íorward navigaLion, navigaLion in any direcLion wiLh read-wriLe, íield
evaluaLion, and so íorLh. And Lhe beauLy oí iL is LhaL you usually don'L have Lo wriLe a loL oí
code Lo enjoy Lhose nice íeaLures.
visµcl 3csic lxµress cllcws
vcµ tc wcrl Micrcscft Access
Jctcbcses. bµt wcrlinç with
SOl Server 200S lxµress lJiticn
çives vcµ cll the benefits cf the
lnterµrise qµclitv cf SOl Server
200S. with the cnlv JcwnsiJe
beinç c reJµceJ set cf fectµres.
Not aII windows lorm controIs
are "databinding aware.¨ when
they are aware, they have a
DataBindings property.
ABG 8
C08622132.indd 147 10/18/05 8:48:10 PM
l48 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
The Car Tracker AppIication DeveIopment
¥ou'll now proceed Lo Lhe developmenL oí Lhe Car 1racker applicaLion. FirsL, you need Lo cre-
aLe a daLaseL LhaL will provide you wiLh all Lhe daLabinding you need íor Lhe Car 1racker
applicaLion. Now LhaL your Lables are esLablished, you can coníigure Lhe daLaseL wiLh all oí
Lhe elemenLs you've jusL added Lo your daLabase.
8eíore creaLing a daLaseL, you musL learn whaL a daLaseL is. A daLaseL is an in-memory
represenLaLion oí one or more Lables and is used Lo sLore Lhe rows you reLrieve LhaL maLch
Lhe query you senL Lo Lhe daLabase. ¥ou can Lhen add, deleLe, or updaLe rows in memory.
When Lhe user is done, you can submiL, save, or commiL Lhe changes Lo Lhe daLabase. 1he
Car1rackerDaLaSeL.xsd is called an XML Schema DeíiniLion íile. 1he .xsd íile ensures LhaL Lhe
daLa will be sLrucLured and respecL Lhe schema. 1his íile will be used laLer in Lhe projecL
when we discuss daLabinding.
1o creaLe a daLaseL, you'll learn Lo use Lhe DaLa Sources window. 1his window gives
you access Lo all oí Lhe daLa sources you have coníigured in your applicaLion. See Figure
8-í6 Lo see where Lhe DaLa Sources window is locaLed. lí you don'L see Lhe DaLa Sources
window, you can access iL by clicking on Lhe DaLa menu and selecLing Show DaLa Sources.
lí Show DaLa Sources does noL appear on Lhe DaLa menu, be sure you have closed all oí Lhe
Car1racker Lable daLa grids and Formí is visible.
TO Ck£AT£ A DATAS£T
$
|r ¦|e Da¦a SoJ·ces .|rdo.. c||c| ¦|e Add New Data Source ||r|. o· c||c| ¦|e Add New Data Source
bJ¦¦or |r ¦|e ¦oo|ba· T|e Da¦a SoJ·ce Cor¦|gJ·a¦|or \|.a·d appea·s
%
T|e ¦|·s¦ sc·eer o¦ ¦|e Da¦a SoJ·ce Cor¦|gJ·a¦|or .|.a·d a||o.s yoJ ¦o c|oose ¦|e da¦a soJ·ce ¦ype
yoJ .ar¦ ¦o c·ea¦e `oJ car c|oose a da¦abase. a \eb se·v|ce. o· ore o¦ yoJ· ob|ec¦s `oJ've |Js¦
bJ||¦ a da¦abase ¦o· ¦|e Ca· T·ac|e· app||ca¦|or. so yoJ'·e go|rg ¦o c|oose ¦|a¦ Da¦a SoJ·ce Type Se|ec¦
Database ard ¦|er c||c| Next
|r ¦|e re.¦ sc·eer. yoJ .||| c|oose yoJ· da¦a correc¦|or Ca·T·ac|e·nd¦ s|oJ|d a|·eady be se|ec¦ed \|er
yoJ c·ea¦ed ¦|e Ca·T·ac|e· SO| Se·ve· |.p·ess da¦abase |r yoJ· p·o|ec¦. a da¦a correc¦|or .as c·ea¦ed ¦o·
yoJ `oJ car c||c| ¦|e p|Js s|gr (+) |r ¦|e bo¦¦on o¦ ¦|e d|a|og bo. ¦o see .|a¦ ¦|e correc¦|or s¦·|rg |oo|s
|||e T||s correc¦|or s¦·|rg de¦|res |o. yoJ· app||ca¦|or .||| correc¦ ¦o ¦|e da¦abase
The Data Sources window might
end up somewhere eIse in your
lD£. Because your lD£ is entireIy
customizabIe to your Iiking, you
can customize your windows and
tabs to appear wherever you feeI
they are most productive for you.
ABG 8
;^\jgZ-"&+
G[X7TgTFbheVXfj\aWbj
C08622132.indd 148 10/18/05 8:48:11 PM
Chapter 8. Managing the Data l49
&

C||c| Next or ¦|e C|oose `oJ· Da¦a Correc¦|or sc·eer
T|e re.¦ sc·eer |r ¦|e .|.a·d |roJ|·es .|e¦|e· yoJ .ar¦ ¦o save ¦||s correc¦|or s¦·|rg |r ¦|e app||ca¦|or
cor¦|gJ·a¦|or ¦||e /s yoJ sa. |r ¦|e p·ev|oJs sc·eer. yoJ |ro. .|e·e yoJ· da¦abase |s s¦o·ed `e¦ yoJ
n|g|¦ c|arge yoJ· n|rd ard dep|oy ¦|e ¦||e sone.|e·e e|se |¦ yoJ do ¦|a¦. yoJ dor'¦ .ar¦ ¦o nod|¦y ¦|e
soJ·ce code ard ·econp||e |¦ |J¦¦|rg ¦|e correc¦|or s¦·|rg |r yoJ· app||ca¦|or cor¦|gJ·a¦|or ¦||e |s ac¦Ja||y
a bes¦ p·ac¦|ce |¦ g|ves yoJ ¦|e advar¦age o¦ or|y nod|¦y|rg ¦|e ¦||e ard ·es¦a·¦|rg ¦|e app||ca¦|or .|¦|oJ¦
·econp||a¦|or so as ¦o aJ¦ona¦|ca||y p|c| Jp ¦|e c|arges |r yoJ· correc¦|or s¦·|rg ard correc¦ ¦o ¦|a¦ re.
|oca¦|or
T|e app||ca¦|or cor¦|gJ·a¦|or |s s¦o·ed |r ar `\| ¦||e raned Js|rg ¦|e app||ca¦|or's e.ecJ¦ab|e rane
ard add|rg .cor¦|g a¦ ¦|e erd o¦ ¦|e e.ecJ¦ab|e ¦||erane |r oJ· app||ca¦|or. ¦|e ¦||e |s raned
Ca·T·ac|e·e.ecor¦|g. a|¦|oJg| yoJ or|y see appcor¦|g .|||e .o·||rg |r \|sJa| S¦Jd|o |¦ yoJ .ar¦ ¦o save
¦|e correc¦|or s¦·|rg. yoJ a·e a|so as|ed ¦o p·ov|de a va·|ab|e rane Jrde· .||c| |¦ .||| be saved |r ¦|e ¦||e
' \a|e sJ·e ¦|e Yes, Save 1he Connectìon As c|ec| bo. |s se|ec¦ed ard ¦|er c||c| Next
(
|r ¦|e re.¦ sc·eer. yoJ'|| se|ec¦ a|| ¦|e ¦ab|es ¦·on ¦|e da¦abase ¦|a¦ .||| be |r yoJ· da¦ase¦ ard rane
yoJ· da¦ase¦ |r yoJ· case. yoJ .||| reed a|| o¦ ¦|e ¦ab|es. so e.pard ¦|e Tab|es rode ard se|ec¦ a||
¦ab|es |eave ¦|e Da¦aSe¦ Nane se¦ ¦o Ca·T·ac|e·Da¦aSe¦ ard ¦|er c||c| fìnìsh
T|e ·esJ|¦ o¦ yoJ· da¦ase¦ cor¦|gJ·a¦|or |s ar .sd ¦||e o· a `\| Sc|ena DocJner¦. ard |¦ .||| de¦|re ¦|e
|r¦e·ra| s¦·Jc¦J·e o¦ yoJ· da¦ase¦ |enenbe· ¦|a¦ a da¦ase¦ |s ar |rneno·y ·ep·eser¦a¦|or o¦ ore o· no·e
¦ab|es ¦·on yoJ· da¦abase /DCN|T .||| Jse ¦||s sc|ena ¦||e .|er .o·||rg .|¦| yoJ· app||ca¦|or \|er
·Jrr|rg ¦|e app||ca¦|or. ¦|e Jse· .||| be ab|e ¦o add. de|e¦e. o· nod|¦y ·o.s |r ¦|e da¦ase¦ (|r ¦|e conpJ¦
e·'s neno·y) T|e c|arges .||| ·ena|r |r neno·y Jr¦|| ¦|e Jse· conn|¦s ¦|e c|arges bac| ¦o ¦|e da¦a
base. .||c| |r oJ· e.anp|e |s ¦|e Ca·T·ac|e·nd¦ ¦||e
C08622132.indd 149 10/18/05 8:48:11 PM
l50 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
)
|r ¦|e So|J¦|or |.p|o·e·. doJb|ec||c| ¦|e .sd ¦||e raned Car1rackerDataSet.xsd /s s|o.r |r ||gJ·e 81¯.
¦|e ·esJ|¦ o¦ ¦|e da¦ase¦ c·ea¦|or |s s|n||a· ¦o ¦|e da¦abase d|ag·an yoJ c·ea¦ed ea·||e· `oJ· d|ag·an
n|g|¦ be d|¦¦e·er¦ deperd|rg or yoJ· sc·eer ·eso|J¦|or ard |o. yoJ cJs¦on|.ed yoJ· |D|
T|e·e a·e sone ro¦ab|e d|¦¦e·erces. |o.eve· `oJ'|| see ¦|e sane co|Jnrs ¦|a¦ yoJ |ave c·ea¦ed |r yoJ·
p|ys|ca| da¦abase. bJ¦ |r ¦|e bo¦¦on o¦ eac| ¦ab|e. yoJ .||| see ne¦|ods ||||. Ce¦Da¦a() T|ese ne¦|ods a·e
pa·¦|cJ|a· ¦o ¦|e da¦ase¦. ard ¦|e /DCN|Tgere·a¦ed code by \|sJa| S¦Jd|o .||| Jse ¦|en ¦o da¦ab|rd da¦a
¦o yoJ· \|rdo.s |o·n cor¦·o|s¬cor¦·o|s ¦|a¦ do ro¦ e.|s¦ ye¦¦
*
|e¦J·r ¦o ¦|e Da¦a SoJ·ces .|rdo. ard e.pard ¦|e da¦ase¦ ¦ab|es `oJ'|| see ¦|e |rneno·y ·ep·e
ser¦a¦|or o¦ yoJ· ¦ab|es. ard yoJ'|| a|so see ¦|a¦ eac| co|Jnr |as a sna|| |cor ¦|a¦ g|ves yoJ |¦s ¦ype
T|ese |cors nay |oo| ¦an|||a· ¦o yoJ becaJse ¦|ey a·e s|n||a· ¦o ¦|e cor¦·o|s |r ¦|e ¦oo|bo. |e¦e· ¦o ||gJ·e
818 ¦o· a oJ|c| g|arce a¦ ¦|e Co|o· ard ||s¦|rg da¦ase¦ ¦ab|es ard ¦|e|· co|Jnr ¦ypes
+
C|ose ¦|e g·ap||ca| ·ep·eser¦a¦|or o¦ yoJ· da¦ase¦ by c||c||rg ¦|e X |r ¦|e co·re· o¦ ¦|e des|gre·
sJ·¦ace
, |r ¦|e So|J¦|or |.p|o·e·. doJb|ec||c| yoJ· forml.vb ¦||e ¦o oper ¦|e des|gre· sJ·¦ace ¦o· |o·n1
;^\jgZ-"&,
:eTc[\VT_eXceXfXagTg\babYg[X
6TeGeTV^XeWTgTfXg
;^\jgZ-"&-
I\XjbYg[X6b_beTaW?\fg\aZ
WTgTfXggTU_XfYeb`j\g[\a
g[X7TgTFbheVXfj\aWbj
C08622132.indd 150 10/18/05 8:48:13 PM
Chapter 8. Managing the Data l5l
$#
|r ¦|e Da¦a SoJ·ces .|rdo.. se|ec¦ ¦|e Lìstìng rode |r yoJ· da¦ase¦ ard c||c| or ¦|e d·opdo.r
a··o. po|r¦ ¦|a¦'s re.¦ ¦o ¦|e .o·d ?\fg\aZ `oJ .||| be p·eser¦ed .|¦| ¦.o c|o|ces Da¦aC·|d\|e. o·
De¦a||s Da¦aC·|d\|e. b·|rgs a|| o¦ ¦|e da¦ase¦ ¦|e|ds |r¦o a ¦ab|e o· g·|d ¦o·na¦ .|¦| nJ|¦|p|e ·o.s. .|||e
De¦a||s b·|rgs ¦|e da¦ase¦ ¦|e|ds |r ore ·o. a¦ a ¦|ne .|¦| a|| ¦|e|ds as |rd|v|dJa| cor¦·o|s |o· oJ· e.anp|e.
se|ec¦ DetaìIs
`oJ'|| a|so see ¦|a¦ eac| nenbe· o¦ ¦|e da¦ase¦ |as ¦|e sane d·opdo.r a··o.. .||c| a||o.s yoJ ¦o
c|arge .||c| cor¦·o|s .||| be d·opped or¦o ¦|e ¦o·n .|er |¦ |s d·agged /||o.|rg yoJ ¦o c|oose cor¦·o|s
p·|o· ¦o d·agg|rg ¦|e da¦ase¦ ¦ab|e or¦o ¦|e ¦o·n p·ever¦s yoJ ¦·on |av|rg ¦o |ay oJ¦ ¦|e || yoJ·se|¦ p|ece
by p|ece
$$
C|arge ¦|e Co|o·|D. \a|e|D. ard Ca·Type|D ¦o ¦|e Conbo|o. ¦ype by c||c||rg or ¦|e d·opdo.r
a··o. re.¦ ¦o eac| co|Jnr ard se|ec¦|rg Comboßox
$%
Se|ec¦ ¦|e Lìstìng rode by c||c||rg or |¦. ard ¦|er d·ag ard d·op |¦ or¦o ¦|e des|gre· sJ·¦ace or
|o·n1
$&
`oJ'|| ro. nod|¦y ¦|e ¦o·n s|.e |||e yoJ d|d |r p·ev|oJs c|ap¦e·s by nod|¦y|rg
¦|e ¦o·n's S|.e p·ope·¦y C|arge ¦|e ¦o·n s|.e so ¦|a¦ |¦s Wìdth |s 450 p|.e|s
ard |¦s Heìght |s 550 p|.e|s
$'
\ove a|| o¦ ¦|e cor¦·o|s so ¦|a¦ ¦|e ¦|·s¦ |abe| |s a|nos¦ |r ¦|e ¦op|e¦¦ co·re· |Js¦
berea¦| ¦|e ¦oo| s¦·|p See ||gJ·e 81º ¦o de¦e·n|re |o. ¦|e cor¦·o|s s|oJ|d
app·o.|na¦e|y be p|aced
;^\jgZ-"&.
EXf\mXW6TeGeTV^XeYbe`TYgXe`bi\aZ
T__bYg[XVbageb_f
You may need to scroII to see aII
of the controIs depending on your
screen resoIution.
J4EA< A:
C08622132.indd 151 10/18/05 8:48:14 PM
l52 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
/s yoJ car see. nary ¦||rgs |ave |Js¦ |appered |e¦'s s¦a·¦ by |oo||rg
a¦ ¦|e des|gre· sJ·¦ace /|| o¦ ¦|e ¦|e|ds ¦·on ¦|e da¦ase¦ |ave beer
added as cor¦·o|s. ard |abe|s .e·e a|so added based or ¦|e rane o¦
¦|e ¦|e|d |r ¦|e da¦ase¦ T||s ¦ea¦J·e |s ca||ed sna·¦ cap¦|or \|sJa|
S¦Jd|o Jses |asca| o· Cane| cas|rg as a nec|ar|sn ¦o |rse·¦ a space
|r |abe|s .|er Js|rg sna·¦ cap¦|ors \|er yoJ d·op ¦|e da¦ase¦ ¦|e|ds
or¦o ¦|e ¦o·n. sna·¦ cap¦|or |oo|s a¦ eac| ¦|e|d's cas|rg \|er |¦ ¦|rds
ar Jppe·case |e¦¦e· o· ar Jrde·sco·e c|a·ac¦e· (|e. _) ¦o||o.|rg a |o.
e·case |e¦¦e·. |¦ |rse·¦s o· ·ep|aces ¦|e _ .|¦| a space /r e.cep¦|or ¦o
¦||s ·J|e car be seer |r ¦|e ||CC|¦y ard ||C||g|.ay ¦|e|ds \|er yoJ
Jse Jppe·case |e¦¦e·s ¦o· ar ac·oryn. ¦o· e.anp|e. \|sJa| S¦Jd|o car
ro¦ d|s¦|rgJ|s| ¦|a¦ ¦|ese a·e ¦.o .o·ds ard ¦|e·e¦o·e doesr'¦ sp||¦
¦|en apa·¦ `oJ'|| |ave ¦o sp||¦ ¦|ese ¦.o ¦|e|ds narJa||y
`oJ .||| a|so ro¦|ce ¦|a¦ a ¦oo| s¦·|p |as beer added ¦|a¦ cor¦a|rs
a|nos¦ ¦|e sane bJ¦¦ors yoJ Jsed .|||e .o·||rg .|¦| ¦|e da¦abase
¦ab|e des|gre·
$(
|ead ¦|e b|Je |npo·¦ar¦ s|deba· ¦o ¦|e |e¦¦ \|¦| ¦||s copy
be|av|o· |r n|rd. | sJgges¦ ¦|a¦ yoJ Jse /pp·oac| +1 ever ¦|oJg|
yoJ'|| |ave ¦o pe·¦o·n sone narJa| s¦eps |¦ yoJ .ar¦ ¦o debJg yoJ·
app||ca¦|or ¦·on .|¦||r \|sJa| S¦Jd|o. |¦'s p·e¦e·ab|e ¦o Jse ¦||s so|J¦|or o·
yoJ .||| ro¦ be ab|e ¦o see ¦|e c|arges app||ed ¦o yoJ· da¦abase ¦||e T|e
da¦abase ¦||e .||| a|.ays cone bac| ¦o ¦|e |r|¦|a| ore ¦·on yoJ· p·o|ec¦.
.||c| |s s|n||a· ¦o ·ese¦¦|rg ¦|e .|o|e da¦abase ¦o .|a¦ |¦ |s |rs|de \|sJa|
S¦Jd|o
$)
Se|ec¦ ¦|e Car1racker.mdf da¦abase ¦||e |r ¦|e So|J¦|or |.p|o·e·
ard c|arge ¦|e Copy To CJ¦pJ¦ D|·ec¦o·y p·ope·¦y ¦o
Do Not Copy |r ¦|e |·ope·¦|es .|rdo.
$*
|·ess f5 ¦o bJ||d ard ·Jr yoJ· app||ca¦|or `oJ'|| ge¦ ar e.cep
¦|or nessage becaJse ¦|e ¦||e .or'¦ be cop|ed |r ¦|e b|r\debJg
d|·ec¦o·y /|so. or ¦|e ¦o·n |oad ever¦ .|er yoJ· code ¦·|es ¦o ¦||| ¦|e
da¦ase¦. |¦ .or'¦ ¦|rd ¦|e da¦abase a¦ ¦|e p|ace spec|¦|ed by ¦|e cor
rec¦|or s¦·|rg T|e·e¦o·e. yoJ ge¦ ar SO||.cep¦|or s¦a¦|rg ¦|a¦ |¦'s ro¦
ab|e ¦o a¦¦ac| ¦o ¦|e da¦abase C||c| ¦|e Stop Debuggìng bJ¦¦or o·
p·ess Shìft+f5 ¦o s¦op debJgg|rg
when working with IocaI database fiIes, it is necessary to understand that
they are treated Iike any other content fiIe. lor desktop projects, it means
that, by defauIt, the database fiIe wiII be copied to the output foIder {i.e.,
bin) each time the project is buiIt. After pressing l5, here's what it wouId
Iook Iike on disk.
CarTracker\CarTracker.mdf
CarTracker\lorml.vb
CarTracker\Bin\Debug\CarTracker.mdf
CarTracker\Bin\Debug\CarTracker.exe
At design time, CarTracker\CarTracker.mdf is used by the data tooIs and wiz-
ards. At run time, the appIication wiII use the database under the bin\debug
foIder. As a resuIt of the copy, many peopIe have the impression that the
appIication did not save the data to the database fiIe. This assumption occurs
because there are two copies of the data fiIe invoIved. This aIso happens
when Iooking at schema/data through the Database £xpIorer. The tooIs are
using the copy in the project foIder and not the fiIe in the bin\debug foIder.
The foIIowing are a few ways to work around this copy behavior.
l. lf you seIect your database fiIe in the SoIution £xpIorer window, you wiII
see a property caIIed Copy To Output Directory in the Properties window.
By defauIt, it is set to Copy AIways, which means that data fiIes in the
project foIder wiII be copied to the bin\debug foIder on each buiId, thus
overwriting the existing data fiIes if any. You can set this property to Do
Not Copy and then manuaIIy pIace a copy of the data fiIe in the bin\debug
foIder. ln this way, on subsequent buiIds, the project system wiII Ieave the
database fiIe in the bin\debug foIder and not try to overwrite it with the
one from the project. The downside to this method is that you wiII stiII
have two copies. Therefore, after you modify the database fiIe using the
appIication, if you want to make those same changes within the project,
you wiII need to copy the changes to the project manuaIIy and vice-versa.
2. You can Ieave the data fiIe outside the project and create a connection to
it in Database £xpIorer. when the lD£ asks you to bring the fiIe into the
project, simpIy say no. ln this way, both the design time and run time wiII
be using the same data fiIe. The downside to this method is that the path
in the connection string wiII be hard coded, and it wiII therefore be harder
to share the project and depIoy the appIication. Before depIoying the
appIication, make sure to repIace the fuII path in the settings with a reIa-
tive path. lf you want to read more about the reIative path versus the fuII
path {pIus a bit more about this copy behavior), read the foIIowing articIe.
]iie/$$Wad\h#bhYc#Xdb$hbVgiXa^ZciYViV$VgX]^kZ$'%%*$%-$'+$)*+--+#Vhem.
You'II see that l took portions of that articIe and modified them so that
they fit our appIication.
< @C BEG4AG
C08622132.indd 152 10/18/05 8:48:14 PM
Chapter 8. Managing the Data l53
$+
|s|rg \|rdo.s |.p|o·e·. go |r¦o yoJ· p·o|ec¦ d|·ec¦o·y (|¦ s|oJ|d be |oca¦ed a¦ \y DocJner¦s\\|sJa|
S¦Jd|o ´JJ'\|·o|ec¦s\Ca·T·ac|e·\Ca·T·ac|e·\) ard copy ¦|e .mdf ard .Idf ¦||es |r¦o ¦|e b|r\debJg
d|·ec¦o·y Jrde· Ca·T·ac|e·
$, |r \|sJa| |as|c |.p·ess. p·ess f5 ¦o bJ||d ard ·Jr yoJ· app||ca¦|or aga|r
`oJ s|oJ|d see ¦|e ¦.o ·eco·ds yoJ've narJa||y |rse·¦ed |r¦o ¦|e ||s¦|rg ¦ab|e `oJ
s|oJ|d be ab|e ¦o rav|ga¦e Js|rg ¦|e ¦oo| s¦·|p ard a|so nod|¦y. |rse·¦. ard de|e¦e a
·eco·d See ||gJ·e 8´J ¦o· a sraps|o¦ o¦ yoJ· Ca· T·ac|e· app||ca¦|or a¦ ·Jr ¦|ne
%# C|arge ¦|e ||| o¦ ¦|e ·o. a¦ pos|¦|or 1 ¦o erd .|¦| .net |rs¦ead .com
%$
/¦¦e· c|arg|rg ¦|e ||| ¦o· ¦|e ·eco·d. p·ess ¦|e dìsk ìcon ¦o conn|¦ ¦|e
c|arges ¦o ¦|e da¦abase
%%
C|ose ¦|e Ca· T·ac|e· app||ca¦|or ard ·es¦a·¦ |¦ by p·ess|rg f5 `oJ s|oJ|d ro.
see ¦|e ¦|·s¦ ·o. .|¦| ¦|e nod|¦|ed ||| erd|rg |r re¦ C|ose ¦|e app||ca¦|or aga|r
%&
To ve·|¦y ¦|a¦ yoJ a·e .o·||rg .|¦| a des|gr ¦|ne ard a ·Jr ¦|ne ve·s|or o¦ ¦|e
Ca·T·ac|e· da¦abase. oper ¦|e Lìstìng ¦ab|e ard se|ec¦ Show 1abIe Data ¦·on
.|¦||r ¦|e Da¦abase |.p|o·e· T|e ¦|·s¦ ·o. s|oJ|d cor¦a|r ar ||| co|Jnr erd|rg |r
con ard abg |r re¦
|o|r¦ p·over¦ T|e da¦abase ¦||e |r \|sJa| S¦Jd|o |s ro. decoJp|ed .|¦| ¦|e ore yoJ·
app||ca¦|or |s Js|rg a¦ ·Jr ¦|ne |ead ¦|e \o·e |r¦o ro¦e or ¦|e re.¦ page ¦o |ea·r
|o. ¦o na|e ¦|e da¦a ¦|e sane |r bo¦| ¦|e des|gr ¦|ne ard ·Jr ¦|ne
Component Tray
When you dragged Lhe LisLing daLaseL Lable onLo Lhe designer suríace, you probably saw
LhaL íour iLems were added in Lhe gray area below Lhe designer suríace. 1his secLion oí Lhe
designer suríace is called Lhe componenL Lray. 1his is Lhe secLion LhaL visual SLudio uses íor
nonvisual conLrols. ln your case, iL added an insLance oí Lhe Car1racker daLaseL, a LisLing
Lable adapLer, a LisLing binding source, and íinally a LisLing binding navigaLor.
LeL's describe Lhe oLhers individually.
N Binding Source ¥ou can Lhink oí a binding source as a "broker" or a layer oí indirecLion.
;^\jgZ-"'%
8kXVhg\babYg[X6TeGeTV^Xe
Tcc_\VTg\ba
C08622132.indd 153 10/18/05 8:48:15 PM
l54 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
lL can also be viewed as an inLermediary beLween a daLa-bound conLrol on your íorm
and a daLa source, such a a daLaseL. A binding source provides currency managemenL
and noLiíicaLions services (evenLs). 1he binding source has many meLhods Lo íaciliLaLe,
such as sorLing, íilLering, navigaLing, and ediLing oí daLa írom iLs daLa-bound conLrols Lo
Lhe daLa source. lL's also linked LighLly Lo Lhe nexL componenL. Lhe binding navigaLor.
When you see a binding navigaLor, you're assured oí geLLing a binding source.
N Binding Navigator 1he binding navigaLor is a means Lo enable navigaLion and daLa
manipulaLion. lL has a Ul componenL or, more speciíically, a Lool sLrip wiLh buLLons Lo
íaciliLaLe Lhe íuncLionaliLies provided by Lhe binding source.
N Typed Dataset AlLhough you know whaL a daLaseL is, you may noL know LhaL iL's a
sLrongly Lyped objecL. lL conLains daLa Lables oí Lhe DaLa1able Lype LhaL consLiLuLe Lhe in-
memory represenLaLion oí your daLabase Lables. 1hese daLa Lables also have a special
daLa adapLer called Lhe Lable adapLer. 1here is a Lable adapLer íor each daLa Lable.
N TabIe Adapter A Lable adapLer is a daLa access objecL. lL connecLs Lo Lhe daLabase (e.g.,
SOL Server 2005 Fxpress FdiLion), execuLes Lhe queries, and íills a daLa Lable wiLh daLa
when iL reLurns írom SOL Server. 1hereíore, iL's Lhe cenLral poinL íor all daLa access on an
individual Lable. 1here is one Lable adapLer per Lable in your daLa source. A Lable adapLer
can have more Lhan one SFLFC1 query.
How Do l Get More MeaningfuI lnformation on My lorm?
LeL's reLurn Lo our Car1racker projecL. As you can see, Lhe ColorlD, MakelD, and Car1ypelD
combo boxes are Lhere, buL Lhey are displaying Lhe lD and noL Lhe name associaLed wiLh Lhe
lD. 1his is noL helpíul íor Lhe user because an lD doesn'L have any meaning Lo Lhe user and
he/she mighL noL be able Lo easily add or modiíy rows wiLhouL having a human-readable
íormaL íor Lhose columns. ConsequenLly, you need Lo make sure Lhe daLa is displayed in a
humanly readable way and LhaL Lhe lD is sLored in Lhe row whenever Lhe user modiíies Lhe
iníormaLion.
1here's an easy way Lo accomplish Lhis, which you will do now íor your Lhree combo
boxes.
lf you want the same data in
VisuaI Studio as you have when
executing the appIication in
debug mode, then you must cIose
your project compIeteIy. Using
windows £xpIorer, copy the .mdf
and .Idf fiIes from the bin\debug
foIder to the project foIder. when
you reopen your project, the data-
base wiII now contain the same
content.
Suppose you then want to
modify the structure of your data-
base, such as adding a coIumn to a
tabIe. lf you don't want to Iose the
data within the bin\debug data-
base fiIes, you must copy them
back to the project foIder before
you modify the tabIe structure.
when done with the modifica-
tions, you simpIy copy both the
.mdf and .Idf fiIes back to the
bin\debug foIder. Of course, if
your appIication needs those new
database changes, you wouId aIso
have to modify the dataset, but
that process is beyond the scope
of this book.
@BE8 < A9 B
C08622132.indd 154 10/18/05 8:48:16 PM
Chapter 8. Managing the Data l55
TO DATABlND wlTH DOMAlN TABL£S
$
|r ¦|e Da¦a SoJ·ces .|rdo.. se|ec¦ ¦|e CoIor ¦ab|e ¦·on ¦|e da¦ase¦. d·ag |¦ or¦o ¦|e ¦o·n's des|gre·
sJ·¦ace ove· ¦|e Co|o·|D conbo bo.. ard d·op |¦
`oJ'|| see ¦|a¦ aro¦|e· ¦ab|e adap¦e· (CoIor1abIeAdapter) ard aro¦|e· b|rd|rg soJ·ce
(CoIorßìndìngSource) .e·e added ¦o ¦|e conporer¦ ¦·ay |¦ yoJ go ¦o ¦|e Co|o·|D conbo bo. ard c||c|
or ¦|e sna·¦ ¦ag ¦·|arg|e. yoJ'|| see ¦|e Da¦a ||rd|rg \ode |r¦o·na¦|or bo. appea·. as s|o.r |r ||gJ·e 8
´1 `oJ'|| ro¦|ce ¦|a¦ yoJ· d·agardd·op ac¦|or boJrd ¦|e conbo bo. cor¦·o| .|¦| ¦|e
Co|o·||rd|rgSoJ·ce |ecaJse o¦ ¦||s ac¦|or. .|ereve· ¦|e conbo bo. d|sp|ays. |¦ .||| d|sp|ay ¦|e co|o·
ranes |rs¦ead o¦ Co|o·|D \|er ¦|e Jse· p|c|s a co|o· ¦·on ¦|e conbo bo.. ¦|e assoc|a¦ed va|Je nenbe·
¦|a¦ .||| be Jsed |r ¦|e ·o. .||| s¦||| be ¦|e Co|o·|D. spec|¦|ca||y. ¦|e Co|o·|D assoc|a¦ed .|¦| ¦|e Co|o·Nane
\orde·¦J|. |sr'¦ |¦¯ /rd ro ||res o¦ code .e·e Jsed
%
|epea¦ ¦|e sane p·ocess ¦o· ¦|e \a|e ard Ca·Type da¦ase¦ ¦ab|es ard ¦|e co··espord|rg \a|e|D
ard Ca·Type|D conbo bo.es
&
|J||d ard ·Jr yoJ· app||ca¦|or ard ¦|er |oo| a¦ eac| conbo bo. `oJ ro. |ave ·ea| co|o· ranes ard
ro¦ ne·e|y Co|o·|Ds. ¦|e sane |s ¦·Je ¦o· Ca·Type ard \a|e T|e conbo bo.es a·e a|so popJ|a¦ed
.|¦| a|| o¦ ¦|e va|Jes con|rg ¦·on ¦|ose ¦ab|es ard ro¦ s|np|y ¦|e va|Je ¦o· ¦|a¦ spec|¦|c ·o. C||c| or ¦|e
d·opdo.r a··o. ard yoJ'|| see a|| o¦|e· po¦er¦|a| va|Jes C|ose ¦|e app||ca¦|or
'
Cr ¦|e ¦o·n. ·enove ¦|e '|D' pa·¦ ¦·on ¦|e CoIorID. MakeID. ard Car1ypeID |abe|s
(
`oJ .||| ro. er|a·ge ¦|e No¦es ¦|e|d by na||rg |¦ a nJ|¦|||re ¦e.¦ bo. Se|ec¦ ¦|e Notes ¦e.¦ bo. ard
c|arge ¦|e \J|¦|||re p·ope·¦y ¦o true /|so c|arge ¦|e MaxLength ¦o 250. ¦|e Sìze:Heìght ¦o 50.
ard ¦|e Sìze:Wìdth ¦o 250
)

De|e¦e ¦|e LìstìngID ¦e.¦ bo. ard |¦s |abe|
;^\jgZ-"'&
6b_be<7Vb`UbUbkf`TeggTZ\aYbe
`Tg\baf[bj\aZ7TgT5\aW\aZ@bWX
This inteIIigent databinding is a
new VisuaI Studio feature caIIed
Smart DefauIts. Smart DefauIts
Iooks in the dataset tabIe to see
whether there's a coIumn of type
string by either the lD or the pri-
mary key. lf so, it tries to use this
one for the data binding.
@BE8 < A9 B
C08622132.indd 155 10/18/05 8:48:17 PM
l56 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
*
S|.e ard ·epos|¦|or ¦|e cor¦·o|s or ¦|e ¦o·n so ¦|a¦ |¦ ·esenb|es ¦|e ¦o·n s|o.r |r ||gJ·e 8´´. |¦
does ro¦ reed ¦o be ar e.ac¦ dJp||ca¦e |¦ .||| be good p·ac¦|ce ¦o b·|rg bac| || des|gr corcep¦s ¦·on
C|ap¦e· ' ard a|so good p·epa·a¦|or ¦o· C|ap¦e· º C|arge ¦|e Te.¦ p·ope·¦y o¦ ¦|e ¦o·n ¦o Car 1racker
;^\jgZ-"''
AXji\fhT_TfcXVgfbYg[X6TeGeTV^Xe
Tcc_\VTg\ba
+

|r ¦|e So|J¦|or |.p|o·e·. ·erane ¦o·n1vb ¦o Maìn.vb
, Se|ec¦ ¦|e ¦o·n ard c|arge ¦|e |ac|Co|o· p·ope·¦y ¦o HìghIìght
|ve·y¦||rg |s rea·|y conp|e¦e ¦o· ¦||s app||ca¦|or. bJ¦ ¦|e ·esea·c| capab|||¦|es a·e |ac||rg CJ··er¦|y. ¦|e
or|y .ay ¦o sea·c| |s ¦o scar ¦|·oJg| a|| o¦ ¦|e ·o.s Jr¦|| yoJ ¦|rd ¦|e co··ec¦ ore T||s |s ro¦ d|¦¦|cJ|¦ ro.
becaJse yoJ |ave or|y ¦.o ·o.s |r yoJ· Ca· T·ac|e· da¦abase `e¦. |¦ yoJ |ad 'JJ ·o.s. ¦|e Scar ne¦|od
.oJ|d ro¦ be e¦¦ec¦|ve a¦ a||¦ T|e·e¦o·e. yoJ'|| |np|ener¦ sea·c| capab|||¦|es by add|rg oJe·|es ¦o yoJ·
app||ca¦|or by Js|rg ¦|e Da¦ase¦ Des|gre·
$#
|r ¦|e Da¦a SoJ·ces .|rdo.. se|ec¦ Car1rackerDataSet ||g|¦c||c| ard se|ec¦ fdìt DataSet wìth
Desìgner
$$
Se|ec¦ ¦|e Lìstìng da¦a ¦ab|e. ard ¦|er se|ec¦ ¦|e Lìstìng1abIeAdapter sec¦|or a¦ ¦|e bo¦¦on o¦ ¦|e
da¦a ¦ab|e
C08622132.indd 156 10/18/05 8:48:18 PM
Chapter 8. Managing the Data l57
\|er yoJ |oo| |r ¦|e |·ope·¦|es .|rdo.. yoJ'|| see ¦|a¦ ¦oJ· ¦ypes o¦ oJe·|es .e·e aJ¦ona¦|ca||y gere·a¦ed
by \|sJa| S¦Jd|o S|||CT. |NS||T. D|||T|. ard ||D/T| T|ey a·e ¦|e oJe·|es ¦|a¦ |e|ped yoJ |ave a ¦J||y
.o·|ab|e app||ca¦|or .|¦|oJ¦ .·|¦|rg a s|rg|e ||re o¦ code \|er yoJ ·ead aboJ¦ ¦ab|e adap¦e·s ea·||e·. yoJ
|ea·red ¦|a¦ yoJ car |ave nJ|¦|p|e oJe·|es .|¦| a ¦ab|e adap¦e· becaJse |¦ |s ¦|e cer¦·a| po|r¦ o¦ da¦a
access `oJ .||| ¦|Js add sea·c| capab|||¦|es ¦o yoJ· app||ca¦|or by add|rg oJe·|es ¦o ¦|e ¦ab|e adap¦e·s ard
by Js|rg pa·ane¦e·s ¦·on ¦|e || ||·s¦. yoJ .||| add ¦|e ab|||¦y ¦o sea·c| ¦o· ||s¦|rgs ¦|a¦ |ave a ce·¦a|r co|o·
$%

||g|¦c||c| ¦|e Lìstìng1abIeAdapter sec¦|or ard se|ec¦ Add Ouery. . . as s|o.r |r ||gJ·e 8´`
T||s .||| b·|rg yoJ ¦o ¦|e Tab|e/dap¦e· OJe·y Cor¦|gJ·a¦|or \|.a·d T||s .|.a·d .||| |e|p yoJ add aro¦|e·
S|||CT oJe·y ¦|a¦ .||| Jse pa·ane¦e·s ¦o ·e¦|re yoJ· sea·c| `oJ car a|so c·ea¦e a S|||CT oJe·y ard ¦J·r |¦
|r¦o a s¦o·ed p·ocedJ·e o· Jse ar e.|s¦|rg s¦o·ed p·ocedJ·e /s |¦s rane |np||es. a s¦o·ed p·ocedJ·e |s ore
¦|a¦ |s s¦o·ed |r SO| Se·ve· ard cor¦a|rs SO| s¦a¦ener¦s. a|org .|¦| o¦|e· p·og·ann|rg cors¦·Jc¦s. ¦|a¦
Jse a |argJage ca||ed TSO| o· T·arsac¦SO| / re. ¦ea¦J·e ¦o SO| Se·ve· ´JJ' |.p·ess |d|¦|or |s ¦|a¦
s¦o·ed p·ocedJ·es car a|so be coded |r naraged |argJages. sJc| as C+ ard \|sJa| |as|c S¦o·ed p·oce
dJ·es a·e e.ecJ¦ed or ¦|e se·ve· S|rce yoJ'·e Js|rg SO| Se·ve· ´JJ' |.p·ess |d|¦|or. ¦||s .||| be o¦ ro
corce·r becaJse ¦|e SO| Se·ve· ard ¦|e app||ca¦|or a·e e.ecJ¦ed or ¦|e sane nac||re
$&
Se|ec¦ Use SOL statements ard c||c| Next \|er as|ed .||c|
¦ype o¦ SO| oJe·y yoJ .ar¦ ¦o Jse. c|oose SfLfC1 whìch
return rows ard ¦|er c||c| Next No¦e ¦|a¦ yoJ coJ|d |ave added
ary SO| oJe·y ¦ype yoJ .ar¦ed
$'
`oJ a·e ro. p·eser¦ed .|¦| ar ed|¦ .|rdo. |r .||c| ¦o add
yoJ· SO| s¦a¦ener¦ ¦|a¦ .||| pe·¦o·n a sea·c| ¦o· a|| o¦ ¦|e ||s¦
|rgs ¦|a¦ cor¦a|r a pa·¦|cJ|a· co|o· |e¦e· ¦o ||gJ·e 8´4 ¦o see ¦|e
SO| connard ed|¦ .|rdo. C||c| or ¦|e Ouery ßuìIder. . . bJ¦¦or ¦o
ge¦ a v|sJa| v|e. o¦ ¦|e oJe·y
$(
`oJ .||| ro. add ¦|e Co|o· ¦ab|e ¦o ¦|e d|ag·an so ¦|a¦ yoJ'||
be ab|e ¦o base yoJ· sea·c| or a pa·¦|cJ|a· co|o· To add ¦|e
Co|o· ¦ab|e. s|np|y ·|g|¦c||c| |r ¦|e d|ag·an a·ea ard se|ec¦ Add
1abIe. . . T|e /dd Tab|e d|a|og bo. appea·s as s|o.r |r ||gJ·e 8´'
Se|ec¦ ¦|e CoIor ¦ab|e ard c||c| ¦|e Add bJ¦¦or \|er ¦|e Co|o·
¦ab|e |as beer added ¦o ¦|e d|ag·an. c||c| ¦|e CIose bJ¦¦or
;^\jgZ-"'(
4WW\aZaXjdhXe\XfgbTgTU_XTWTcgXe
;^\jgZ-"')
FD?Vb``TaWXW\gj\aWbjeXTWlgb
Vhfgb`\mXg[XhfXeÆffXTeV[
C08622132.indd 157 10/18/05 8:48:19 PM
l58 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
$)
|r ¦|e SO| code pare o¦ OJe·y |J||de·. apperd ¦|e ¦o||o.|rg SO| code ¦|a¦ .||| |e|p |r ¦|e ¦||¦e·|rg
p·ocess
WhLRL 1Co]o¡.Co]o¡Name LlkL `x` + 0co]o¡name + `x`}
$*
|e¦o·e yoJ p·oceed .|¦| yoJ· re. oJe·y. na|e sJ·e |¦ .|||
g|ve yoJ ¦|e ·esJ|¦s yoJ'·e e.pec¦|rg C||c| ¦|e fxecute
Ouery bJ¦¦or ¦o d|sp|ay ¦|e OJe·y |a·ane¦e·s d|a|og bo.. as
s|o.r |r ||gJ·e 8´b
$+
T·y ·ep|ac|rg ¦|e .o·d AH?? .|¦| bIue ard ¦|er c||c| OK
T|e |esJ|¦s pare o¦ OJe·y |J||de· s|oJ|d d|sp|ay or|y ore
·o. |s|rg ¦|e .o·d bIack s|oJ|d ·e¦J·r ¦|e b|ac| ca· ·o.
S|np|y er¦e· ¦|e |e¦¦e· b. ard yoJ s|oJ|d ge¦ bo¦| ¦|e b|Je ard
¦|e b|ac| ·o.s Crce yoJ'·e sa¦|s¦|ed .|¦| yoJ· oJe·y. c||c| OK |r
OJe·y |J||de·
;^\jgZ-"'*
G[X4WWGTU_XW\T_bZUbk
The ´%' symboI is the wiIdcard in
SOL and it can mean anything. lor
exampIe, in the previous wH£k£
cIause, it means return something
that has a coIor simiIar to the coI-
orname parameter.
@BE8 < A9 B
;^\jgZ-"'+
DhXelCTeT`XgXefW\T_bZUbkj\g[ceb`cggbXagXeTVb_beaT`XiT_hX
C08622132.indd 158 10/18/05 8:48:20 PM
Chapter 8. Managing the Data l59
$,
Cr ¦|e Spec|¦y a SO| S|||CT S¦a¦ener¦ sc·eer o¦ ¦|e .|.a·d.
c||c| Next |¦'s ¦|ne ¦o add yoJ· oJe·y ¦o ¦|e app||ca¦|or
%#
/ sc·eer appea·s ¦|a¦ .||| p·onp¦ yoJ ¦o rane ¦|e ne¦|ods
¦|a¦ yoJ· oJe·y .||| gere·a¦e T|ose ne¦|ods .||| be ava||ab|e
a¦¦e· ¦||s c·ea¦|or ¦·on ¦|e ||s¦|rg ¦ab|e adap¦e· |e¦e· ¦o ||gJ·e 8´¯
¦o v|e. ¦||s sc·eer cor¦a|r|rg ¦|e ¦.o re. ne¦|od ranes |o· bo¦|
ranes yoJ bas|ca||y reed ¦o add .|a¦ yoJ· ¦||¦e· |s |r yoJ· case. yoJ
car add CoIorName s|rce yoJ ¦||¦e·ed by ¦|a¦ rane |r yoJ· \||||
c|aJse \|er dore. c||c| Next
%$
/¦¦e· p·ocess|rg ¦o· a ¦e. secords. ¦|e conpJ¦e· s|oJ|d cone
bac| .|¦| a ·esJ|¦s sc·eer |r¦o·n|rg yoJ ¦|a¦ yoJ· S|||CT s¦a¦e
ner¦ ard yoJ· re. |||| ard Ce¦ ne¦|ods a·e ·eady ¦o Jse C||c| ¦|e
fìnìsh bJ¦¦or
|oo| a¦ ¦|e ¦ab|e adap¦e· sec¦|or o¦ ¦|e ||s¦|rg da¦a ¦ab|e `oJ· re.
ne¦|ods .||| be added ¦|e·e
%%
|epea¦ s¦eps s¦a·¦|rg a¦ s¦ep 1´ ard add aro¦|e· oJe·y ¦o ¦|e
||s¦|rgTab|e/dap¦e· ¦o· ¦|e Ca·Type ¦ab|e |se ¦|e ¦o||o.|rg \|||| c|aJse
WhLRL 1Ca¡1ype.Ca¡1ypeName LlkL `x` + 0ca¡fypename + `x`}
Nane ¦|e ¦||| ard ge¦ da¦a ne¦|ods fìIIßyCar1ypeName ard CetDataßyCar1ypeName
%&
|epea¦ s¦eps s¦a·¦|rg a¦ s¦ep 1´ ard add aro¦|e· oJe·y ¦o ¦|e ||s¦|rgTab|e/dap¦e· ¦o· ¦|e \a|e ¦ab|e
|se ¦|e ¦o||o.|rg \|||| c|aJse
WhLRL 1Make.MakeName LlkL `x` + 0makename + `x`}
Nane ¦|e ¦||| ard ge¦ da¦a ne¦|ods fìIIßyMakeName ard CetDataßyMakeName
`oJ'·e a|nos¦ dore ro.. bJ¦ yoJ s¦||| |ave ¦o b|rd ¦|ose re. oJe·|es ¦o cor¦·o|s or yoJ· ¦o·n Cr ary
da¦aboJrd cor¦·o| or yoJ· ¦o·n. yoJ car se|ec¦ Add Ouery. ¦·on ¦|e sna·¦ ¦ag nerJ |r yoJ· case. yoJ
.ar¦ ¦o add sea·c| capab|||¦|es ¦o ¦|e .|o|e ¦o·n ard ro¦ s|np|y ¦o a pa·¦|cJ|a· cor¦·o|
%'
Co ¦o ¦|e conporer¦ ¦·ay. c||c| ¦|e Lìstìng1abIeAdapter sna·¦ ¦ag. ard se|ec¦ Add Ouery. . . `oJ'||
see a Sea·c| C·|¦e·|a |J||de· d|a|og bo. ¦|a¦ .||| p·onp¦ yoJ ¦o c·ea¦e a re. oJe·y o· p|c| ar e.|s¦|rg
ore S|rce yoJ |Js¦ bJ||¦ ¦|·ee re. se¦s o¦ ne¦|ods. yoJ ne·e|y reed ¦o se|ec¦ oreSe|ec¦ ¦|e fxìstìng
Ouery Name op¦|or ard ¦|er se|ec¦ fìIIßyCoIorName as s|o.r |r ||gJ·e 8´8 or ¦|e re.¦ page
;^\jgZ-"',
7\T_bZUbkj\g[ceb`cggbeXaT`X
g[X`Xg[bWfXhfXWgb\aVeXTfX
fXTeV[VTcTU\_\g\Xf
C08622132.indd 159 10/18/05 8:48:21 PM
l60 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
%(
C||c| ¦|e OK bJ¦¦or `oJ'|| see ¦|a¦ a ¦oo| s¦·|p |as beer p|aced a¦ ¦|e ¦op o¦
¦|e ¦o·n .|¦| a sea·c| bJ¦¦or ¦|a¦ .||| ca|| yoJ· ne¦|od .|er yoJ c||c| |¦.
¦|e·eby g|v|rg yoJ a .ay o¦ sea·c||rg by ce·¦a|r c·|¦e·|a T||s .as acconp||s|ed by
or|y ¦yp|rg |r ¦|e ¦|·ee \|||| c|aJses ¦o· yoJ· spec|¦|c oJe·|es
%)
|epea¦ s¦ep ´4 ard add ¦|e |||||yCa·TypeNane ard |||||y\a|eNane oJe·|es.
.||c| .||| add ¦.o no·e ¦oo| s¦·|ps
%*
No. add a ¦oo| s¦·|p cor¦a|re· ¦o ¦|e ¦o·n |||e yoJ d|d |r C|ap¦e· b Se¦ ¦|e
Dock p·ope·¦y ¦o ¦||| ¦|e ¦o·n |r ¦|e sna·¦ ¦ag nerJ. se|ec¦ Re-Parent
ControIs ¦o p|ace a|| o¦ yoJ· ¦oo| s¦·|ps or ¦|e ¦op pare| ard a|| o¦ yoJ· o¦|e· cor
¦·o|s |r yoJ· cor¦er¦ pare| |¦ recessa·y. Jse ¦|e DocJner¦ CJ¦||re .|rdo. ¦o v|e.
ard ad|Js¦ ¦|e ||e·a·c|y o¦ ob|ec¦s or ¦|e ¦o·n
%+
\|er yoJ'·e ¦|·oJg| nov|rg cor¦·o|s. e.¦erd ¦|e ¦op pare| by c||c||rg ¦|e
g·|p ard pJ|||rg |¦ do.r so ¦|a¦ |¦ becones ¦.o ¦oo| s¦·|ps .|de
%,
\a|e sJ·e yoJ· app||ca¦|or |oo|s |||e ¦|e ore s|o.r |r ||gJ·e 8´º |·ess f5
¦o see ¦|e ·esJ|¦s o¦ yoJ· .o·| Type 'b|Je' |r ¦|e co|o·rane ¦oo| s¦·|p ard
c||c| |||||yCo|o·Nane ¦o see |¦ |¦ ·e¦J·rs b|Je co|o· ca· ||s¦|rgs |.pe·|ner¦ .|¦| ¦|e
o¦|e· ¦ea¦J·es o¦ ¦|e app||ca¦|or
;^\jgZ-"'.
9\aT_6TeGeTV^XeTcc_\VTg\bafVeXXa
;^\jgZ-"'-
FXTeV[6e\gXe\T5h\_WXej\g[g[X
9\__5l6b_beAT`X`Xg[bWfX_XVgXW
C08622132.indd 160 10/18/05 8:48:23 PM
Chapter 8. Managing the Data l6l
1his was a simple applicaLion LhaL you can probably modiíy Lo handle more iníormaLion,
such as car picLures. 8uL Lhere is noLhing LhaL you can'L add by yourselí now! Here's a lisL oí
oLher Lhings you can do ií you wanL Lo conLinue Lo work on Lhis applicaLion.
ln Summary.
1haL was a big chapLer wiLh a loL oí maLerial! LeL's review whaL you've learned. ¥ou were íirsL
inLroduced Lo daLabases and daLabase concepLs. ¥ou learned whaL consLiLuLed a daLabase
and whaL you usually íind wiLhin a daLabase. ¥ou learned abouL daLa inLegriLy and how iL is
relaLed Lo Lhe primary key and Lhe íoreign key.
¥ou Lhen used visual 8asic 2005 Fxpress FdiLion Lo creaLe a daLabase and Lables and
Lhen populaLed Lhem wiLh some iniLial daLa using various Lools in visual 8asic 2005 Fxpress
FdiLion. ¥ou implemenLed all oí Lhe íoreign key relaLionships wiLhouL leaving visual SLudio
and validaLed Lhem as well.
AíLer enLering your daLa manually, you learned Lo allow a user Lo enLer daLa more easily
by developing a sample Car 1racker applicaLion LhaL uses ADO.NF1 and daLabinding.
LasLly, you learned abouL Lhe new componenLs oí ADO.NF1 2.0 and how, wiLh liLLle or no
code, you can develop a íully working daLa-cenLric applicaLion. ¥ou've only been inLroduced
Lo a brieí parL oí ADO.NF1, íor iL's a vasL subjecL. lí you wanL Lo learn more, Lry looking aL
some code or samples on MSDN. A good place Lo begin is Lhe í0í samples íor visual SLudio
2005. Pay parLicular aLLenLion Lo Lhe daLa access and Windows Forms samples. Here's Lhe
link. httµ.//msJn.micrcscft.ccm/vbcsic/JcwnlccJs/200S/ccJe/101scmµles/.
ln Lhe nexL chapLer, you will develop Lhe íinal applicaLion oí Lhis book-Lhe WeaLher
1racker applicaLion. ¥ou'll learn new concepLs such as deploymenL, consuming Web services,
user seLLings, and much more in a compleLe applicaLion wiLh all oí Lhe necessary validaLions.
Q Add validaLions íor user inpuL, such as
making sure Lhe year oí Lhe car is noL
greaLer Lhan Lhe currenL year - 2
Q Add picLures in Lhe daLabases and on Lhe
íorm
Q Add a sold check mark
Q Add Lhree íorms Lo add daLa in Lhe
domain Lables (Car1ype, Make, Color)
Q Add more iníormaLion in Lhe lisLing, such
as conLacL iníormaLion
Q Make Lhe URL clickable
Q Save an ad as a LexL íile
C08622132.indd 161 10/18/05 8:48:23 PM
C08622132.indd 162 10/18/05 8:48:23 PM
l63
6[TcgXe,
5h\_WLbheBja
JXTg[XeGeTV^Xe
4cc_\VTg\baAbj
.
¥ou have now reached Lhe lasL chapLer oí Lhe book and have learned quiLe
a íew new concepLs along Lhe way. ln Lhis chapLer, you'll doL Lhe i's and cross
Lhe L's by developing a íull applicaLion. ¥ou will be working wiLh new pro-
cesses in Lhis chapLer, buL you will also need Lo draw on whaL you've learned
in previous chapLers Lo creaLe Lhe íinal producL. ¥ou've been developing
oLher applicaLions LhroughouL Lhe book, buL you may noL have períormed
all oí Lhe validaLions based on Lhe sLeps or on my recommendaLion. ¥ou will
puL iL all LogeLher wiLh Lhis applicaLion.
9XTgheXfbYg[XJXTg[Xe
GeTV^Xe4cc_\VTg\ba$)'
6eXTg\aZg[X4cc_\VTg\ba
HfXe<agXeYTVX$)(
Hf\aZJXUFXei\VXf$**
4aWAbj=hfg
6_\V^BaVX$,+
This chapter uses a United States weather web service. l
expect that this web service wiII be avaiIabIe and func-
tion as described in this chapter. However, since this
web service is on the lnternet, it is possibIe that this
web service might change or might not be avaiIabIe. lf
you don't have an lnternet connection or if the weather
web service is not avaiIabIe, the appIication wiII Iet you
know by dispIaying a message. lf there are any changes
or corrections for this chapter {or in the book), they wiII
be coIIected and added to a Microsoft KnowIedge Base
articIe. To view the Iist of known corrections for this
book, visit the foIIowing articIe.
]iie/$$hjeedgi#b^Xgdhd[i#Xdb$`W$.%*%()$
< @C BEG4AG
C09622132.indd 163 10/19/05 10:41:54 AM
l64 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
leatures of the weather Tracker AppIication
ln Lhis secLion, you'll become acquainLed wiLh Lhe íeaLures used Lo creaLe version í.0 oí Lhe
WeaLher 1racker applicaLion. 1his applicaLion conLains Lhe íollowing íeaLures in version í.0.
N SLarLs and resides as an icon in Lhe noLiíicaLion area
N Coníigures opLional user seLLings írom Lhe noLiíicaLion area icon in Lhe conLexL menu
N Reíreshes all weaLher daLa on demand írom Lhe conLexL menu in Lhe noLiíicaLion area
N Uses a Web service íor Lhe daLa (weaLher)
N SLores and persisLs user seLLings using XML
N 1he applicaLion will minimize and noL close when Lhe user clicks Lhe Close buLLon in Lhe LiLle
bar. 1he applicaLion will only close when Lhe user selecLs FxiL on Lhe conLexL menu.
N ConLains a splash screen on sLarLup
N ConLains an AbouL box írom Lhe conLexL menu
N Displays Lhe currenL LemperaLure in Lhe sysLem Lray using Lhe weaLher icon as íollows. a Lem-
peraLure above í00 degrees is displayed in red, any negaLive LemperaLure is displayed in blue,
normal LemperaLure is displayed in black
1he applicaLion will noL conLain Lhe íollowing íeaLures in version í.0.
N Will noL work íor more Lhan one ciLy aL a Lime
N No graphical gauge conLrols íor wind, pressure, LemperaLure, and so íorLh
N Conversions beLween meLric uniLs and Fnglish uniLs
The High-LeveI PIan
For Lhe WeaLher 1racker applicaLion, Lhe user will brieíly see Lhe splash screen and Lhen iL will
go direcLly Lo Lhe noLiíicaLion area in Lhe Windows 1askbar and display Lhe currenL LemperaLure.
lí Lhe LemperaLure is above í00 degrees F, Lhe LemperaLure will be displayed in red sLarLing
aL 00. lí Lhe LemperaLure is below 0 degrees F (negaLive degrees), Lhe LemperaLure will be
C09622132.indd 164 10/19/05 10:41:55 AM
Chapter 9. BuiId Your Own weather Tracker AppIication Now! l65
displayed in blue. OLherwise, Lhe LemperaLure will be displayed in black. lí a problem occurs,
a red NA will show up in place oí Lhe LemperaLure reading.
lí Lhe user righL-clicks Lhe icon in Lhe noLiíicaLion area, iL will open a conLexL menu wiLh
choices Lo open Lhe Main íorm and reLrieve Lhe currenL weaLher. 1he currenL weaLher will
have an icon and provide all oí Lhe weaLher daLa LhaL Lhe Web service can provide.
lí Lhe user selecLs Reíresh WeaLher lnío írom Lhe conLexL menu írom Lhe noLiíicaLion area
icon, iL will auLomaLically Lrigger a call Lo Lhe weaLher Web service Lo updaLe boLh Lypes oí
weaLher daLa. 1his will be done asynchronously and will sLarL by updaLing Lhe currenL weaLher.
lí Lhe user selecLs OpLions írom Lhe conLexL menu, an OpLions dialog box will be displayed.
1he user will be able Lo change Lhe ZlP code where weaLher daLa should be reLrieved. lí Lhe
user selecLs AbouL írom Lhe conLexL menu, iL will display an AbouL dialog box.
ln Lhis chapLer, l will use a diííerenL approach Lhan used in previous chapLers. As long as
you are using Lhe same componenLs LhaL l speciíy, you may personalize your applicaLion as
íar as size, color, and oLher aLLribuLes are concerned. l'll also presenL my soluLion as well aL
diííerenL sLeps along Lhe developmenL, Lhereíore, ií you like whaL you're seeing, you may
proceed wiLh your applicaLion írom Lhe companion conLenL provided. l will also posL a greaL
deal oí code and explain Lhe secLions LhaL are linked Lo Lhe íeaLures described above.
1o produce Lhe applicaLion in Lhis chapLer, an incremenLal approach will be used in which
you will implemenL one íeaLure, inLegraLe iL wiLh Lhe resL oí Lhe applicaLion, and Lhen LesL iL.
¥ou will Lhen move Lo Lhe nexL íeaLure unLil compleLion.
Creating the AppIication User lnterface
1he Main íorm user inLeríace (Ul) will conLain all oí Lhe weaLher iníormaLion LhaL you'll dis-
play Lo Lhe user. Reíer Lo Figure 9-í Lo see whaL Lhe Main íorm will look like when íinished.
As you learned in Lhe lasL chapLer, you will use daLa binding Lo bind Lhe conLrols Lo Lhe
weaLher daLa. ¥ou'll recall LhaL when creaLing Lhe daLa source, you had a choice oí DaLabase,
Web Service, and ObjecLs. ln Lhis applicaLion, you will use a Web service as a daLa source,
and Lhe íields you will display on Lhe íorm will be daLa-bound Lo Lhe Web service daLaseL.
C09622132.indd 165 10/19/05 10:41:55 AM
l66 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
TO Ck£AT£ A DATA SOUkC£ lOk A MAlN lOkM CONTkOL
$
S¦a·¦ \|sJa| |as|c ´JJ' |.p·ess |d|¦|or ard c·ea¦e a re. \|rdo.s /pp||ca¦|or p·o|ec¦ Nane ¦|e
app||ca¦|or KYUh\YfHfUW_Yf
%

|r ¦|e So|J¦|or |.p|o·e·. ·erane |o·n1vb ¦o AU]b"jV
&

|s|rg ¦|e |·ope·¦|es .|rdo.. c|arge ¦|e p·ope·¦|es ¦o· ¦|e \a|r ¦o·n Js|rg ¦|e ¦o||o.|rg ¦ab|e
Property VaIue
Size.WidLh 550
Size.HeighL 850
8ackColor SysLem.MenuHighlighL
ForeColor Web.WhiLe
FonL 1ahoma 8 8old
;^\jgZ."&
I\XjbYg[X@T\aYbe`\ag[XJXTg[Xe
GeTV^XeTcc_\VTg\ba
AII icons or image fiIes in this
chapter wiII be Iocated in a foIder
named lmages under the Chapter9
foIder where you instaIIed the
companion content. The defauIt
Iocation is \My Documents\
Microsoft Press\VB 2005 £xpress\.
< @C BEG4AG
6bag\ahXWbag[XaXkgcTZX
C09622132.indd 166 10/19/05 10:41:56 AM
Chapter 9. BuiId Your Own weather Tracker AppIication Now! l67
Property VaIue
lcon Sun.ico
Minimize8ox False
Maximize8ox False
SLarLPosiLion CenLerScreen
Form8orderSLyle FixedDialog
Showln1ask8ar False
Double8uííered 1rue
WindowSLaLe Minimized
Adding Notification Area CapabiIities
Now LhaL you have esLablished Lhe Main íorm, you'll add Lhe noLiíicaLion area capabiliLies.
LeL's Lalk abouL Lerminology. lí an applicaLion uses an icon locaLed in Lhe noLiíicaLion area
(Lhe area on Lhe Windows 1askbar where Lhe clock normally appears), Lhis icon is called a
noLiíy icon and is implemenLed wiLh a NoLiíylcon conLrol. 1he icon can have a conLexL menu
wiLh diííerenL acLions. ¥our icon will have a conLexL menu wiLh Lhe íollowing choices. AbouL,
Reíresh WeaLher lnío, OpLions, Open, and FxiL.
TO Ck£AT£ A NOTllYlCON CONTkOL
$
|r ¦|e Too|bo. ¦·on ¦|e Connor Cor¦·o|s g·oJp. d·ag a Bch]Zm=Wcb or ¦|e ¦o·n |¦ appea·s |r ¦|e
conporer¦ ¦·ay Nane ¦|e cor¦·o| bch]ZmKYUh\Yf
%

C|arge |¦s ¦e.¦ p·ope·¦y ¦o KYUh\YfHfUW_Yf
DoubIeBuffered heIps to reduce
or prevent fIickering when the
form is redrawn. The form controI
is using a secondary buffer to
update the form's graphics data,
whereby a quick write to the dis-
pIayed surface memory is then
performed, reducing the chances
of fIickering. lf DoubIeBuffered
is not enabIed, then progressive
redrawing of parts of the dis-
pIayed form occurs, creating the
fIickering.
@BE8 < A9 B
6bag\ahXWYeb`cTZX$))
The Notifylcon controI does not
have a design representation on
the form surface, so it is added to
the components tray at the same
pIace where the ADO.N£T compo-
nents were added in the previous
chapter.
ABG 8
C09622132.indd 167 10/19/05 10:41:57 AM
l68 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
&
|r ¦|e Too|bo. ¦·on ¦|e \erJs & Too|ba·s g·oJp. d·ag a
Cor¦e.¦\erJS¦·|p or¦o ¦|e ¦o·n ard rane |¦ WagBch]Zm
'
|s|rg ¦|e sna·¦ ¦ag or ¦|e cnsNo¦|¦y cor¦·o| |r ¦|e conporer¦
¦·ay. se|ec¦ 9X]h]hYag""" T|e |¦ens Co||ec¦|or |d|¦o· appea·s
(
|r ¦|e |¦ens Co||ec¦|or |d|¦o·. c|arge ¦|e cnsNo¦|¦y cor¦·o|'s
p·ope·¦|es Js|rg ¦|e ¦o||o.|rg ¦ab|e
Property VaIue
8ackColor SysLem.CradienL
lnacLiveCapLion
ShowlmageMargin False
)
|·on ¦|e Se|ec¦ |¦en /rd /dd To ||s¦ |e|o. d·opdo.r ||s¦ or
¦|e |e¦¦. se|ec¦ AYbi=hYa ard c||c| ¦|e 5XX bJ¦¦or C|arge ¦|e
cor¦·o|'s p·ope·¦|es Js|rg ¦|e ¦o||o.|rg ¦ab|e
Property VaIue
(Name) LsmiAbouL
1exL AbouL . . .
ForeColor SysLem.MenuHighlighL
*
|·on ¦|e Se|ec¦ |¦en /rd /dd To ||s¦ |e|o. d·opdo.r ||s¦.
se|ec¦ GYdUfUhcf ard c||c| ¦|e 5XX bJ¦¦or C|arge |¦s |o·eCo|o·
p·ope·¦y ¦o GmghYa.AYbi<][\`][\h
+
|·on ¦|e Se|ec¦ |¦en /rd /dd To ||s¦ |e|o. d·opdo.r ||s¦.
se|ec¦ AYbi=hYa ard c||c| ¦|e 5XX bJ¦¦or C|arge ¦|e cor¦·o|'s
p·ope·¦|es Js|rg ¦|e ¦o||o.|rg ¦ab|e
Property VaIue
(Name) LsmiReíresh
1exL Reíresh WeaLher lnío
ForeColor SysLem.MenuHighlighL
,
|·on ¦|e Se|ec¦ |¦en /rd /dd To ||s¦ |e|o. d·opdo.r ||s¦.
se|ec¦ GYdUfUhcf ard c||c| ¦|e 5XX bJ¦¦or C|arge |¦s |o·eCo|o·
p·ope·¦y ¦o GmghYa.AYbi<][\`][\h
$#
|·on ¦|e Se|ec¦ |¦en /rd /dd To ||s¦ |e|o. d·opdo.r ||s¦.
se|ec¦ AYbi=hYa ard c||c| ¦|e 5XX bJ¦¦or C|arge ¦|e
cor¦·o|'s p·ope·¦|es Js|rg ¦|e ¦o||o.|rg ¦ab|e
Property VaIue
(Name) LsmiOpLions
1exL OpLions.
ForeColor SysLem.MenuHighlighL
$$
|·on ¦|e Se|ec¦ |¦en /rd /dd To ||s¦ |e|o. d·opdo.r ||s¦.
se|ec¦ GYdUfUhcf ard c||c| ¦|e 5XX bJ¦¦or C|arge |¦s |o·eCo|o·
p·ope·¦y ¦o GmghYa.AYbi<][\`][\h
$%
|·on ¦|e Se|ec¦ |¦en /rd /dd To ||s¦ |e|o. d·opdo.r ||s¦.
se|ec¦ AYbi=hYa ard c||c| ¦|e 5XX bJ¦¦or C|arge ¦|e cor
¦·o|'s p·ope·¦|es Js|rg ¦|e ¦o||o.|rg ¦ab|e
Property VaIue
(Name) LsmiOpen
1exL Open.
ForeColor SysLem.MenuHighlighL
C09622132.indd 168 10/19/05 10:41:58 AM
Chapter 9. BuiId Your Own weather Tracker AppIication Now! l69
$&
|·on ¦|e Se|ec¦ |¦en /rd /dd To ||s¦ |e|o. d·opdo.r ||s¦. se|ec¦ AYbi=hYa ard c||c| ¦|e 5XX bJ¦
¦or C|arge ¦|e cor¦·o|'s p·ope·¦|es Js|rg ¦|e ¦o||o.|rg ¦ab|e
Property VaIue
(Name) LsmiFxiL
1exL FxiL
ForeColor SysLem.MenuHighlighL
`oJ'·e ¦|r|s|ed add|rg |¦ens ¦o ¦|e cor¦e.¦ nerJ s¦·|p T|e |¦ens Co||ec¦|or |d|¦o· s|oJ|d |oo| |||e ||gJ·e º´
$' C||c| C? ¦o c|ose ¦|e |¦ens Co||ec¦|or |d|¦o·
`oJ ro. reed ¦o assoc|a¦e ¦|e cor¦e.¦ nerJ s¦·|p .|¦| ¦|e ro¦|¦y\ea¦|e· cor¦·o|. .||c| |s ¦a|·|y easy ¦o do
$(
Se|ec¦ ¦|e bch]ZmKYUh\Yf cor¦·o| |r ¦|e conporer¦ ¦·ay ard |r ¦|e |·ope·¦|es .|rdo.. c|arge ¦|e
Cor¦e.¦\erJS¦·|p p·ope·¦y ¦o WagBch]Zm
;^\jgZ."'
<gX`fYbeg[XVbagXkg`Xah
C09622132.indd 169 10/19/05 10:41:59 AM
l70 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
`oJ a·e cJ··er¦|y ac¦|rg as ¦|e Jse· |r o·de· ¦o· yoJ ¦o be ab|e ¦o c||c| or ¦|e app||ca¦|or .|er |¦'s |r ¦|e ro¦|
¦|ca¦|or a·ea. yoJ· ro¦|¦y\ea¦|e· cor¦·o| reeds ar |cor T|e |cor .||| |a¦e· becone dyran|ca||y gere·a¦ed by
yoJ· app||ca¦|or. ard ¦|e |cor .||| becone ¦|e cJ··er¦ ¦enpe·a¦J·e T|e·e¦o·e. yoJ ro. reed ¦o assoc|a¦e a
¦enpo·a·y |cor .|¦| ¦|e app||ca¦|or. o¦|e·.|se yoJ .oJ|d ro¦ be ab|e ¦o se|ec¦ |¦ |r ¦|e ro¦|¦|ca¦|or a·ea
$)
|r ¦|e |·ope·¦|es .|rdo. ¦o· ro¦|¦y\ea¦|e·. se¦ ¦|e |cor p·ope·¦y ¦och\Yfcdh]cbg"]Wc T||s ¦||e |s |oca¦
ed |r a ¦o|de· raned |nages Jrde· ¦|e C|ap¦e·º ¦o|de· .|e·e yoJ |rs¦a||ed ¦|e conpar|or cor¦er¦
$*
|·ess :) ¦o e.ecJ¦e ¦|e app||ca¦|or
`oJ s|oJ|d see ¦||s |cor |r yoJ· ro¦|¦|ca¦|or a·ea |¦ yoJ ·|g|¦c||c| or ¦||s |cor. yoJ s|oJ|d see ¦|e
cor¦e.¦ nerJ s|o.r |r ||gJ·e º`
When you are íinished, Lhe only way Lo sLop Lhe applicaLion is Lo click Lhe blue SLop
Debugging buLLon in Lhe visual SLudio Loolbar. ¥ou will now add anoLher way Lo sLop Lhe
applicaLion.
TO STOP AN APPLlCATlON
$ Se|ec¦ ¦|e hga]9l]h cor¦·o| ¦·on ¦|e |·ope·¦|es .|rdo. d·opdo.r ||s¦
%
C||c| or ¦|e YjYbh |cor (ye||o. ||g|¦r|rg) |r ¦|e |·ope·¦|es .|rdo. ard ¦|er doJb|ec||c| ¦|e W`]W_
ever¦ T|e Code v|e. appea·s
&
|d|¦ ¦|e hga]9l]hS7`]W_ ever¦ |ard|e· ard add ¦|e S|J¦do.r ne¦|od as s|o.r |r ¦|e ¦o||o.|rg code
/ S|J¦do.r ne¦|od |s c·ea¦ed becaJse yoJ a|.ays .ar¦ ¦o na|e yoJ· code ·eJsab|e. ard a
S|J¦do.r ne¦|od .||| erab|e yoJ ¦o do ¦||s
1 P¡1vafe 5ub fsm1Lx1fC]1ck18yva] sende¡ As 5ysfem.Ob1ecf. 8yva] e As 5ysfem.LvenfA¡gs}
hand]es fsm1Lx1f.C]1ck
2 Me.5hufdoWn1}
3 Lnd 5ub
4
S P¡1vafe 5ub 5hufdoWn1}
6 lf nof1fyWeafhe¡.v1s1b]e 1hen
7 nof1fyWeafhe¡.v1s1b]e = Ia]se
ß Lnd lf
9 App]1caf1on.Lx1f1}
10 Lnd 5ub
;^\jgZ."(
6bagXkg`XahbYg[X
abg\YlJXTg[XeVbageb_
C09622132.indd 170 10/19/05 10:42:00 AM
Chapter 9. BuiId Your Own weather Tracker AppIication Now! l7l
T|e ¦|·s¦ |rs¦·Jc¦|or o¦ ¦|e S|J¦do.r ne¦|od .||| ve·|¦y |¦ ¦|e ro¦|¦y\ea¦|e· cor¦·o| |s v|s|b|e ard. |¦ |¦ |s. .|||
na|e ¦|e ro¦|¦y |cor d|sappea· ¦·on ¦|e ro¦|¦|ca¦|or a·ea T|e |as¦ ||re .||| ¦e·n|ra¦e ¦|e app||ca¦|or `oJ
.||| ro. be ab|e ¦o se|ec¦ ¦|e |.|¦ nerJ c|o|ce ¦·on ¦|e cor¦e.¦ nerJ ¦o ¦e·n|ra¦e ¦|e app||ca¦|or. yoJ
.or'¦ reed ¦o Jse ¦|e S¦op DebJgg|rg bJ¦¦or `oJ car ¦·y yoJ· app||ca¦|or by p·ess|rg |' ¦o ve·|¦y .|e¦|
e· ¦|e |.|¦ nerJ c|o|ce .o·|s as e.pec¦ed
No. yoJ car e.|¦ ¦·on yoJ· app||ca¦|or. bJ¦ yoJ dor'¦ |ave a .ay ¦o oper ¦|e na|r ¦o·n. .||c| .||| |ave
¦|e .ea¦|e· |r¦o·na¦|or To do ¦||s. yoJ .||| .ar¦ ¦o ||r| ¦|e doJb|ec||c| ever¦ o¦ ¦|e ro¦|¦y\ea¦|e· cor¦·o|
|cor |r ¦|e ro¦|¦|ca¦|or a·ea ¦o ¦|e ac¦|or o¦ oper|rg ¦|e \a|r ¦o·n |r ¦|e n|dd|e o¦ ¦|e sc·eer
'
|r Des|gr v|e.. se|ec¦ ¦|e bch]ZmKYUh\Yf cor¦·o| |r ¦|e conporer¦ ¦·ay |r ¦|e ever¦s ||s¦ o¦ ¦|e |·ope·¦|es
.|rdo.. doJb|ec||c| ¦|e AcigY8ciV`Y7`]W_ ever¦ |d|¦ ¦|e bch]ZmKYUh\YfSAcigY8ciV`Y7`]W_ ever¦
|ard|e· ard add ¦|e |es¦o·e ne¦|od as s|o.r |r ¦|e ¦o||o.|rg code
11 P¡1vafe 5ub Resfo¡e1}
12 lf Me.W1ndoW5fafe = Io¡mW1ndoW5fafe.M1n1m1zed 1hen
13 Me.W1ndoW5fafe = Io¡mW1ndoW5fafe.No¡ma]
14 Lnd lf
1S Me.v1s1b]e = 1¡ue
16 Lnd 5ub
17 P¡1vafe 5ub nof1fyWeafhe¡MouseDoub]eC]1ck18yva] sende¡ As 5ysfem.Ob1ecf. 8yva] e As
5ysfem.W1ndoWs.Io¡ms.MouseLvenfA¡gs} hand]es nof1fyWeafhe¡.MouseDoub]eC]1ck
1ß Me.Resfo¡e1}
19 Lnd 5ub
/ga|r. a p·|va¦e ne¦|od ca||ed |es¦o·e() .as c·ea¦ed |r case yoJ reed |¦ e|se.|e·e |r yoJ· app||ca¦|or
T|e ¦|·s¦ ||re o¦ code |r ¦|e |es¦o·e ne¦|od |s ¦|e·e becaJse |¦ |s |nposs|b|e ¦o |ro. |r .||c| cor¦e.¦ yoJ·
ne¦|od .||| be ca||ed |r yoJ· case. .|er yoJ c·ea¦ed ¦|e ¦o·n. yoJ se¦ ¦|e \|rdo.S¦a¦e p·ope·¦y ¦o n|r|
n|.ed ard S|o.|rTas|ba· ¦o ¦a|se so ¦|a¦ ¦|e ¦o·n s¦a·¦s n|r|n|.ed ard ¦|e Jse· doesr'¦ see |¦ \|er yoJ
s¦a·¦ ¦|e app||ca¦|or. ¦|e ¦|·s¦ ¦|ne ¦|e Jse· se|ec¦s ¦|e Cper nerJ c|o|ce (yoJ'|| code ¦||s soor) o· doJ
b|ec||c|s ¦|e ro¦|¦y\ea¦|e· cor¦·o|. ¦|e Jse· .or'¦ be ab|e ¦o see ¦|e ¦o·n |¦ yoJ |ave or|y se¦ |¦s \|s|b|e
p·ope·¦y ¦o ¦·Je T|e·e¦o·e. yoJ reed ¦o ve·|¦y |r .||c| \|rdo.S¦a¦e ¦|e ¦o·n appea·s |¦ |¦'s s¦||| n|r|n|.ed.
yoJ reed ¦o se¦ |¦ ¦o No·na| so ¦|a¦ ¦|e ¦ocJs |s or ¦|e \a|r ¦o·n
(
|·ess :) ¦o ¦es¦ ¦|e c|arges DoJb|ec||c| ¦|e bch]ZmKYUh\Yf |cor |r ¦|e ro¦|¦|ca¦|or a·ea ard ¦|e
\a|r ¦o·n s|oJ|d appea·
No. |e¦'s see .|a¦ |appers |¦ ¦|e Jse· c||c|s ¦|e C|ose bJ¦¦or (·ed `) |¦ yoJ c|ose ¦|e app||ca¦|or by Js|rg
¦|e C|ose bJ¦¦or. |¦ c|oses pe·narer¦|y `e¦. oJ· des|gr ·eoJ|·ener¦s s¦a¦e ¦|a¦ ¦|e app||ca¦|or s|oJ|d s|n
p|y n|r|n|.e bac| ¦o ¦|e ro¦|¦|ca¦|or a·ea .|er ¦|e Jse· c||c|s ¦|e C|ose bJ¦¦or T|e·e¦o·e. yoJ'|| ro. |r¦e·
cep¦ ar ever¦ ¦|a¦ occJ·s |Js¦ be¦o·e ¦|e ¦o·n |s c|osed ard |Js¦ be¦o·e ¦|e ¦o·n ob|ec¦ |s de|e¦ed. .||c| |s
ar ever¦ ca||ed ¦|e |o·nC|os|rg ever¦ |y Js|rg a |o·nC|os|rg ever¦. yoJ car e.¦·ac¦ ¦|e ·easor ¦o· ¦|e
¦o·n's c|os|rg ard |r ¦||s .ay |r¦e·cep¦ ¦|e ever¦ .|er ¦|e Jse· c||c|s ¦|e C|ose bJ¦¦or
C09622132.indd 171 10/19/05 10:42:00 AM
l72 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
)
|r Des|gr \|e.. se|ec¦ ¦|e AU]b ¦o·n Co ¦o ¦|e ever¦s ||s¦ |r ¦|e |·ope·¦|es .|rdo. ard doJb|ec||c|
¦|e :cfa7`cg]b[ ever¦ /dd ¦|e ¦o||o.|rg code ¦o ¦|e \a|r_|o·nC|os|rg ever¦ |ard|e·
20 P¡1vafe 5ub Ma1nIo¡mC]os1ng18yva] sende¡ As 5ysfem.Ob1ecf. 8yva] e As 5ysfem.W1ndoWs.
Io¡ms.Io¡mC]os1ngLvenfA¡gs} hand]es My8ase.Io¡mC]os1ng
21 lf 1e.C]oseReason = C]oseReason.use¡C]os1ng} 1hen
22 e.Cance] = 1¡ue
23 Me.h1de1}
24 Lnd lf
2S Lnd 5ub
|a·¦ o¦ ¦|e ever¦ |s ¦|e |o·nC|os|rg|ver¦/·gs. .||c| cor¦a|rs ¦|e a·gJner¦s ¦|a¦ acconpary ¦|e ever¦
ro¦|¦|ca¦|or as .e|| as ¦|e ·easor .|y ¦|e ¦o·n |s c|os|rg
|¦ ¦|e Jse· |s c|os|rg ¦|e ¦o·n. se¦¦|rg ¦|e Carce| p·ope·¦y ¦o ¦·Je .||| s¦op ¦|ec|os|rgp·ocess ard p·ever¦
¦|e ¦o·n ¦·on c|os|rg T|e re.¦ |rs¦·Jc¦|or |s a ca|| ¦o ¦|e ||de ne¦|od T|e ||de ne¦|od |s s|np|y a syr
oryn ¦o· se¦¦|rg ¦|e \|s|b|e p·ope·¦y ¦o ¦a|se T|e ¦o·n .||| s|np|y be ||dder
\|er ¦|e Jse· se|ec¦s ¦|e |.|¦ c|o|ce ¦·on ¦|e cor¦e.¦ nerJ. ¦|e |o·nC|os|rg ever¦ .||| be ·a|sed. |o.ev
e·. ¦|e ·easor g|ver .||| ro¦ be |se·C|os|rg |rs¦ead |¦ .||| be /pp||ca¦|or|.|¦Ca||. ard ¦|e·e¦o·e ¦|e app||ca
¦|or .||| cor¦|rJe ¦|e c|os|rg p·ocess
*
`oJ'|| ro. add ¦|e code ¦o· ¦|e Cper nerJ c|o|ce To .·|¦e ¦|e code ¦o· ¦||s ever¦. se|ec¦
WagBch]Zm |r ¦|e conporer¦ ¦·ay ard ¦|er doJb|ec||c| or ¦|e CdYb""" nerJ c|o|ce or ¦|e cor¦er¦
nerJ s¦·|p T|e c||c| ever¦ |ard|e· .||| be c·ea¦ed. ard yoJ'|| ca|| ¦|e |es¦o·e ne¦|od ¦o |ard|e ¦|e ¦o·n's v|s|
b|||¦y. .||c| |s dore by ¦|e \e|es¦o·e() code `oJ .||| .·|¦e code ¦o na|e sJ·e ¦|e ¦o·n |as ¦|e ¦ocJs so ¦|a¦ |¦
erds Jp or ¦op o¦ ary o¦|e· d|sp|ayed .|rdo.s /dd ¦|e ¦o||o.|rg code ¦o ¦|e ¦sn|Cper_C||c| ever¦ |ard|e·
26 P¡1vafe 5ub fsm1OpenC]1ck18yva] sende¡ As 5ysfem.Ob1ecf. 8yva] e As 5ysfem.LvenfA¡gs}
hand]es fsm1Open.C]1ck
27 Me.Resfo¡e1}
2ß Me.Iocus1}
29 Lnd 5ub
No.. ¦es¦ ¦|e app||ca¦|or .|¦| ¦|e ¦o||o.|rg ¦es¦ scera·|o
S¦a·¦ ¦|e app||ca¦|or by p·ess|rg :). ||g|¦c||c| or ¦|e bch]Zm |cor ard se|ec¦ CdYb""" T|e \a|r ¦o·n
s|oJ|d appea· |r ¦|e n|dd|e o¦ yoJ· sc·eer \|r|n|.e ¦|e \a|r ¦o·n by c||c||rg ¦|e7`cgY bJ¦¦or Crce |¦ |s
n|r|n|.ed. doJb|ec||c| ¦|e bch]Zm |cor `oJ s|oJ|d aga|r see ¦|e \a|r |o·n Te·n|ra¦e ¦|e app||ca¦|or by
se|ec¦|rg ¦|e 9l]h nerJ c|o|ce
lf you want to Iearn more about
why a form might be cIosing, you
can search the HeIp system by
entering the foIIowing keywords.
CIosekeason enumeration.
@BE8 < A9 B
The UserCIosing event wouId aIso
be caIIed whenever the user press-
es AIt+l4 or if the user seIects
CIose from the lorm ControI menu
{the menu that appears when you
cIick on the Ieft corner where the
icon is usuaIIy Iocated).
@BE8 < A9 B
C09622132.indd 172 10/19/05 10:42:01 AM
Chapter 9. BuiId Your Own weather Tracker AppIication Now! l73
Adding the SpIash Screen and About DiaIog Box
Since you have done Lhese procedures in ChapLer 6, l won'L spend Loo much Lime on Lhis
secLion. ¥ou merely need Lo add Lwo new íorms Lo your projecL and name Lhem
SpIashweather.vb and AboutweatherTracker.vb. Don'L íorgeL Lo use Lhe correcL LemplaLe
when you add Lhe íorm Lo Lhe projecL, ií you don'L, you won'L be able Lo access all oí Lhe
pre-populaLed iníormaLion.
Using Lhe íollowing Lable, seL Lhe speciíied properLies on Lhe SplashWeaLher íorm. 1he
locaLion oí your labels doesn'L maLLer, place Lhem wherever you wanL. Any images are locaL-
ed in Lhe lmages íolder under ChapLer9 oí Lhe companion conLenL.
Component Property VaIue
SplashWeaLher SLarLPosiLion CenLerScreen
MainLayouLPanel 8ackgroundlmage MounLain.jpg
Main LayouLPanel 8ackgroundlmageLayouL SLreLch
ApplicaLion1iLle FonL 1ahoma 20 Regular
ApplicaLion1iLle ForeColor Web.WhiLe
version FonL 1ahoma 9 8old
version ForeColor Web.WhiLe
CopyrighL FonL 1ahoma 9 8old
CopyrighL ForeColor Web.WhiLe
Using Lhe íollowing Lable, seL Lhe speciíied properLies on Lhe AbouLWeaLher1racker íorm.
C09622132.indd 173 10/19/05 10:42:01 AM
l74 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
Component Property VaIue
AbouLWeaLher1racker FonL 1ahoma 8
AbouLWeaLher1racker 8ackColor SysLem.MenuHighlighL
AbouLWeaLher1racker SLarLPosiLion CenLerScreen
AbouLWeaLher1racker ForeColor Web.WhiLe
1exL8oxDescripLion FonL 1ahoma 8
1exL8oxDescripLion 8ackColor SysLem.MenuHighlighL
1exL8oxDescripLion ForeColor Web.WhiLe
OK8uLLon FlaLSLyle FlaL
LogoPicLure8ox lmage SunseL.jpg
Now you need Lo aLLach Lhese Lwo íorms Lo Lhe resL oí Lhe applicaLion.
TO ATTACH lOkMS TO AN APPLlCATlON
$
To a¦¦ac| ¦|e sp|as| sc·eer. yoJ'|| Jse ¦|e |·o|ec¦ Des|gre· |r ¦|e So|J¦|or |.p|o·e·. ·|g|¦c||c| ¦|e
KYUh\YfHfUW_Yf p·o|ec¦ ard se|ec¦ DfcdYfh]Yg Cr ¦|e /pp||ca¦|or ¦ab. se¦ ¦|e Sp|as| Sc·eer d·op
do.r ||s¦ ¦o Gd`Ug\KYUh\Yf
%
\|||e yoJ a·e a¦ |¦. c|arge ¦|e app||ca¦|or |cor ¦o Gib"]Wc |r ¦|e |cor d·opdo.r ||s¦. se|ec¦
06fckgY"""2 Se|ec¦ ¦|e Gib"]Wc ¦||e |r ¦|e C|ap¦e·º |nages ¦o|de·
&
C||c| 5ggYaV`m=bZcfaUh]cb ard se¦ ¦|e assenb|y |r¦o·na¦|or ¦o· ¦|e p·o|ec¦ T||s |r¦o·na¦|or .||| ¦|||
¦|e sp|as| sc·eer ard /boJ¦ d|a|og bo. ¦|e|ds
'
To a¦¦ac| ¦|e /boJ¦ bo.. yoJ reed ¦o ¦|e |¦ ¦o ¦|e cor¦e.¦ /boJ¦ nerJ c|o|ce Co ¦o yoJ· AU]b"
jV ¦o·n |r des|gr node
(
Se|ec¦ WagBch]Zm |r ¦|e conporer¦ ¦·ay. ard doJb|ec||c| ¦|e 5Vcih""" nerJ c|o|ce |r ¦|e cor¦e.¦
nerJ s¦·|p /dd ¦|e ¦o||o.|rg code ¦o ¦|e ¦sn|/boJ¦_C||c| ever¦ |ard|e·
C09622132.indd 174 10/19/05 10:42:02 AM
Chapter 9. BuiId Your Own weather Tracker AppIication Now! l75
1 P¡1vafe 5ub fsm1AboufC]1ck18yva] sende¡ As 5ysfem.Ob1ecf. 8yva] e As 5ysfem.LvenfA¡gs}
hand]es fsm1Abouf.C]1ck
2 AboufWeafhe¡1¡acke¡.5hoWD1a]og1}
3 Lnd 5ub
Adding the Options DiaIog Box
¥ou now have Lhree íorms. ¥ou will add Lhe íinal OpLions dialog box íorm LhaL will appear
when Lhe user selecLs Lhe OpLions. menu choice in Lhe conLexL menu írom Lhe noLiíy icon.
TO ADD TH£ OPTlONS DlALOG BOX lOkM
$
|r ¦|e So|J¦|or |.p|o·e·. ·|g|¦c||c| KYUh\YfHfUW_Yf. se|ec¦ 5XX. ard ¦|er se|ec¦ K]bXckg:cfa
¦·on ¦|e cor¦e.¦ nerJ
%

|·on ¦|e ¦enp|a¦es. se|ec¦ K]bXckg:cfa. rane ¦|e ¦o·n Cdh]cbg"jV. ard ¦|er c||c| 5XX
&
|s|rg ¦|e ¦o||o.|rg ¦ab|e. se¦ p·ope·¦|es ard add cor¦·o|s ¦o ¦|e Cp¦|ors ¦o·n so ¦|a¦ |¦ |oo|s |||e ¦|e
¦o·n s|o.r |r ||gJ·e º4
Component ControI Type Property VaIue
OpLions Form FonL 1ahoma 8
OpLions Form 8ackColor SysLem.MenuHighlighL
OpLions Form SLarLPosiLion CenLerScreen
OpLions Form ForeColor Web.WhiLe
OpLions Form ConLrol8ox False
OpLions Form Form8orderSLyle FixedDialog
OpLions Form Maximize8ox False
OpLions Form Minimize8ox False
OpLions Form 1exL OpLions
;^\jgZ.")
G[XBcg\bafW\T_bZUbk
6bag\ahXWbag[XaXkgcTZX
C09622132.indd 175 10/19/05 10:42:03 AM
l76 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
Component ControI Type Property VaIue
LxLCurrenLZipCode 1exLbox AccepLReLurn 1rue
LxLCurrenLZipCode 1exLbox 8ackColor SysLem.lnacLiveCapLion
lblCurrenLZipCode Label 1exL CurrenL Zip Code.
bLnOk 8uLLon 8ackColor SysLem.lnacLiveCapLion
bLnOk 8uLLon FlaLSLyle Popup
bLnOk 8uLLon 1exL Ok
bLnCancel 8uLLon 8ackColor SysLem.lnacLiveCapLion
bLnCancel 8uLLon 1exL Cancel
bLnCancel 8uLLon FlaLSLyle Popup
OpLions Form AccepL8uLLon bLnOk
OpLions Form Cancel8uLLon bLnCancel
TO HOOK UP TH£ lOkM TO TH£ CONT£XT M£NU
$ D|sp|ay ¦|e AU]b ¦o·n |r des|gr node
%
Se|ec¦ WagBch]Zm |r ¦|e conporer¦ ¦·ay. ard doJb|ec||c| ¦|e Cdh]cbg""" nerJ c|o|ce |r ¦|e cor
¦e.¦ nerJ s¦·|p
&
/dd ¦|e ¦o||o.|rg ||res o¦ code ¦o ¦|e ¦s|Cp¦|ors_C||c| ever¦ |ard|e·
1 P¡1vafe 5ub fsm1Opf1onsC]1ck18yva] sende¡ As 5ysfem.Ob1ecf. 8yva] e As 5ysfem.
LvenfA¡gs} hand]es fsm1Opf1ons.C]1ck
2 opf1onsIo¡m.5hoWD1a]og1}
3 Lnd 5ub
' /¦ ¦|e ¦op o¦ \a|rvb. |Js¦ be|o. |Jb||c C|ass \a|r. add ¦|e ¦o||o.|rg ||re o¦ code
D1m opf1onsIo¡m as NeW Opf1ons1}
(
|·ess :) ¦o ·Jr ¦|e app||ca¦|or `oJ s|oJ|d see yoJ· sp|as| sc·eer |se ¦|e cor¦e.¦ nerJ or ¦|e ro¦|¦y
|cor ¦o oper ¦|e 5Vcih d|a|og bo. ard ¦|e Cdh]cbg d|a|og bo. \|er ¦|r|s|ed. e.|¦ ¦|e app||ca¦|or
6bag\ahXWYeb`g[XceXi\bhfcTZX
At this point in the chapter, the
current project state is saved in
the Chapter9 companion content
in a foIder named weather Tracker
Ul. To add the web service func-
tionaIity, you can continue with
your own own project or use the
project in weather Tracker Ul.
ABG 8
C09622132.indd 176 10/19/05 10:42:03 AM
Using web Services
Chapter 9. BuiId Your Own weather Tracker AppIication Now! l77
¥ou are now íinished wiLh Lhis parL oí Lhe projecL. 8e sure Lo save your projecL. lL is Lime
Lo add Lhe meaL oí Lhe projecL. using Web services.
¥ou have consLrucLed a nice shell, buL Lhe shell is raLher empLy aL Lhis momenL. ¥ou need Lo
access weaLher daLa in order Lo populaLe Lhe shell. 1o accomplish Lhis, you will learn Lo con-
sume Web services. 8uL íirsL. whaL is a Web service¯
A Web service is an applicaLion LhaL runs on a Web server, such as lnLerneL lníormaLion
Services (llS). A Web service has a series oí exposed public meLhods LhaL an applicaLion
can call. 1here are numerous examples oí Web services on Lhe lnLerneL. ¥ou can use Web
services LhaL períorm a varieLy oí operaLions, such as íinding a ZlP code, viewing a map,
buying movie LickeLs, looking íor iníormaLion on search engines such as MSN or Coogle,
and accessing weaLher iníormaLion like your applicaLion will soon do. ln Lhe .NF1 world,
Lhere are classes and wizards available Lo help you consume Web services.
Web services use XML Lo send messages and reLurn resulLs. All objecLs are serialized
(Lhe messages are senL as a series oí biLs and pieces over Lhe lnLerneL) and are Lhen de-
serialized on Lhe oLher side back inLo objecLs. 1he beauLy oí XML Web services is LhaL Lhey
can be hosLed and consumed on any operaLing sysLem and developed in any language.
8ecause Lhey use a series oí sLandardized proLocols and rules, XML Web services promoLe
inLeroperabiliLy and eííiciency. 1he íuLure oí Lhe LransacLed world over Lhe lnLerneL lies in big
parL wiLh Lhe success oí Web services.
ln Lhis projecL, you will use a weaLher daLa Web service. ¥ou will need Lo Lell visual
SLudio where Lhis Web service is locaLed so LhaL iL can add a Web reíerence Lo your projecL
LhaL poinLs Lo Lhis service. Follow Lhe sLeps below Lo accomplish Lhis Lask.
TO ADD w£B S£kVlC£S TO YOUk PkOJ£CT USlNG VlSUAL STUDlO
$
`oJ'|| reed ¦o ·eg|s¦e· ¦o· ¦|e ¦·ee .ea¦|e· \eb se·v|ce ¦o ob¦a|r a Jse·rane ard pass.o·d ¦o
aJ¦|er¦|ca¦e yoJ·se|¦ |r ¦ac¦. yoJ'|| Jse ¦|a¦ |r¦o·na¦|or ¦o ·e¦·|eve ¦|e .ea¦|e· da¦a C¦|e·.|se yoJ·
e.pe·|erce .||| be ||n|¦ed Co ¦o httµ.//www.ejse.ccm/WectherSiçnµµ/Siçnµµ.csµx ¦o ·eg|s¦e· ard ·ece|ve
yoJ· |og|r |r¦o·na¦|or
The weather web service you wiII
use in this part of the chapter
requires a free registration. Of
course, when you say free, you
have to think about the avaiIabiIity
and robustness of the web service.
l tried to choose a reIiabIe web
service, but l can't guarantee that
it wiII be avaiIabIe at aII times. You
can find many web services by
using your favorite search engine,
but here are a few web sites that
Iist web services. ]iie/$$lll#
mbZi]dYh#XdbVcY]iie/$$lll#
lZWhZgk^XZm#cZi$LH$YZ[Vjai#Vhem.
< @C BEG4AG
You wiII need to be connected to
the lnternet for the entire chapter,
otherwise nothing wiII work from
this point on, especiaIIy in the next
section because that's where
you're communicating with the
web service.
< @C BEG4AG
C09622132.indd 177 10/19/05 10:42:04 AM
l78 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
%
|r ¦|e So|J¦|or |.p|o·e·. ·|g|¦c||c| or ¦|e p·o|ec¦ rane ard ¦|er se|ec¦ 5XXKYVFYZYfYbWY""" T|e
/dd \eb |e¦e·erce d|a|og bo. appea·s
&
|r ¦|e ||| ¦e.¦ bo.. er¦e· ¦|e ||| o¦ ¦|e .ea¦|e· \eb se·v|ce. httµ.//www.ejse.ccm/WectherService/
Service.csmx. ard c||c| ¦|e ;c bJ¦¦or
'
|¦ yoJ a·e correc¦ed ¦o ¦|e |r¦e·re¦ ard ¦|e .ea¦|e· \eb se·v|ce |s ava||ab|e. yoJ .||| see a ||s¦ o¦
ne¦|ods ¦|a¦ a·e e.posed ¦o yoJ. ¦|e p·og·anne· |e¦e· ¦o ||gJ·e º' ¦o v|e. ¦|e ·esJ|¦s
;^\jgZ."*
?\fgbYXkcbfXW`Xg[bWfYeb`g[XjXTg[XeJXUfXei\VX
Ycµr reçµlcr Web brcwser
ccn be µseJ tc trv c Web ser-
vice withcµt writinç c line cf
ccJe. Ycµ ccn µsµcllv µcint
vcµr Web brcwser tc the Web
service cJJress cnJ invcle its
methcJs. 1here mcv be times.
hcwever. when this mcv nct
be µcssible. 1his is cn excel-
lent wcv tc lecrn whct c Web
methcJ neeJs cnJ whct its cµt-
µµt lccls lile. Plecse ncte thct
it´s nct µcssible tc tcll tc cll cf
them in this wcv. As cn excm-
µle. trv c reçicn Web service
thct retµrns c list cf cll stctes
in the U.S.. httµ.//www.svncµ-
ticJiçitcl.ccm/webservice/µµb-
lic/reçicns.csmx. Clicl cn the
IistByCountry methcJ. tvµe
in USA in the vellcw text bcx.
cnJ clicl lnvoke. ln c seµcrcte
brcwser. vcµ will cbtcin the list
cf stctes in the U.S.
(
C|arge ¦|e KYVFYZYfYbWYbUaY ¦o KYUh\YfKYVGYfj]WY ard c||c| ¦|e 5XX
FYZYfYbWY bJ¦¦or |¦ .||| ¦a|e sone ¦|ne as \|sJa| S¦Jd|o c·ea¦es a|| o¦ ¦|e recessa·y
¦||es ¦o· yoJ ¦o corsJne ¦|a¦ \eb se·v|ce
)
|rde· ¦|e \eb |e¦e·erces ¦o|de·. yoJ s|oJ|d ro. |ave a sna|| g|obe |cor .|¦| ¦|e
rane yoJ |Js¦ gave ¦o ¦|a¦ \eb se·v|ce T||s |s a \eb ·e¦e·erce ¦|a¦ po|r¦s ¦o ¦|e
.ea¦|e· \eb se·v|ce d|scove·ed a¦ ¦|a¦ ||| `oJ car ro. correc¦ ard e.ecJ¦e ¦|e
ne¦|ods or ¦||s \eb se·v|ce
C09622132.indd 178 10/19/05 10:42:05 AM
Chapter 9. BuiId Your Own weather Tracker AppIication Now! l79
*
C||c| or G\ck5``Z]`Yg |r ¦|e So|J¦|or |.p|o·e· ¦oo|ba· ard e.pard ¦|e KYUh\YfKYVGYfj]WY rode
`oJ'|| ¦|rd ¦|a¦ ¦|·ee |¦ens .e·e c·ea¦ed by \|sJa| S¦Jd|o a ¦||e ca||ed |e¦e·ercenap. a ¦||e ca||ed
Se·v|ce.sd|. ard aro¦|e· ca||ed Se·v|ced|sco |e¦e· ¦o ||gJ·e ºb ¦o see .|a¦ ¦|e So|J¦|or |.p|o·e· s|oJ|d
|oo| |||e T|e .ea¦|e· \eb se·v|ce |s ¦|e |ea·¦ ard soJ| o¦ ¦||s app||ca¦|or. .|¦|oJ¦ .||c| yoJ s|np|y |ave a
r|ce s|e|| .|¦|oJ¦ cor¦er¦
Reíerence.map is an XML íile LhaL conLains Lhe URL oí LhaL Web service. lL is produced by a
Lool called wsdl.exe. WSDL sLands íor Web Services DescripLion Language. 1he reíerence.map
íile is linked Lo a proxy íile called reíerence.vb. 1he proxy íile is a local represenLaLion oí Lhe
Web service on Lhe lnLerneL LhaL enables you Lo call meLhods in your projecLs wiLhouL having
compilaLion error or run Lime problems. lL also enables you Lo have lnLelliSense in visual SLudio
when using Lhe Web service. 1he proxy íile is a copy oí a class on Lhe Web server where Lhe
Web service is hosLed. 1he .wsdl íile is an XML íile describing Lhe Web service and whaL iL has
Lo oííer. Finally, Lhe .disco íile is an opLional íile íor discovering Lhe Web service.
ln ChapLer 8, "Managing Lhe DaLa," you learned LhaL you could creaLe daLa sources írom
a Web service, a daLabase, or an objecL. ln Lhis secLion, LhaL's exacLly whaL you'll beneíiL írom
here. ¥ou will see how re-using Lools and componenLs allows you Lo be more producLive.
¥ou will use Lhe same Lechniques used in Lhe previous daLabase secLions excepL LhaL Lhis
Lime you will be binding daLa coming across Lhe wire írom all parLs oí Lhe globe.
¥ou will now call Lhe public Web meLhods and obLain Lhe iníormaLion you need. 1his is
where Lhe íun begins. As you have previously done wiLh Lhe daLabase and wiLh daLabinding,
you will now execuLe Lhe same procedures íor Lhe Web service. Co Lo Lhe DaLa Sources win-
dow near Lhe SoluLion Fxplorer. ¥ou'll noLice LhaL you already have a weaLher Web service
daLa source. lL will Lhereíore be easy Lo drop Lhe weaLher iníormaLion you will need Lo see
on your íorm.
TO ADD w£ATH£k lNlOkMATlON TO YOUk lOkM
$ ||·s¦. na|e sJ·e yoJ a·e v|e.|rg ¦|e \a|r ¦o·n or ¦|e des|gre· sJ·¦ace
%
|r ¦|e Da¦a SoJ·ces .|rdo.. e.pard ¦|e weatherwebService rode. ard yoJ'|| see ¦|a¦ ¦|e·e a·e
¦|ve d|¦¦e·er¦ ¦ypes o¦ da¦a ¦|a¦ ¦|e \eb s|¦e car ·e¦·|eve T|ey a·e a|| da¦ase¦s
;^\jgZ."+
G[XjXTg[XeJXUfXei\VXTWWXW
gbg[Xceb]XVg
C09622132.indd 179 10/19/05 10:42:06 AM
l80 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
&
|.pard ¦|e KYUh\Yf=bZc rode ard c|arge a|| e|ener¦ ¦ypes (e.cep¦ |cor|rde.) |r ¦|a¦ da¦ase¦ ¦·on
Te.¦|o. ¦o |abe| by c||c||rg ¦|e do.r a··o. or eac| e|ener¦ ard se|ec¦|rg @UVY` ¦·on ¦|e d·opdo.r ||s¦
'
Se¦ |cor|rde. ¦o D]WhifY6cl |¦ ¦|e p|c¦J·e bo. ¦ype |s ro¦ ava||ab|e. yoJ car add |¦ |r ¦|e d·opdo.r
||s¦. c||c| ¦|e 7ighca]nY c|o|ce |r ¦|e Cp¦|ors d|a|og bo.. yoJ'|| see a d·opdo.r ||s¦ .|¦| poss|b|e
da¦a ¦ypes |r¦ege· s|oJ|d be se|ec¦ed by de¦aJ|¦ becaJse \|sJa| S¦Jd|o ·ecogr|.es ¦|a¦ ¦|e ¦|e|d ·e¦J·red
by ¦|e \eb ne¦|od |s ar |r¦ege·. |¦ ro¦. se|ec¦ =bhY[Yf ¹Js¦ be|o.. yoJ s|oJ|d see a se·|es o¦ c|ec| bo.es
·ep·eser¦|rg a ||s¦ o¦ assoc|a¦ed cor¦·o|s. .||c| nears a ||s¦ o¦ cor¦·o|s .|¦| .||c| ar |r¦ege· car be assoc|
a¦ed \|er yoJ sc·o|| do.r. yoJ s|oJ|d see ¦|e ||c¦J·e|o. cor¦·o| C||c| |r ¦|e c|ec| bo. bes|de
D]WhifY6cl ard ¦|er c||c| C? No. ¦o· |cor|rde.. se|ec¦ ¦|e D]WhifY6cl ¦ype ¦·on d·opdo.r ||s¦
(
|r ¦|e Da¦a SoJ·ces .|rdo.. se|ec¦ ¦|e KYUh\Yf=bZc rode ard c|arge ¦|e da¦a ·ep·eser¦a¦|or ¦·on
Da¦aC·|d\|e. ¦o 8YhU]`g by se|ec¦|rg |¦ |r ¦|e d·opdo.r ||s¦ \|er ¦|r|s|ed. ¦|e Da¦a SoJ·ces .|r
do. s|oJ|d |oo| |||e ||gJ·e º¯
)
D·ag ard d·op KYUh\Yf=bZc or¦o ¦|e \a|r ¦o·n des|gre· sJ·¦ace / \ea¦|e·|r¦o||rd|rgSoJ·ce ard
a \ea¦|e·|r¦o||rd|rgNav|ga¦o· |s added ¦o ¦|e conporer¦ ¦·ay
*
Se|ec¦ KYUh\Yf=bZc6]bX]b[BUj][Uhcf |r ¦|e conporer¦ ¦·ay ard c|arge |¦s \|s|b|e p·ope·¦y ¦o
ZU`gY (`oJ n|g|¦ reed ¦o c||c| or ¦|e DfcdYfh]Yg bJ¦¦or a¦ ¦|e ¦op o¦ ¦|e |·ope·¦|es .|rdo. ¦o see
¦|e ||s¦ o¦ p·ope·¦|es)
+
`oJ car'¦ see ¦|e boJrda·|es o¦ a|| ¦|e cor¦·o|s or ¦|e ¦o·n To |e|p .|¦| |ayoJ¦. se|ec¦ a|| o¦ ¦|e cor
¦·o|s or ¦|e ¦o·n by c·ea¦|rg a |a·ge se|ec¦|or ·ec¦arg|e a·oJrd ¦|en .|¦| yoJ· noJse |r ¦|e
|·ope·¦|es .|rdo.. se¦ ¦|e |o·de·S¦y|e ¦·on Nore ¦o :]lYXG]b[`Y ||ac| bo·de·s s|oJ|d appea· a·oJrd a||
cor¦·o|s Crce ¦|e ¦o·n |s dore. .e'|| se¦ ¦|e |o·de·S¦y|e bac| ¦o Nore
,
Cr ¦|e ||c¦J·e|o. cor¦·o|. se¦ ¦|e Nane p·ope·¦y ¦o dV7iffYbhHYad. se¦ ¦|e |ac|Co|o· p·ope·¦y ¦o
KYV.K\]hY. se¦ ¦|e S|.e\|d¦| ¦o )). ard se¦ S|.e|e|g|¦ ¦o ()
$# De|e¦e ¦|e |cor |rde. |abe|
$$ /dd a |abe| cor¦·o| ard se¦ ¦|e Te.¦ p·ope·¦y ¦o 7iffYbhHYadYfUhifY
$% C|arge ¦|e ¦e.¦ o¦ ¦|e Tenp·a¦J·e |abe| ¦o HYadYfUhifY.
;^\jgZ.",
G[XJXTg[Xe<aYbWTgT\ag[X
jXTg[XeJXUfXei\VX
C09622132.indd 180 10/19/05 10:42:07 AM
Chapter 9. BuiId Your Own weather Tracker AppIication Now! l8l
$&
C|arge ¦|e Nane p·ope·¦y o¦ ¦|e |abe| ¦o ¦|e ·|g|¦ o¦ Tenpe·a¦J·e ¦o `V`HYadYfUhifY7iffYbh T|e
|abe| |s |der¦|¦|ed .|¦| a ·ed bo. |r ||gJ·e º8
$'
|s|rg ||gJ·e º8 as a gJ|de. s|.e ard pos|¦|or ¦|e cor¦·o|s or ¦|e ¦o·n |¦ yoJ .ar¦. ad|Js¦ ¦|e ¦or¦
s|.e ard s¦y|e o¦ ¦|e |abe|s
;^\jgZ."-
?TlbhgbYg[XVheeXagjXTg[Xe\aYbe
`Tg\ba
User Settings
For Lhe WeaLher 1racker applicaLion Lo work, you will need Lo speciíy your weaLher Web ser-
vice regisLraLion iníormaLion. ¥our username and password will be saved in Lhe applicaLion
seLLings. 1he applicaLion seLLings are seLLings sLored in an XML íile and persisL írom one exe-
cuLion Lo anoLher. 1he currenL ZlP code will also be saved here.
TO Ck£AT£ US£k S£TTlNG £NTkl£S
$
|r ¦|e So|J¦|or |.p|o·e·. ·|g|¦c||c| ¦|e KYUh\YfHfUW_Yf p·o|ec¦ ard se|ec¦ DfcdYfh]Yg T|e |·o|ec¦
Des|gre· appea·s
%
|r ¦|e Se¦¦|rgs ¦ab. add er¦·|es ¦o· IgYfbUaY. DUggkcfX. ard
7iffYbhN]d7cXY as s|o.r |r ||gJ·e ºº (`oJ .||| reed ¦o spec|¦y
yoJ· o.r Jse·rane ard pass.o·d)
;^\jgZ.".
4cc_\VTg\bafXgg\aZf\ag[XCeb]XVg
7Xf\ZaXe
C09622132.indd 181 10/19/05 10:42:08 AM
l82 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
/|| er¦·|es a·e s¦·org|y ¦yped (|e. ·ea| N|T ¦ype) ard se¦ ¦o S¦·|rg ¦ype `oJ n|g|¦ be a|a·ned by ¦|e ¦ac¦
¦|a¦ |se·rane ard |ass.o·d a·e |r c|ea· ¦e.¦ |r yoJ· case. yoJ dor'¦ reed a g·ea¦ dea| o¦ secJ·|¦y becaJse
¦|e .ea¦|e· \eb se·v|ce |s a ¦·ee se·v|ce |¦ yoJ |ad ¦o. yoJ coJ|d erc·yp¦ ¦|ose se¦¦|rgs ¦o na|e ¦|en
no·e secJ·e
& Save yoJ· p·o|ec¦ ard c|ose ¦|e |·o|ec¦ Des|gre·
working in the Background
lí you Lry Lo run your íorm now, you won'L geL anyLhing írom Lhe Web service. 1his process
diííers írom your work wiLh daLabases, in which a greaL deal oí code was compleLed íor you
so you could reLrieve Lhe daLa and populaLe Lhe íields. When dealing wiLh a Web service,
you musL do more oí Lhe acLual coding Lo geL Lhe daLa inLo Lhe íorm. LeL's Lalk abouL how
you'll do Lhis.
FirsL, Lalking Lo a Web service can be a long process. A long process Lypically means only
a íew seconds (perhaps up Lo 80 seconds), buL you can'L leave Lhe user wiLh a blocked Ul
while your applicaLion is reLrieving iníormaLion. ¥ou Lhereíore need a way oí saying Lo your
applicaLion. "Co geL Lhis iníormaLion and leL me know when you have iL." 1his programming
Lechnique is called mulLiLhreaded programming wiLh call
backs. ln .NF1 Framework 2.0, Lhis Lype oí programming is
simpliíied by creaLing a new class called Lhe
8ackgroundWorker class. As iLs name implies, iL works in Lhe
background on a Lask, whaL's noL implied is LhaL iL will leL you
know when iL has compleLed Lhe Lask.
TO P£klOkM A TASK lN TH£ BACKGkOUND
$ Cper ¦|e AU]b ¦o·n |r des|gr v|e.
%
Co ¦o ¦|e Hcc`Vcl |r ¦|e Conporer¦s sec¦|or. se|ec¦ ¦|e 6UW_[fcibXKcf_Yf cor¦·o| ard d·ag |¦
or¦o yoJ· ¦o·n |¦ doesr'¦ |ave a des|gr ¦|ne po·¦|or. so |¦ .||| be added ¦o ¦|e conporer¦ ¦·ay
|erane |¦ 6UW_[fcibX7iffYbhKcf_Yf
&
/¦ ¦|e ¦op o¦ ¦|e |·ope·¦|es .|rdo. ¦o· |ac|g·oJrdCJ··er¦\o·|e·. c||c| ¦|e YjYbhg |cor (ye||o. ||g|¦
r|rg) ard ¦|er doJb|ec||c| ¦|e 8cKcf_ ever¦
The foIIowing sections contain
quite a bit of code. lf you don't
want to type this code, you can
open the compIeted weather
Tracker project in the companion
content and copy the sections of
code as needed.
ABG 8
C09622132.indd 182 10/19/05 10:42:09 AM
Chapter 9. BuiId Your Own weather Tracker AppIication Now! l83
' /dd ¦|e ¦o||o.|rg code ¦o ¦|e |ac|g·oJrdCJ··er¦\o·|e·_Do\o·| ever¦ |ard|e·
1 P¡1vafe 5ub 8ackg¡oundCu¡¡enfWo¡ke¡DoWo¡k18yva] sende¡ As Ob1ecf. 8yva] e As 5ysfem.
ComponenfMode].DoWo¡kLvenfA¡gs} hand]es 8ackg¡oundCu¡¡enfWo¡ke¡.DoWo¡k
2 ´ 1h1s mefhod W1]] execufe 1n fhe backg¡ound fh¡ead c¡eafed
3 ´ by fhe 8ackg¡oundWo¡ke¡ componenf
4 D1m des1¡edZ1pCode As lnfege¡ = e.A¡gumenf
S D1m Weafhe¡5e¡v1ce As NeW Weafhe¡Web5e¡v1ce.5e¡v1ce1}
6 e.Resu]f = Weafhe¡5e¡v1ce.GefWeafhe¡lnfo21
7 My.5eff1ngs.use¡name.
ß My.5eff1ngs.PassWo¡d. des1¡edZ1pCode}
9 Lnd 5ub
T|e Do\o·| ever¦ |ard|e· |s .|e·e ¦|e ca|| ¦o ¦|e .ea¦|e· \eb se·v|ce |s pe·¦o·ned `oJ .||| s¦a·¦ by ca||
|rg ¦|e Ce¦\ea¦|e·|r¦o´ ne¦|od e.posed by ¦|e \eb se·v|ce \|er yoJ |rvo|e ¦|e Ce¦\ea¦|e·|r¦o´
ne¦|od. |¦ ·Jrs |r a sepa·a¦e cor¦e.¦ so ¦|a¦ |¦ doesr'¦ b|oc| ¦|e app||ca¦|or || C¦|e·.|se. ¦|e app||ca¦|or
n|g|¦ appea· ¦o |arg
T|e Ce¦\ea¦|e·|r¦o´ ne¦|od ¦a|es ¦|·ee pa·ane¦e·s (Jse·rane. pass.o·d. ¯|| code) ard ·e¦J·rs a
\ea¦|e·|r¦o ·esJ|¦ |o. do yoJ |ro. .|a¦ pa·ane¦e·s yoJ reed ard ¦|e ·e¦J·r va|Je¯ `oJ car |oo| a¦ ¦|e
\SD| (p·oroJrced \|SD||) ¦||e ¦o Jrde·s¦ard /|so. .|er yoJ added ¦|e .ea¦|e· \eb se·v|ce. \|sJa|
S¦Jd|o gere·a¦es ¦|e app·op·|a¦e |r¦e|||Serse
( /dd ¦|e ¦o||o.|rg s¦a·¦|ac|g·oJrdTas|CJ··er¦Day ne¦|od
10 P¡1vafe 5ub sfa¡f8ackg¡ound1askCu¡¡enfDay1}
11 ´ Lxecufe fhe 8ackg¡ound 1ask on]y 1f 1f´s nof a]¡eady Wo¡k1ng
12 lf Nof 18ackg¡oundCu¡¡enfWo¡ke¡.ls8usy1}} 1hen
13 Me.useWa1fCu¡so¡ = 1¡ue
14 8ackg¡oundCu¡¡enfWo¡ke¡.RunWo¡ke¡Async1Clnf1cu¡¡enfZ1pCode}}
1S Lnd lf
16 Lnd 5ub
T|e s¦a·¦|ac|g·oJrdTas|CJ··er¦Day ne¦|od s¦a·¦s ¦|e |ac|g·oJrd\o·|e· T|e ¦|·s¦ ¦||rg yoJ reed ¦o do |s
ve·|¦y .|e¦|e· ¦|e |ac|g·oJrd\o·|e· |s a|·eady bJsy .|¦| a p·ev|oJs ca||. |¦ yoJ dor'¦ do ¦||s. yoJ'|| erd Jp
.|¦| ar |rva||dCpe·a¦|or|.cep¦|or S|np|y ve·|¦y|rg .|e¦|e· ¦|e |ac|g·oJrd\o·|e· |s bJsy ersJ·es ¦|a¦ yoJ
.or'¦ ge¦ ¦|a¦ e.cep¦|or .|er ca|||rg ¦|e |Jr\o·|e·/syrc ne¦|od |r ¦ac¦. ¦||s |s ¦|e or|y e.cep¦|or ¦|a¦
¦||s ne¦|od car ·a|se / oJ|c| |oo| a¦ ¦|e docJner¦a¦|or car cor¦|·n ¦||s
C09622132.indd 183 10/19/05 10:42:09 AM
l84 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
|.ecJ¦|rg ¦|e |Jr\o·|e·/syrc ne¦|od |s sJbn|¦¦|rg a ·eoJes¦ ¦o s¦a·¦ ar ope·a¦|or asyrc|·oroJs|y. .||c|
·a|ses ¦|e Do\o·| ever¦ /r ever¦ |ard|e· .|¦| ¦|e ¦o||o.|rg rane ¦o·na¦ |s |rvo|ed ·yoJ· bac|g·oJrd
.o·|e· va·|ab|e>_Do\o·| |r yoJ· case. ¦|e |ac|g·oJrdCJ··er¦\o·|e·_Do\o·| ne¦|od |s e.ecJ¦ed .|er
¦|e Do\o·| ever¦ |s ·a|sed
) S.|¦c| ¦o des|gr v|e. ard se|ec¦ 6UW_[fcibX7iffYbhKcf_Yf |r ¦|e ro¦|¦|ca¦|or a·ea
* |r ¦|e ever¦s ||s¦ |r ¦|e |·ope·¦|es .|rdo.. doJb|ec||c| ¦|e FibKcf_Yf7cad`YhYX ever¦
+ /dd ¦|e ¦o||o.|rg code ¦o ¦|e |ac|g·oJrdCJ··er¦\o·|e·_|Jr\o·|e·Conp|e¦ed ever¦ |ard|e·
17 P¡1vafe 5ub 8ackg¡oundCu¡¡enfWo¡ke¡RunWo¡ke¡Comp]efed18yva] sende¡ As Ob1ecf. 8yva]
e As 5ysfem.ComponenfMode].RunWo¡ke¡Comp]efedLvenfA¡gs} hand]es 8ackg¡oundCu¡¡enfWo¡ke¡.
RunWo¡ke¡Comp]efed
1ß D1m Weafhe¡lnfo As Weafhe¡Web5e¡v1ce.Weafhe¡lnfo
19
20 lf 1e.L¡¡o¡ ls Nofh1ng} 1hen
21 Me.useWa1fCu¡so¡ = Ia]se
22 ´ 1h1s evenf f1¡es When fhe DoWo¡k evenf comp]efes
23 Weafhe¡lnfo = e.Resu]f
24 Weafhe¡lnfo81nd1ng5ou¡ce.Dafa5ou¡ce = Weafhe¡lnfo
2S pbCu¡¡enf1emp.Load1".\\lmages\\" +
26 Weafhe¡lnfo.lconlndex.1o5f¡1ng1} + ".g1f"}
27 ´ lf Web se¡v1ce ¡efu¡ned Weafhe¡ 1nfo. fhen
2ß ´ updafe nof1fy 1con
29 cu¡¡enf1empe¡afu¡e = Me.Lxf¡acf1empe¡afu¡e1}
30 Me.C¡eafelcon1Clnf1Mafh.Round1cu¡¡enf1empe¡afu¡e}}}
31 L]self 1e.L¡¡o¡.Message.lndexOf1"S03"} > 0} 1hen
32 Message8ox.5hoW1
33 "Weafhe¡ Web se¡v1ce 1s unava1]ab]e. ¡ef¡y ]afe¡!" +
34 vbLf + "Ref¡y ]afe¡ us1ng fhe Ref¡esh Weafhe¡ lnfo menu.".
3S "Weafhe¡ Web se¡v1ce unava1]ab]e"}
36 L]self 1e.L¡¡o¡.Message.lndexOf1"f1med ouf"} > 0} 1hen
37 Message8ox.5hoW1
3ß "unab]e fo ¡ef¡1eve fhe 1nfo¡maf1on 1n fhe f1me " +
39 "a]]oWed" + vbLf + "Ref¡y ]afe¡ us1ng fhe Ref¡esh " +
40 "Weafhe¡ lnfo menu.". "Weafhe¡ Web se¡v1ce 11meouf"}
41 L]se
42 Message8ox.5hoW1
43 "P¡ob]em W1fh Weafhe¡ Web se¡v1ce! L¡¡o¡ message:" +
C09622132.indd 184 10/19/05 10:42:09 AM
Chapter 9. BuiId Your Own weather Tracker AppIication Now! l85
44 vbLf + e.L¡¡o¡.Message + vbLf + "Ref¡y Lafe¡!".
4S "Weafhe¡ Web se¡v1ce p¡ob]em"}
46 Lnd lf
47 Lnd 5ub
|¦ ¦|e \eb se·v|ce .as ava||ab|e ard yoJ· pa·ane¦e·s .e·e good. ¦|e ne¦|od yoJ |rvo|ed |s ¦|er .o·||rg
|r a d|¦¦e·er¦ cor¦e.¦ ard or |¦s o.r \|er |¦ |s ¦|r|s|ed .|¦| |¦s bJs|ress. yoJ .||| be ro¦|¦|ed ¦|a¦ ¦|e ne¦|
od |as conp|e¦ed becaJse a |Jr\o·|e·Conp|e¦ed ever¦ .||| be ·a|sed
1o reLrieve Lhe resulLs, you musL have an evenL handler wiLh Lhe íollowing name. <your
backgroundworker variable>_RunWorkerCompleLed. ln Lhis meLhod, you have a parameLer
oí Lype RunWorkerCompleLedFvenLArgs LhaL conLains everyLhing you need Lo obLain Lhe
resulLs. lí an excepLion was raised in Lhe DoWork evenL handler, you'll be able Lo reLrieve iL
by checking Lhe Frror properLy, which is oí Lype FxcepLion. lí Lhere is no error, Lhen you musL
reLrieve Lhe resulLs yourselí. Remember LhaL Lhe ResulLs properLy will give you an elemenL oí
Lype ObjecL, which by iLselí will noL help you. ¥ou need Lo assign iL a variable wiLh Lhe same
Lype LhaL Lhe Web service meLhod used Lo spiL ouL Lhe resulLs.
ln your case, ií you look inLo Lhe meLhod signaLure íor CeLWeaLherlnío2, WeaLherlnío is
Lhe Lype oí resulLs iL is producing. 1hereíore, you need Lo declare a variable oí LhaL Lype. lí
you recall, when you dragged Lhe WeaLherlnío daLaseL onLo Lhe designer suríace, you auLo-
maLically creaLed daLabound conLrols íor all oí Lhose íields. 1hus, you simply need Lo assign
LhaL WeaLherlnío variable as Lhe DaLaSource íor your 8indingSource, and you will have a link
beLween whaL's coming írom Lhe Web service and Lhe conLrols on your íorm.
Now, you also have a picLure box on Lhe designer suríace LhaL will serve Lo display an
icon porLraying Lhe currenL íorecasL. 1he inío is reLurned Lo you as an index. Lhe lconlndex
properLy. ¥ou need Lo load an image íile írom your hard drive inLo Lhe picLure box you
dragged onLo Lhe designer suríace using Lhe index as Lhe íilename.
WhaL happens nexL is Lhe creaLion oí Lhe icon LhaL will appear in Lhe noLiíicaLion area
represenLing Lhe currenL LemperaLure.
C09622132.indd 185 10/19/05 10:42:10 AM
l86 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
TO ADD SUPPOkTlNG BACKGkOUND COD£
$ /¦ ¦|e ve·y ¦op o¦ \a|rvb. add ¦|e ¦o||o.|rg |npo·¦s s¦a¦ener¦s
1 lmpo¡fs 5ysfem.Runf1me.lnfe¡op5e¡v1ces
2 lmpo¡fs 5ysfem.Nef
% /¦ ¦|e ¦op o¦ ¦|e c|ass. add ¦|e ¦o||o.|rg ||res |Js¦ be|o. |Jb||c C|ass \a|r
3 Pub]1c 5ha¡ed cu¡¡enf1empe¡afu¡e As Doub]e
4 Pub]1c 5ha¡ed cu¡¡enfZ1pCode As 5f¡1ng = My.5eff1ngs.Cu¡¡enfZ1pCode
T|e ¦|·s¦ ||re o¦ code |s dec|a·ed as a |Jb||c S|a·ed ¦|e|d raned cJ··er¦Tenpe·a¦J·e / s|a·ed ¦|e|d s|np|y
nears ¦|a¦ |¦ doesr'¦ be|org ¦o ary pa·¦|cJ|a· |rs¦arce o¦ ¦|a¦ c|ass. bJ¦ ¦|a¦ ¦|e·e |s or|y ore ¦o· ¦|e er¦|·e
c|ass T|e cJ··er¦¯|pCode code ¦oJrd |r ¦|e re.¦ ||re |s a|so a s|a·ed ¦|e|d |¦'s |r|¦|a||.ed ¦·on ¦|e Jse· se¦
¦|rgs. bJ¦ |¦ .||| c|arge orce yoJ conp|e¦e ¦|e Cp¦|ors ¦o·n
& /dd ¦|e ¦o||o.|rg |.¦·ac¦Tenpe·a¦J·e ne¦|od
S P¡1vafe Iuncf1on Lxf¡acf1empe¡afu¡e1} As Doub]e
6 lf 1]b]1empe¡afu¡eCu¡¡enf.1exf.Lengfh = 0} 1hen
7 Refu¡n lnfege¡.M1nva]ue
ß L]se
9 ´ Refu¡n1ng on]y fhe numbe¡ po¡f1on 1gno¡1ng fhe
10 ´ fhe I
11 Refu¡n CDb]1]b]1empe¡afu¡eCu¡¡enf.1exf.5ubsf¡1ng10.
12 ]b]1empe¡afu¡eCu¡¡enf.1exf.lndexOf1"¨"c}}}
13 Lnd lf
14 Lnd Iuncf1on
T|e |.¦·ac¦Tenpe·a¦J·e ne¦|od e.¦·ac¦s ¦|e ¦enpe·a¦J·e ¦·on ¦|e |b|Tenpe·a¦J·eCJ··er¦ |abe| ard ·e¦J·rs
|¦ as a rJnbe·
' /dd ¦|e ¦o||o.|rg |pda¦e\ea¦|e· ne¦|od
1S Pub]1c 5ub updafeWeafhe¡1}
16 1¡y
17 Me.fsm1Ref¡esh.Lnab]ed = Ia]se
1ß Me.fsm1Opf1ons.Lnab]ed = Ia]se
19 Me.sfa¡f8ackg¡ound1askCu¡¡enfDay1}
20 Me.fsm1Ref¡esh.Lnab]ed = 1¡ue
C09622132.indd 186 10/19/05 10:42:10 AM
Chapter 9. BuiId Your Own weather Tracker AppIication Now! l87
21 Me.fsm1Opf1ons.Lnab]ed = 1¡ue
22 Cafch WebLx As WebLxcepf1on
23 Message8ox.5hoW1
24 "Web se¡v1ce cu¡¡enf]y unava1]ab]e" + vbC¡Lf +
2S "Ref¡y ]afe¡ us1ng fhe Ref¡esh Weafhe¡ lnfo menu.".
26 "Web Lxcepf1on"}
27 Me.fsm1Ref¡esh.Lnab]ed = 1¡ue
2ß Cafch ex As Lxcepf1on
29 Message8ox.5hoW1
30 "unknoWn p¡ob]em. L¡¡o¡ message:" + vbC¡Lf
31 + ex.Message + vbC¡Lf +
32 "P]ease. ¡ef¡y ]afe¡!". "unknoWn e¡¡o¡"}
33 Me.fsm1Ref¡esh.Lnab]ed = 1¡ue
34 Lnd 1¡y
3S Lnd 5ub
T|e |pda¦e\ea¦|e· ne¦|od |r|¦|a¦es ¦|e Jpda¦e o¦ ¦|e .ea¦|e· da¦a by ca|||rg ¦|e s¦a·¦|ac|g·oJrdTas|
CJ··er¦Day ne¦|od yoJ added ea·||e· T|e .ea¦|e· da¦a reeds ¦o be Jpda¦ed .|er ¦|e ¯|| code |s
c|arged o· .|er ¦|e |e¦·es| \ea¦|e· |r¦o nerJ c|o|ce |s se|ec¦ed or ¦|e cor¦e.¦ nerJ o¦ ¦|e ro¦|¦y |cor
T|e |pda¦e\ea¦|e· ne¦|od a|so erab|es o· d|sab|es nerJ c|o|ces or ¦|e cor¦e.¦ nerJ as app·op·|a¦e
CompIeting the Core weather Tracker lunctionaIity
ln Lhe nexL secLions, you will add more code Lo obLain a working version oí Lhe WeaLher
1racker applicaLion. 1his includes creaLing Lhe icon, veriíying connecLiviLy, veriíying weaLher
Web service availabiliLy, and oLher Lasks.
ln Lhe íollowing secLion, you will add code Lo creaLe and desLroy Lhe icon in Lhe noLiíica-
Lion area. ¥ou can review Lhe code, buL l won'L discuss iL in much deLail because CDl- and
COM "inLerop" are subjecLs LhaL are Loo advanced íor Lhis book. However, you may wanL Lo
reíer Lo Lhe commenLs wiLhin Lhe code.
TO ADD Ck£AT£ AND D£STkOY NOTlllCATlON lCON COD£
$ |r \a|rvb. add ¦|e ¦o||o.|rg C·ea¦e|cor ne¦|od
1 P¡1vafe 5ub C¡eafelcon18yva] fempe¡afu¡e As lnfege¡}
2 D1m d1sp]ay5f¡1ng As 5f¡1ng
3 D1m d¡aWnlcon As 81fmap
4 D1m b¡ush1oD¡aW5f¡1ng As 5o]1d8¡ush
C09622132.indd 187 10/19/05 10:42:10 AM
l88 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
S D1m 1conCo]o¡ As Co]o¡
6 D1m 1conG¡aph1c As G¡aph1cs
7 D1m fonfIam1]y As NeW IonfIam1]y1"A¡1a]"}
ß D1m lconIonf As NeW Ionf1
9 fonfIam1]y.
10 11.
11 Ionf5fy]e.Regu]a¡.
12 G¡aph1csun1f.P1xe]}
13
14 lf 1fempe¡afu¡e = lnfege¡.M1nva]ue} 1hen
1S d1sp]ay5f¡1ng = "NA"
16 1conCo]o¡ = Co]o¡.Red
17 L]self 1fempe¡afu¡e > 100} 1hen
1ß 1conCo]o¡ = Co]o¡.Red
19 d1sp]ay5f¡1ng = 1fempe¡afu¡e - 100}.1o5f¡1ng1}
20 L]self 1fempe¡afu¡e < 0} 1hen
21 1conCo]o¡ = Co]o¡.8]ue
22 d1sp]ay5f¡1ng = 1fempe¡afu¡e " -1}.1o5f¡1ng1}
23 L]se
24 1conCo]o¡ = Co]o¡.8]ack
2S d1sp]ay5f¡1ng = fempe¡afu¡e.1o5f¡1ng1}
26 Lnd lf
27
2ß ´ 5fa¡f by c¡eaf1ng a neW b1fmap fhe s1ze of an 1con
29 d¡aWnlcon = NeW 81fmap116. 16}
30
31 ´1o d¡aW fhe sf¡1ng We need a b¡ush
32 b¡ush1oD¡aW5f¡1ng = NeW 5o]1d8¡ush11conCo]o¡}
33
34 ´ C¡eaf1ng a neW g¡aph1c ob1ecf so fhaf We
3S ´ can ca]] fhe d¡aWsf¡1ng mefhod W1fh ou¡
36 ´ fempe¡afu¡e o¡ NA 1f fhe¡e 1s no femp.
37 1conG¡aph1c = G¡aph1cs.I¡omlmage1d¡aWnlcon}

39 ´ NoW We a¡e d¡aW1ng fhe fempe¡afu¡e sf¡1ng onfo
40 ´ g¡aph1c and fhe¡efo¡e on fhe b1fmap.
41 1conG¡aph1c.D¡aW5f¡1ng1d1sp]ay5f¡1ng. lconIonf.
42 b¡ush1oD¡aW5f¡1ng. 0. 0}
43
44 ´ We a¡e geff1ng ¡eady fo conve¡f fhe b1fmap 1nfo
4S ´ an 1con and fo sef fhe nof1fyWeafhe¡.lcon W1fh
46 ´ fh1s neW]y c¡eafed 1con
C09622132.indd 188 10/19/05 10:42:11 AM
Chapter 9. BuiId Your Own weather Tracker AppIication Now! l89
47 D1m hlcon As lnfPf¡ = d¡aWnlcon.Gefh1con1}
4ß D1m cusfomMadelcon As lcon =
49 D¡aW1ng.lcon.I¡omhand]e1hlcon}
S0 nof1fyWeafhe¡.lcon = cusfomMadelcon
S1
S2 ´NoW fhaf We´¡e done man1pu]af1ng fhe neW 1con
S3 ´ We need fo desf¡oy fhe unmanaged ¡esou¡ce.
S4 ´ofhe¡W1se We´]] have a hand]e ]eak.
SS Desf¡oylcon1hlcon}
S6 Lnd 5ub
% /dd ¦|e ¦o||o.|rg Des¦·oy|cor ne¦|od
S7 ´ 1he Geflcon mefhod gene¡afed an unmanaged hand]e
Sß ´ fhaf We need fo fake ca¡e of ofhe¡W1se fhe¡e
S9 ´ W1]] be a hand]e ]eak.
60 <D]]lmpo¡f1"use¡32.d]]". Lnf¡yPo1nf:="Desf¡oylcon"}>
61 Pub]1c 5ha¡ed Iuncf1on Desf¡oylcon1
62 8yva] hlcon As lnfPf¡} As 8oo]ean
63 Lnd Iuncf1on
Once Lhe applicaLion sLarLs Lo load, you'll veriíy LhaL Lhe user has a valid and esLablished
lnLerneL connecLion. 1o veriíy Lhis, you'll make a simple H11P requesL Lo Lhe MicrosoíL Web
siLe. lí you receive an H11P OK (e.g., 200) code back írom Lhe Web server, iL means LhaL you
successíully accessed Lhe Web page you requesLed. lí an excepLion is raised, iL's because you
have encounLered a problem or Lhe MicrosoíL.com Web siLe is down, which is quiLe rare. lí a
valid lnLerneL connecLion is noL deLecLed, a message will be displayed.
¥ou'll also veriíy LhaL Lhe weaLher Web Service is up and running even beíore making a
call Lo iL. Again, ií Lhe Web service is noL up and running, a message will be displayed.
TO ADD V£klllCATlON COD£
$ |r \a|rvb. add ¦|e ¦o||o.|rg \e·|¦yCorrec¦edTo|r¦e·re¦ ne¦|od
1 Pub]1c Iuncf1on ve¡1fyConnecfed1olnfe¡nef1} As 8oo]ean
2 1¡y
3 D1m ¡equesf As WebRequesf =
4 WebRequesf.C¡eafe1"hffp://WWW.m1c¡osoff.com/"}
S D1m ¡esponse As hffpWebResponse =
6 C1ype1¡equesf.GefResponse1}. hffpWebResponse}
C09622132.indd 189 10/19/05 10:42:11 AM
l90 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
7 lf 1¡esponse.5fafusCode = hffp5fafusCode.Ok} 1hen
ß Refu¡n 1¡ue
9 L]se
10 Refu¡n Ia]se
11 Lnd lf
12 Cafch ex As Lxcepf1on
13 Refu¡n Ia]se
14 Lnd 1¡y
1S Lnd Iuncf1on
% /dd ¦|e ¦o||o.|rg \e·|¦y\ebSe·v|ce ne¦|od
16 P¡1vafe Iuncf1on ve¡1fyWeb5e¡v1ce1} As 8oo]ean
17 1¡y
1ß D1m ¡equesf As WebRequesf = WebRequesf.C¡eafe1
19 "hffp://WWW.e1se.com/Weafhe¡5e¡v1ce/5e¡v1ce.asmx?op=GefWeafhe¡lnfo2"}
20 D1m ¡esponse As hffpWebResponse =
21 C1ype1¡equesf.GefResponse. hffpWebResponse}
22 lf 1¡esponse.5fafusCode = hffp5fafusCode.Ok} 1hen
23 Refu¡n 1¡ue
24 L]se
2S Refu¡n Ia]se
26 Lnd lf
27 Cafch Lx As Lxcepf1on
2ß Refu¡n Ia]se
29 Lnd 1¡y
30 Lnd Iuncf1on
TO llNlSH TH£ MAlN lOkM
$
|r Des|gr v|e.. se|ec¦ ¦|e AU]b ¦o·n |r ¦|e |·ope·¦|es .|rdo.. go ¦o ¦|e ever¦ ||s¦ ard doJb|ec||c|
¦|e @cUX ever¦
% /dd ¦|e ¦o||o.|rg code ¦o ¦|e \a|r_|oad ever¦ |ard|e·
1 P¡1vafe 5ub Ma1nLoad18yva] sende¡ As Ob1ecf. 8yva] e As 5ysfem.LvenfA¡gs} hand]es
Me.Load
2 ´Chang1ng fhe f1f]e of ou¡ ma1n fo¡m W1fh fhe
3 ´app]1caf1on name and fhe ve¡s1on
C09622132.indd 190 10/19/05 10:42:11 AM
Chapter 9. BuiId Your Own weather Tracker AppIication Now! l9l
4 Me.1exf = My.App]1caf1on.lnfo.11f]e + " " +
S My.App]1caf1on.lnfo.ve¡s1on.1o5f¡1ng1}
6
7 ´C¡eaf1ng fempo¡a¡1]y fhe NA 1con.
ß Me.C¡eafelcon1lnfege¡.M1nva]ue}
9
10 lf Nof ve¡1fyConnecfed1olnfe¡nef1} 1hen
11 Message8ox.5hoW1
12 "You¡ compufe¡ doesn´f seem fo be connecfed " +
13 "fo fhe lnfe¡nef o¡ you¡ lnfe¡nef connecf1on " +
14 "1s nof Wo¡k1ng p¡ope¡]y!"}
1S Me.fsm1Opf1ons.Lnab]ed = Ia]se
16 Me.fsm1Ref¡esh.Lnab]ed = Ia]se
17 L]self Nof ve¡1fyWeb5e¡v1ce1} 1hen
1ß Message8ox.5hoW1
19 "1he Web se¡v1ce 1s unava1]ab]e af fhe momenf." + vbLf +
20 "Ref¡y ]afe¡ us1ng fhe Ref¡esh Weafhe¡ lnfo menu.".
21 "Web 5e¡v1ce Nof ava1]ab]e"}
22 lf 1cu¡¡enfZ1pCode = 5f¡1ng.Lmpfy} 1hen
23 Me.fsm1Ref¡esh.Lnab]ed = Ia]se
24 L]se
2S Me.fsm1Ref¡esh.Lnab]ed = 1¡ue
26 Lnd lf
27 L]se
2ß ´lf We have a va]1d z1p code fhen We can p¡oceed
29 fsm1Ref¡esh.Lnab]ed = Ia]se
30 Me.updafeWeafhe¡1}
31 Lnd lf
32 Lnd 5ub
T|e \a|r_|oad ever¦ |ard|e· |s ¦|e s¦a·¦|rg po|r¦ ¦o· \ea¦|e· T·ac|e· |r ¦||s code. ¦|e \y cors¦·Jc¦ |s
Jsed ¦o bJ||d ¦|e ¦|¦|e o¦ ¦|e app||ca¦|or by Js|rg |¦s rane ard ¦|e ve·s|or s¦o·ed |r ¦|e assenb|y pa·ane
¦e·s |r ¦|e sane narre· ¦|a¦ ¦|e /boJ¦ bo. Jses ¦||s |r¦o·na¦|or Ne.¦. a ·ed N/ (ro¦ ava||ab|e) |cor |s
d·a.r |r ¦|e ro¦|¦|ca¦|or a·ea ¦|a¦ .||| ·ena|r Jr¦|| ¦|e \eb se·v|ce ·e¦J·rs .|¦| pos|¦|ve ·esJ|¦s. |r .||c|
case ¦|e ¦enpe·a¦J·e .||| be d·a.r as ar |cor |¦ a correc¦|or ¦o ¦|e |r¦e·re¦ carro¦ be ve·|¦|ed o· ¦|e
.ea¦|e· \eb se·v|ce carro¦ be ve·|¦|ed. a nessage |s d|sp|ayed ard ¦|e app·op·|a¦e nerJ c|o|ces |r ¦|e
ro¦|¦y |cor cor¦e.¦ nerJ a·e d|sab|ed |¦ eve·y¦||rg |s .o·||rg as e.pec¦ed. ¦|e p·ocess ¦o· ob¦a|r|rg ¦|e
.ea¦|e· da¦a |s s¦a·¦ed
&
|r Des|gr v|e.. se|ec¦ WagBch]Zm |r ¦|e conporer¦ ¦·ay |r ¦|e cor¦e.¦ nerJ s¦·|p. doJb|ec||c| ¦|e
FYZfYg\KYUh\Yf=bZc nerJ c|o|ce
C09622132.indd 191 10/19/05 10:42:12 AM
l92 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
'
/dd ¦|e ¦o||o.|rg code ¦o ¦|e ¦sn||e¦·es|_C||c| ever¦ |ard|e· T||s code |r|¦|a¦es ar Jpda¦e o¦ ¦|e
.ea¦|e· da¦a .|er ¦|e |e¦·es| \ea¦|e· |r¦o nerJ c|o|ce |r ¦|e cor¦e.¦ nerJ |s se|ec¦ed
33 P¡1vafe 5ub fsm1Ref¡eshC]1ck18yva] sende¡ As 5ysfem.Ob1ecf. 8yva] e As 5ysfem.
LvenfA¡gs} hand]es fsm1Ref¡esh.C]1ck
34 Me.fsm1Ref¡esh.Lnab]ed = Ia]se
3S Me.updafeWeafhe¡1}
36 Lnd 5ub
(
|r Des|gr v|e.. se|ec¦ a|| o¦ ¦|e cor¦·o|s or ¦|e \a|r ¦o·n Se¦ ¦|e |o·de·S¦y|e p·ope·¦y ¦o BcbY (`oJ
n|g|¦ reed ¦o c||c| ¦|e DfcdYfh]Yg bJ¦¦or a¦ ¦|e ¦op o¦ ¦|e |·ope·¦|es .|rdo. ¦o see ¦|e ||s¦ o¦
p·ope·¦|es)
All oí Lhe weaLher íorecasL images need Lo be copied írom your companion conLenL
Lo Lhe same íolder where Lhe WeaLher 1racker applicaLion is locaLed. ¥ou need Lo creaLe a
íolder called lmages and copy all oí Lhe *.gií weaLher image íiles inLo Lhis íolder.
TO ADD w£ATH£k lCONS
$
|r ¦|e So|J¦|or |.p|o·e·. ·|g|¦c||c| ¦|e KYUh\YfHfUW_Yf p·o|ec¦. se|ec¦ 5XX. ard ¦|er se|ec¦ BYk
:c`XYf Nane ¦|e ¦o|de· =aU[Yg
%
|s|rg \|rdo.s |.p|o·e·. copy ¦|e ¯g|¦ |nages (1g|¦ ¦|·oJg| 4¯g|¦) ¦·on ¦|e conpar|or cor¦er¦ ¦o
¦|e |nages ¦o|de· yoJ |Js¦ c·ea¦ed (T|e de¦aJ|¦ |oca¦|or |s \\y DocJner¦s\\|sJa| S¦Jd|o ´JJ'\
|·o|ec¦s\\ea¦|e· T·ac|e·\\ea¦|e· T·ac|e·\|nages)
&
|r ¦|e So|J¦|or |.p|o·e·. ·|g|¦c||c| ¦|e =aU[Yg ¦o|de·. se|ec¦ 5XX. ard ¦|er se|ec¦ 9l]gh]b[=hYa T|e
/dd |.|s¦|rg |¦en d|a|og bo. appea·s
' |r ¦|e |||es C¦ Type d·opdo.r ||s¦. se|ec¦ =aU[Y:]`Yg
(
\a|e sJ·e yoJ a·e |oo||rg |r ¦|e |nages ¦o|de· ard se|ec¦ a|| o¦ ¦|e g|¦ ¦||es To se|ec¦ a|| ¦|e ¦||es. yoJ
car p·ess 7hf`5 o· yoJ car Jse G\]ZhW`]W_
)
\|er a|| o¦ ¦|e g|¦ ¦||es a·e se|ec¦ed. c||c| ¦|e 5XX bJ¦¦or ¦o add ¦|e |nages ¦o ¦|e \ea¦|e· T·ac|e·
p·o|ec¦
Ycµ mcv be wcnJerinç.
Whct is the linl between the
inJex cnJ the filencme. cnJ
whc is crectinç thct linl? 1his
is c ccnventicn µseJ bv mcnv
wecther µrcviJers cn the
lnternet. therefcre. this is scme-
thinç thct will wcrl with mcnv
services if vcµ wcnt tc cJJ
scme lcter.
C09622132.indd 192 10/19/05 10:42:12 AM
Chapter 9. BuiId Your Own weather Tracker AppIication Now! l93
*
|r ¦|e So|J¦|or |.p|o·e·. se|ec¦ a|| o¦ ¦|e g|¦ ¦||es ||·s¦. se|ec¦ %"[]Z ard ¦|er. .|||e p·ess|rg ¦|e G\]Zh
|ey. se|ec¦ ¦|e |as¦ g|¦ ¦||e
+
\|¦| a|| ¦|e g|¦ ¦||es se|ec¦ed. |r ¦|e |·ope·¦|es .|rdo.. se¦ ¦|e Copy To CJ¦pJ¦ D|·ec¦o·y p·ope·¦y ¦o
7cdm5`kUmg. as s|o.r |r ||gJ·e º1J \a|e sJ·e ¦|e |J||d /c¦|or p·ope·¦y |s se¦ ¦o 7cbhYbh
Testing weather Tracker
8eíore running Lhe WeaLher 1racker applicaLion, veriíy LhaL you have successíully compleLed
Lhe íollowing.
N ¥ou have an lnLerneL connecLion.
N ¥ou have regisLered íor Lhe íree weaLher Web service.
N ¥ou have speciíied your username and password in Lhe applicaLion seLLings.
Now you will see wheLher your applicaLion works. Press F5 Lo run WeaLher 1racker. lí you
have any build errors, review Lhe errors in Lhe Frror LisL window and íix Lhem. lí necessary,
you can review Lhe compleLed applicaLion in Lhe CompleLe íolder. When you run Lhe appli-
caLion, you should see your splash screen and Lhen see a red NA in Lhe noLiíicaLion area
indicaLing LhaL Lhe currenL LemperaLure has noL been
reLrieved. lí Lhe weaLher Web service is available, you
should see Lhe currenL LemperaLure íor Lhe Redmond,
WashingLon, area in Lhe noLiíicaLion area. (8e paLienL.
Depending on Lhe currenL Web service load, you mighL
have Lo waiL a íew momenLs.) When you double-click Lhe
LemperaLure in Lhe noLiíicaLion area, you should see
deLailed weaLher iníormaLion as shown in Figure 9-íí.
RighL-click Lhe LemperaLure Lo see Lhe conLexL menu. When
íinished, exiL Lhe applicaLion.
;^\jgZ."&%
JXTg[Xe\VbafTWWXWgbg[Xceb]XVg
;^\jgZ."&&
G[XJXTg[XeGeTV^XeTcc_\VTg\baW\fc_Tl\aZ
jXTg[XeWTgTYeb`TJXUfXei\VX
C09622132.indd 193 10/19/05 10:42:14 AM
l94 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
working with the Options DiaIog Box
CurrenLly, Lhe ZlP code is seL Lo a parLicular value, and LhaL really isn'L our inLenL. 1hereíore,
you will use Lhe OpLions dialog box and leL Lhe user enLer Lhe ZlP code Lhey wanL Lo moni-
Lor. 1he ZlP code will be persisLed Lo disk so LhaL, whenever Lhe user resLarLs Lhe applicaLion,
iL will be resLored Lo Lhe lasL ZlP code Lhey speciíied. Remembering Lhe user's seLLings írom
one execuLion Lo anoLher will provide Lhe user wiLh a beLLer experience.
¥ou will also períorm some checking Lo veriíy Lhe ZlP code enLered by Lhe user. ¥ou will
veriíy LhaL Lhe ZlP code is a number and wiLhin a speciíic range. ¥ou will use Lhe error pro-
vider conLrol Lo display appropriaLe LexL ií Lhe ZlP code is empLy or noL wiLhin range.
1he error provider conLrol is used Lo display error iníormaLion Lo Lhe user. For example, ií
Lhe user enLers invalid iníormaLion in a LexL box, an error icon is displayed nexL Lo Lhe con-
Lrol indicaLing LhaL an error has occurred. 8y deíaulL, Lhe error icon is a small red circle wiLh
an exclamaLion poinL. When Lhe user clicks Lhe error icon, an error descripLion is displayed Lo
explain whaL is wrong Lo Lhe user. ¥ou can change how Lhe error is presenLed. For example,
you can use a diííerenL error icon, and you can make Lhe error icon blink. Once a user
addresses Lhe error, you seL Lhe error descripLion Lo an empLy sLring Lo make Lhe error icon
disappear.
TO VALlDAT£ US£k lNPUT
$ Cper ¦|e Cdh]cbg ¦o·n |r Des|gr v|e.
%
|·on ¦|e Too|bo. |r ¦|e Conporer¦s g·oJp. add ar 9ffcfDfcj]XYf cor¦·o| ¦o ¦|e ¦o·n T|e cor¦·o|
.||| appea· |r ¦|e conporer¦ ¦·ay
& Nane ¦|e cor¦·o| 9ffcfDfcj]XYf7iffYbhN]d7cXY
' DoJb|ec||c| ¦|e C? bJ¦¦or
( /dd ¦|e ¦o||o.|rg code ¦o ¦|e b¦rC|_C||c| ever¦ |ard|e·
1 P¡1vafe 5ub bfnOkC]1ck18yva] sende¡ As 5ysfem.Ob1ecf. 8yva] e As 5ysfem.LvenfA¡gs}
hand]es bfnOk.C]1ck
C09622132.indd 194 10/19/05 10:42:14 AM
Chapter 9. BuiId Your Own weather Tracker AppIication Now! l95
2 lf va]1dafeZ1p1} 1hen
3 updafeCu¡¡enflnfo1}
4 Me.D1a]ogResu]f = W1ndoWs.Io¡ms.D1a]ogResu]f.Ok
S Me.C]ose1}
6 Lnd lf
7 Lnd 5ub
) /dd ¦|e ¦o||o.|rg \a||da¦e¯|p ne¦|od
ß P¡1vafe Iuncf1on va]1dafeZ1p1} As 8oo]ean
9 D1m z1pNumbe¡ As lnfege¡
10 D1m va]1dZ1pCode As 8oo]ean = 1¡ue
11
12 lf 1fxfCu¡¡enfZ1pCode.1exf <> 5f¡1ng.Lmpfy} 1hen
13 z1pNumbe¡ = Clnf1fxfCu¡¡enfZ1pCode.1exf}
14 lf Nof 11z1pNumbe¡ > 999} And 1z1pNumbe¡ <= 999S0}} 1hen
1S L¡¡o¡P¡ov1de¡Cu¡¡enfZ1pCode.5efL¡¡o¡1
16 Me.fxfCu¡¡enfZ1pCode.
17 "lnva]1d Z1p Code. enfe¡ a va]1d u5 z1p code " +
1ß "18efWeen 1000 and 999S0}"}
19 va]1dZ1pCode = Ia]se
20 L]se
21 L¡¡o¡P¡ov1de¡Cu¡¡enfZ1pCode.5efL¡¡o¡1
22 Me.fxfCu¡¡enfZ1pCode. ""}
23 Lnd lf
24 L]se
2S L¡¡o¡P¡ov1de¡Cu¡¡enfZ1pCode.5efL¡¡o¡1
26 Me.fxfCu¡¡enfZ1pCode.
27 "lnva]1d Z1p Code. enfe¡ a va]1d u5 z1p code " +
2ß "18efWeen 1000 and 999S0}"}
29 va]1dZ1pCode = Ia]se
30 Lnd lf
31 Refu¡n va]1dZ1pCode
32 Lnd Iuncf1on
T|e \a||da¦e¯|p ne¦|od ersJ·es ¦|a¦ ¦|e ¯|| code ¦e.¦ bo. |s ro¦ enp¦y ard c|ec|s ¦|a¦ ¦|e ¯|| code |s
g·ea¦e· ¦|ar ººº ard |ess ¦|ar o· eoJa| ¦o ººº'J |¦ ro¦. ar e··o· |s d|sp|ayed Js|rg ¦|e Se¦|··o· ne¦|od o¦
e··o· p·ov|de· cor¦·o| |r ¦|e ca|| ¦o Se¦|··o·. ¦|e ¦.¦CJ··er¦¯|pCode ¦e.¦ bo. |s spec|¦|ed. .||c| |rd|ca¦es
¦|a¦ ¦|e e··o· |s assoc|a¦ed .|¦| ¦|e ¦.¦CJ··er¦¯|pCode cor¦·o| |¦ ¦|e ¯|| code appea·s ¦o be va||d. ¦|e
\a||d¯|p ne¦|od ·e¦J·rs T·Je. o¦|e·.|se. |¦ ·e¦J·rs |a|se
* S.|¦c| bac| ¦o ¦|e Options ¦o·n |r Des|gr v|e.
C09622132.indd 195 10/19/05 10:42:14 AM
l96 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
+ Se|ec¦ ¦|e hlh7iffYbhN]d7cXY ¦e.¦ bo.
, |r ¦|e |·ope·¦|es .|rdo.. c||c| ¦|e YjYbhg |cor (ye||o. ||g|¦r|rg) ¦o d|sp|ay ¦|e ever¦s ||s¦
$#
DoJb|ec||c| ¦|e ?Ym8ckb ever¦
$$ /dd ¦|e ¦o||o.|rg code ¦o ¦|e ¦.¦CJ··er¯|pCode_|eyDo.r ever¦ |ard|e·
33 P¡1vafe 5ub fxfCu¡¡enfZ1pCodekeyDoWn18yva] sende¡ As 5ysfem.Ob1ecf. 8yva] e As 5ysfem.
W1ndoWs.Io¡ms.keyLvenfA¡gs} hand]es fxfCu¡¡enfZ1pCode.keyDoWn
34 lf e.keyCode < keys.D0 O¡L]se e.keyCode > keys.D9 1hen
3S ´ Defe¡m1ne Whefhe¡ fhe keysf¡oke 1s a numbe¡ f¡om fhe keypad.
36 lf e.keyCode < keys.NumPad0 O¡L]se e.keyCode > keys.NumPad9 1hen
37 ´ Defe¡m1ne Whefhe¡ fhe keysf¡oke 1s a backspace.
3ß lf e.keyCode <> keys.8ack 1hen
39 lf e.keyCode <> keys.Lnfe¡ 1hen
40 Message8ox.5hoW1"On]y nume¡1c cha¡acfe¡s p]ease!"}
41 Lnd lf
42 Lnd lf
43 Lnd lf
44 Lnd lf
4S Lnd 5ub
T||s code c|ec|s ¦|e Jse·'s |eys¦·o|es as ¦|ey ¦ype |r ¦|e ¯|| code |¦ ¦|e |eys¦·o|e |s ro¦ a rJnbe·. a nes
sage bo. |s d|sp|ayed
TO SAV£ S£TTlNGS AND UPDAT£ w£ATH£k DATA
$ |r Cp¦|orsvb. add ¦|e ¦o||o.|rg |pda¦eCJ··er¦|r¦o ne¦|od
1 P¡1vafe 5ub updafeCu¡¡enflnfo1}
2 lf 1Me.fxfCu¡¡enfZ1pCode.1exf <> My.5eff1ngs.Cu¡¡enfZ1pCode} 1hen
3 My.5eff1ngs.Cu¡¡enfZ1pCode = Me.fxfCu¡¡enfZ1pCode.1exf
4 Ma1n.cu¡¡enfZ1pCode = Me.fxfCu¡¡enfZ1pCode.1exf
S My.5eff1ngs.5ave1}
6 Ma1n.updafeWeafhe¡1}
7 Lnd lf
ß Lnd 5ub
C09622132.indd 196 10/19/05 10:42:15 AM
Chapter 9. BuiId Your Own weather Tracker AppIication Now! l97
T|e |pda¦eCJ···er¦|r¦o ne¦|od saves ¦|e Jse·'s ¯|| code bac| ¦o ¦|e app||ca¦|or se¦¦|rgs |¦ a|so ca||s ¦|e
|pda¦e\ea¦|e· ne¦|od |r ¦|e code ¦o· ¦|e \a|r ¦o·n ¦o Jpda¦e ¦|e .ea¦|e· da¦a ¦o· ¦|e re. ¯|| code
T|e |pda¦eCJ··er¦|r¦o ne¦|od |s ca||ed .|er ¦|e Jse· c||c|s C| |r ¦|e Cp¦|ors d|a|og bo.
Testing weather Tracker
Now you will LesL Lhe OpLions dialog box. Press F5 Lo run WeaLher 1racker. Once Lhe splash
screen disappears, righL-click Lhe icon in Lhe noLiíicaLion area and click OpLions. ln Lhe
OpLions dialog box, LesL Lhe ZlP validaLion code. For example, Lry Lo Lype alphabeLic charac-
Lers and Lry Lo Lype an invalid ZlP code. Figure 9-í2 shows Lhe error provider conLrol when
an ouL-oí-range ZlP code is enLered.
When íinished, Lype in a valid ZlP code and click OK. WaiL íor a íew momenLs and open
Lhe Main íorm. ¥ou should see weaLher daLa íor Lhe new ZlP code.
¥ou should be proud oí yourselí. ¥ou've developed an applicaLion wiLh numerous com-
plex íeaLures, and iL works! 1he WeaLher 1racker applicaLion accomplishes Lhe basic íeaLures
esLablished aL Lhe beginning oí Lhe chapLer. 1here is plenLy oí room íor enhancemenL. ln
íacL, ií you look in Lhe ChapLer9 íolder oí Lhe companion conLenL, you will íind an enhanced
version. lí you wanL, check ouL Lhis enhanced version and maybe sLep Lhrough Lhe code Lo
see how iL works. 1he enhanced version includes Lhe íollowing capabiliLies.
N Displays weaLher daLa íor Lhe currenL day, as well as a 8-day íorecasL.
N lncludes an OpLions dialog box wiLh many more conLrols and seLLings.
;^\jgZ."&'
G[XXeebecebi\WXeVbageb_\aW\VTg\aZ
TaXeebe
C09622132.indd 197 10/19/05 10:42:15 AM
l98 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
N Uses a ZlP Code Web service, which allows a user Lo selecL írom a lisL oí ZlP codes.
N Uses a SLaLe/Provinces Web service, which allows a user Lo search íor a ZlP code by ciLy.
N Allows user Lo selecL LemperaLure uniL, wheLher Celcius or FahrenheiL.
N Users a 1imer conLrol Lo auLomaLically reíresh Lhe weaLher daLa aL regular inLervals (e.g.,
every í0 minuLes). 1he user can change Lhis inLerval.
Now iL's Lime Lo learn how Lo disLribuLe WeaLher 1racker or anoLher applicaLion.
A new Lechnology íor deploymenL called CIickOnce is now available wiLh .NF1 Framework 2.0.
lL's a íanLasLic íeaLure LhaL leL's you cusLomize how your Lool geLs onLo oLher people's
machines. lL's very easy-almosL as easy as deploying Web applicaLions, which oíLen only
enLails copying íiles onLo a server. ClickOnce allows you, Lhe developer, Lo disLribuLe your
applicaLion via a robusL and reliable mechanism. ¥ou can deploy on Web servers, íile servers,
or onLo a CD/DvD. ¥ou can add Lhe .NF1 Framework Lo your disLribuLion package along wiLh
SOL Server 2005 Fxpress FdiLion ií your applicaLion needs iL. ClickOnce handles rollback and
uninsLall well, and iL's a charm Lo push new updaLes. ln your case, you'll deploy Lo a CD/DvD.
TO PACKAG£ AND PUBLlSH YOUk APPLlCATlON
$
To ersJ·e ¦|a¦ a|| o¦ ¦|e ¯g|¦ ¦||es ·ep·eser¦|rg ¦|e .ea¦|e· |cors a·e |rc|Jded .|¦| ¦|e |rs¦a||a¦|or.
na|e sJ·e ¦|e |J||d /c¦|or p·ope·¦y |s se¦ ¦o 7cbhYbh ¦o· a|| o¦ ¦|e ¯g|¦ |nages |r ¦|e So|J¦|or
|.p|o·e· (T||s p·ocess .as desc·|bed ea·||e· |r ¦|e c|ap¦e·)
% |ebJ||d ¦|e app||ca¦|or conp|e¦e|y by c||c||rg 6i]`XKYUh\YfHfUW_Yf on Lhe 8uild menu
&
|r ¦|e So|J¦|or |.p|o·e·. ·|g|¦c||c| ¦|e KYUh\YfHfUW_Yf p·o|ec¦ ard se|ec¦ DfcdYfh]Yg ¦o oper ¦|e
|·o|ec¦ Des|gre·
And Now, Just CIickOnce!
C09622132.indd 198 10/19/05 10:42:16 AM
Chapter 9. BuiId Your Own weather Tracker AppIication Now! l99
' C||c| ¦|e DiV`]g\ ¦ab `oJ s|oJ|d see a sc·eer ¦|a¦ |oo|s |||e ¦|e ore s|o.r |r ||gJ·e º1`
;^\jgZ."&(
ChU_\f[gTU\ag[XCeb]XVg7Xf\ZaXe
(
C||c| ¦|e 5dd`]WUh]cb:]`Yg bJ¦¦or ¦o see ¦|e ||s¦ o¦ ¦||es ¦|a¦ .||| be |rc|Jded |r ¦|e |rs¦a||a¦|or /|| o¦
¦|e ¯g|¦ ¦||es s|oJ|d be ||s¦ed C||c| C?
`oJ ro. .ar¦ ¦o se|ec¦ ¦|e p·e·eoJ|s|¦es ¦o· yoJ· app||ca¦|or \|er ¦|e |rs¦a||e· ·Jrs or ¦|e Jse·'s
nac||re. |¦ .||| c|ec| ¦o· ¦|e p·eserce o¦ ¦|ese |¦ens |¦ ¦|ey a·e ro¦ p·eser¦. ¦|e |rs¦a||e· .||| by de¦aJ|¦
do.r|oad ¦|en ¦·on \|c·oso¦¦con o· aro¦|e· soJ·ce ¦|a¦ yoJ |ave cor¦|gJ·ed
)
C||c| ¦|e DfYfYei]g]hYg bJ¦¦or |r ¦|e |·e·eoJ|s|¦es d|a|og bo.. se|ec¦ ¦|e "B9H:fUaYkcf_&"$ ard
K]bXckg=bghU``Yf'"% c|ec| bo.es \a|e sJ·e ¦|e 8ckb`cUXDfYfYei]g]hYg:fcaH\Y7cadcbYbh
JYbXcfÇgKYVG]hY op¦|or |s se|ec¦ed C||c| C?
* C||c| ¦|e DiV`]g\K]nUfX bJ¦¦or
T|e ¦|·s¦ page o¦ ¦|e .|.a·d appea·s. as s|o.r |r ||gJ·e º14. as||rg yoJ ¦o spec|¦y a |oca¦|or ¦o pJb||s|
¦|e app||ca¦|or
C09622132.indd 199 10/19/05 10:42:17 AM
200 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
+
C||c| ¦|e 6fckgY bJ¦¦or |r ¦|e Cper \eb S|¦e d|a|og bo..
se|ec¦ :]`YGmghYa or ¦|e |e¦¦. ard ¦|er se|ec¦ a |oca¦|or or
yoJ· conpJ¦e· .|e·e yoJ .ar¦ ¦o pJb||s| yoJ· app||ca¦|or | sJgges¦
yoJ c·ea¦e a re. ¦o|de· raned \ea¦|e· T·ac|e· `oJ car Jse ¦|e
C·ea¦e Ne. |o|de· |cor a¦ ¦|e Jppe· |e¦¦ ¦o c·ea¦e a re. ¦o|de·
\|er ¦|r|s|ed. c||c| ¦|e Cper bJ¦¦or ard ¦|er c||c| Ne.¦
,
Cr ¦|e re.¦ page. se|ec¦ |o. ¦|e Jse· .||| |rs¦a|| ¦|e app||ca
¦|or |¦ coJ|d be a \eb s|¦e. a |NC s|a·e or a re¦.o·|. o· a
CD o· D\D Se|ec¦ ¦|e :fca578!FCACf8J8!FCA c|o|ce ard
c||c| BYlh
$#
T|e re.¦ page as|s .|e¦|e· yoJ .ar¦ yoJ· app||ca¦|or ¦o |oo|
¦o· Jpda¦es eve·y ¦|ne |¦ s¦a·¦s |ecaJse yoJ a·e dep|oy|rg or
a CD o· D\D. yoJ .or'¦ |ave yoJ· app||ca¦|or c|ec| ¦o· Jpda¦es
Se|ec¦ ¦|e de¦aJ|¦ H\Y5dd`]WUh]cbK]``Bch7\YW_:cfIdXUhYg
c|o|ce ard C||c| Next ¦o cor¦|rJe
$$
C||c| :]b]g\ ¦o pJb||s| yoJ· app||ca¦|or
/¦¦e· a ¦e. noner¦s. se¦Jp ¦||es .||| be c·ea¦ed a¦ ¦|e ¦o|de· |oca¦|or yoJ se|ec¦ed ea·||e· To ¦es¦ ¦|e |rs¦a|
|a¦|or. doJb|ec||c| ¦|eGYhid"YlY ¦||e DJ·|rg ¦|e |rs¦a||a¦|or. a s|o·¦cJ¦ .||| be added ¦o ¦|e |·og·ans
nerJ To Jr|rs¦a|| ¦|e app||ca¦|or. Jse 5XXCfFYacjYDfc[fUag |r ¦|e Cor¦·o| |are|
Crce ¦|e |rs¦a||a¦|or .o·|s as e.pec¦ed. yoJ car dep|oy yoJ· app||ca¦|or by s|np|y bJ·r|rg ¦|e |rs¦a||a¦|or
¦||es or¦o a CD o· D\D
ClickOnce has more íeaLures, buL Lhis shorL demonsLraLion can geL you sLarLed creaLing
your own insLallaLions.
;^\jgZ."&)
ChU_\f[\aZJ\mTeWÆfY\efgcTZX
During the instaIIation, if you get
an error message that the appIica-
tion vaIidation did not succeed or
you receive another error message,
try re-pubIishing your appIication
and testing again
ABG 8
£very time you pubIish your appIi-
cation, the pubIish version number
{not the appIication version num-
ber) wiII be incremented-that
is, it wiII become version l.0.0.0,
l.0.0.l, and so on.
ABG 8
C09622132.indd 200 10/19/05 10:42:18 AM
Chapter 9. BuiId Your Own weather Tracker AppIication Now! 20l
CongraLulaLions on geLLing Lhis íar! l hope you've learned a loL and had some íun develop-
ing applicaLions using visual 8asic 2005 Fxpress FdiLion. lí you like whaL you've learned (and
l cerLainly hope so), Lhen your educaLion is jusL beginning. 1here's so much more Lo see and
Lry. 1his book provided a small sample oí Lhe Lypes oí applicaLions you can creaLe. My advice
Lo you is Lo conLinue Lhinking oí íun projecLs you can creaLe! ¥ou'll be surprised aL how
much you can accomplish. ln my opinion, developing an applicaLion is one Lhe greaLesL íeel-
ings oí accomplishmenL. People are proud oí Lheir applicaLions and you will be, Loo!
lí you happen Lo creaLe an applicaLion LhaL's useíul Lo you, chances are LhaL iL could be
useíul íor oLhers as well. ln Lhe end, you mighL be helping people by providing Lhem wiLh
Lhe íruiLs oí your labor. ¥ou can also join developmenL projecLs íor íun and help oLhers in
Lhe process while learning a greaL deal. visiL CoLDoLNeL Workspaces (httµ.//www.çctJctnet.
ccm/wcrlsµcces/) íor a sample oí cool projecL examples.
ln SepLember 2005, l sLarLed wriLing a column on MSDN LhaL is devoLed Lo programming
íor íun. 1he column is called Coding4Fun and can be íound aL httµ.//msJn.micrcscft.ccm/
ccJinç4fµn/. ParL oí Lhis column on MSDN perLains Lo improving Lhe WeaLher 1racker appli-
caLion you creaLed here, so be sure Lo look íor Lhis íeaLure. ¥ou can also visiL my blog aL
httµ.//blcçs.msJn.ccm/µµellcnJ/. l haven'L updaLed my blog íor a while because l've been
busy wriLing Lhis book as well as helping Lo ship visual SLudio 2005, buL resL assured LhaL in
Lhe near íuLure, l will have some updaLes (l normally blog aL leasL 4-5 Limes a week). ¥ou can
use my siLe as anoLher locaLion Lo keep iníormed abouL cool sLuíí in Lhe Fxpress producLs
line.
UnLil Lhen, happy developing!
ln Summary...
C09622132.indd 201 10/19/05 10:42:19 AM
C09622132.indd 202 10/19/05 10:42:19 AM
4
argument6kVg^VWaZi]Vi^heVhhZYidVhjWegd\gVb
5
bIack box testing ;jcXi^dcVaiZhi^c\d[VXdbejiZgegd"
\gVbidhZZi]Vi^ieZg[dgbhXdggZXian
breakpoint6eVjhZdghidee^c\eaVXZ^cVegd\gVb!^ciZc"
i^dcVaan^chZgiZYid]Zael^i]YZWj\\^c\
6
cIassI]ZWVh^XWj^aY^c\WadX`d[DDegd\gVbb^c\0^i
YZ[^cZhi]Z[^ZaYh!egdeZgi^Zh!bZi]dYh!VcYZkZcihd[Vc
dW_ZXi
compiIer6XdbejiZgegd\gVbi]ViigVchaViZhi]Z^chigjX"
i^dchlg^iiZc^cdcZXdbejiZgaVc\jV\Z^ciddjieji^c
Vcdi]ZgXdbejiZgaVc\jV\Z0Xdbe^aZghigVchaViZhdjgXZXdYZ
^cidhdbZineZd[bVX]^cZaVc\jV\Zi]ViXVcWZZmZXjiZY
WnVXdbejiZg
consoIe appIication6cVeea^XVi^dci]Vi^hgjc[gdbVXdb"
bVcYegdbeil^i]cdL^cYdlhdgLZW^ciZg[VXZ
context-sensitive menu6bZcji]Viegdk^YZhY^[[ZgZci
X]d^XZhidi]ZjhZgYZeZcY^c\dcl]Zc^i^hVXXZhhZY
controIs8dbedcZcihd[V<gVe]^XVaJhZg>ciZg[VXZ!hjX]Vh
iZmiWdmZhdgWjiidch
7
data member 9ViVZcXVehjaViZYl^i]^cVXaVhhdgdW_ZXi
database6XdaaZXi^dcd[YViVi]Vi^hhidgZY^c[^aZhjh^c\V
hnhiZbVi^XhigjXijgZ
databinding6cZVhnVcYigVcheVgZcilVnidgZVY$lg^iZ
Va^c`WZilZZcVXdcigdadcVL^cYdlh;dgbVcYVYViV
hdjgXZ[gdbndjgVeea^XVi^dc
debugger6XdbejiZgegd\gVbjhZYid[^cYi]ZYZ[ZXih^c
Vcdi]Zgegd\gVb
DLL {Dynamic Link Library)6W^cVgnVeea^XVi^dca^WgVgn
[^aZ[dgbVi^cB^Xgdhd[iL^cYdlh
8
encapsuIation=^YZheg^kViZbZi]dYhd[VXaVhhdgdW_ZXi0
ZchjgZhi]ViVcdW_ZXiXVccdiWZX]Vc\ZY
event6hd[ilVgZbZhhV\Zi]Vi^cY^XViZhhdbZi]^c\]Vh
]VeeZcZY^ci]Zegd\gVb
execution engine9ZkZadebZciidda[dgZmZXji^c\egd"
\gVbh
9
lCL {lramework CIass Libraries)6hZid[egZ"lg^iiZcXdYZ
[dgXdbbdcegd\gVbb^c\iVh`h
;
hyperIink6gZ[ZgZcXZ^cV]neZgiZmiYdXjbZciidVcdi]Zg
YdXjbZcidgadXVi^dc
<
icon6hbVaa^bV\Zdge^XijgZjhZYidgZegZhZciVegd\gVb!
[^aZ!dgdi]ZgdW_ZXi
lD£ {lntegrated DeveIopment £nvironment)8dbejiZg
hd[ilVgZiddahi]Vi]ZaeYZkZadeZghlg^iZXdbejiZgegd"
\gVbh
:_bffTeL
203
Z01622132.indd 203 10/18/05 9:12:41 PM
inheritDW_ZXihVgZXgZViZYi]ViVgZheZX^Va^oZYineZhd[
Zm^hi^c\dW_ZXihVcYXVch]VgZVcYZmiZcYi]Z^gWZ]Vk^dg
l^i]dji]Vk^c\idgZ"^beaZbZci^i
instance6bVc^[ZhiVi^dcd[VXaVhh
=
Jscript6c6Xi^kZHXg^ei^c\:c\^cZ0i]ZB^Xgdhd[ikZgh^dcd[
?VkVHXg^ei
@
methodEgdXZYjgZdg[jcXi^dc0Ve^ZXZd[XdYZVhhdX^ViZY
l^i]VXaVhhdgdW_ZXi
Microsoft .N£T6hd[ilVgZYZkZadebZcieaVi[dgbYZkZa"
deZYWnB^Xgdhd[i
B
override6XaVhhdgdW_ZXibVngZeaVXZVWZ]Vk^dg^i]Vh
^c]Zg^iZY
C
PerI6egd\gVbb^c\aVc\jV\Zi]VihjeedgihWdi]egdXZ"
YjgVaVcYdW_ZXi"dg^ZciZYegd\gVbb^c\
programming Ianguage6bZi]dY[dgegdk^Y^c\^chigjX"
i^dchidVXdbejiZg
property6fjVa^ind[VcdW_ZXi
Python6cdW_ZXi"dg^ZciZYXdbejiZgegd\gVbb^c\aVc"
\jV\Z
E
referenceI]ZVYYgZhhd[i]ZbZbdgnheVXZjhZYidhidgZ
^c[dgbVi^dcVWdjiVkVg^VWaZ
F
Screen TipH]dgi!XdciZmi"hZch^i^kZ^c[dgbVi^dcegdk^YZYVi
i]Zed^cil]ZgZi]ZXjghdg^h]ZaY
spIash screen6c^bV\Zi]ViVeeZVghdci]ZhXgZZcl]^aZ
Vegd\gVb^hadVY^c\0^iegdk^YZh^c[dgbVi^dcidi]ZjhZg
VWdjii]ZadVY^c\egdXZhhVcYY^hVeeZVghdcXZi]Zegd\gVb
^hadVYZY
SOL Server 2005 £xpress £dition6kZgh^dcd[HFAHZgkZg
'%%*YZh^\cZYid]ZaeYZkZadeZghWj^aYVeea^XVi^dchWnegd"
k^Y^c\VedlZg[jaYViVWVhZi]Vi^hVahd[gZZVcYZVhnidjhZ
string6hZfjZcXZd[X]VgVXiZghdgldgYh
G
tooIbar6gdldghZXi^dcd[Xa^X`VWaZ^Xdchi]ViVXi^kViZY^["
[ZgZci[jcXi^dchd[Vegd\gVb
H
user interface {Ul) I]ZbZVchWnl]^X]jhZgh^ciZgVXil^i]
VXdbejiZgegd\gVb
I
variabIe6higjXijgZi]Vi]daYh^c[dgbVi^dciZbedgVg^an[dg
jhZaViZg^cVegd\gVb
VisuaI Basic 2005 £xpress £dition6higZVba^cZYkZgh^dc
d[K^hjVa7Vh^Xi]Viegdk^YZh]dWWn^hih!hijYZcih!VcYcdk^XZh
VcZVhn"id"jhZL^cYdlhegd\gVbb^c\VcYYZkZadebZci
idda
J
windows appIication8dbejiZghd[ilVgZi]Viegdk^YZh
kVg^djh[jcXi^dch[dgi]ZjhZg!hjX]VhldgYegdXZhh^c\!YViV"
WVhZ!dghegZVYh]ZZi
204 Microsoft VisuaI Basic 2005 £xpress £dition. BuiId a Program Now!
Z01622132.indd 204 10/18/05 9:12:41 PM
<aWXk
'%*
Fl`Ub_f
( (leíL parenLhesis), 6í
% wildcard (SOL), í58
. (period), wiLh lnLelliSense, 60-62
- (plus symbol), íor properLies, 49
4
AbouL dialog box, 88-9í, í78-í75
adding Windows íorms, 82-87
ADO.NF1, í46-í60
aligning wiLh snap lines, 58-59
All Lab (lnLelliSense), 62
AlphabeLical buLLon, 49
anLispyware programs, í6
applicaLions
bringing up (splash screens), 82-88,
í78-í75
console vs. Windows applicaLions,
26-27
cusLomizing icons wiLhin, í00-í06
debugging. HZZ debugging
DLLs in, íí0
reíerences Lo, ííí-íí2
selecLing icons íor, 85
sLopping, í70
LiLles, changing, 85
argumenLs íor meLhods, 52
arranging Lables, í4í
ASCll characLers, 27
Assembly lníormaLion... buLLon, 85
auLo-correcLion oí code, 64-66
5
background code, adding supporLing,
í86-í87
background Lasks, períorming, í82-í85
binding navigaLor, í54
binding source, í58
black box LesLing, 98
breakpoinLs, íí2-í28
browser, creaLing (example), 45-55
cusLomizing and personalizing,
8í-í07
building applicaLions. HZZ compiling
8uLLon conLrol, 72
buLLons, icons íor, í00-í06
8yReí keyword, íí8
8yval keyword, íí8
6
cardinaliLy, í4í
caLching excepLions, í2í
Check8ox conLrol, 78
class libraries, í0
ClickOnce deploymenL, í2, í98-200
code
how Lo read, 88
snippeLs (lnLelliSense), íí, 62-64
sLepping Lhrough, íí8
veriíicaLion, adding Lo weaLher
Lracker, í89-í90
Codezone CommuniLy, 86
coding errors, deLecLion oí, 64-66
columns, adding Lo daLabase Lables,
í87
Combo8ox conLrol, 78
commenLs in code, 77
Common Lab (lnLelliSense), 62
common Windows conLrols, lisL oí,
72-74
communiLy access and sLarL page, í2
compiling, 88
componenL Lray, í58-í54
.coníig íiles, í49
connecLion sLrings, í48
console applicaLions, í0
coding (example), 87-4í
geLLing help on. HZZ help
Windows applicaLions vs., 26-27
consLrainLs íor íoreign keys, í82-í88
conLrols, íí. HZZVahd speciíic conLrol by
name
adding Lo íorms, 50
aligning wiLh snap lines, 58-59
common, lisL oí, 72-74
daLabinding, í47
dialog boxes, 88-98, 9í-98, í78-í75
AbouL dialog box, 88-9í
main íorm conLrol, daLa source íor,
í66
properLies íor, 48
renaming, 68-7í
z-order, 95
copyrighL iníormaLion, 85
X1175551Z02.indd 205 10/19/05 2:30:21 PM
'%+ B^Xgdhd[iK^hjVa7Vh^X'%%*:megZhh:Y^i^dc/7j^aYVEgd\gVbCdl
core íuncLionaliLy, weaLher Lracker
applicaLion, í87-í98
correcLing errors, 64-66
black box LesLing, 98
correcLions íor Lhis book, web locaLion,
í68
C1P versions oí visual SLudio 2005, í7
CLrl-Spacebar (lnLelliSense), 60
7
daLa connecLions, í48
daLa inLegriLy and normalizaLion,
í27-í29
daLa managemenL, í25-í6í
daLa members, 52
DaLa Source ConíiguraLion Wizard, í85
daLa source íor main íorm conLrol, í66
DaLa Sources Lab, í48
daLabase diagrams, í89
DaLabase Fxplorer, í86
daLabases, í26-í84
creaLing, í85-í88
creaLing relaLionships beLween Lables,
í89-í42
enLering Lable daLa, í48-í46
inLeracLing wiLh, í88-í84
normalizaLion and inLegriLy, í27-í29
primary and íoreign keys, í80-í88
daLabinding, í47
daLaseLs, creaLing, í48
debugger visualizers, í2, í20
debugging, 64-66, íí0-í28
black box LesLing, 98
delay, splash screen, 88
deleLing breakpoinLs, íí7
deleLing rows íor Lables, í45
design layouL, 47-58
designer. HZZ Windows Form Designer
DesLinaLion Folder, íor insLallaLion, 20
deLecLing errors, 64-66
black box LesLing, 98
dialog boxes, 88-98
AbouL dialog box, 88-9í, í78-í75
NavigaLe dialog box, 9í-98
disabling breakpoinLs, íí7
DLLs (dynamic link libraries), íí0
DocumenL OuLline view, 95
documenLaLion (help)
accessing, 88
Help menu, adding Lo applicaLions,
89
insLalling, 20
Double8uííered properLy, í67
duraLion oí splash screen, 88
8
FdiL and ConLinue íeaLure, í2, íí6
Fnable XP visual sLyles checkbox, 86
encapsulaLion, 52
enLering daLa in daLabase Lables,
í48-í46
error deLecLion and correcLion, 64-66
black box LesLing, 98
evenLs, Lriggering, 75-78
progress bars, 96-99
FxcepLion AssisLanL, íí9
excepLions. HZZVahd debugging
caLching, í2í
unhandled, íí8
Fxpress FdiLions oí visual
SLudio 2005, 9-í2
insLalling, í5-28
9
íilLering, lnLelliSense, 62
Find NexL buLLon, 70
íixing applicaLions. HZZ debugging
íolder íor soíLware insLallaLion, 20
íoreign keys, í80, í82-í88
creaLing relaLionships, í40-í4í
deleLing rows íor Lables, í45
íorm conLrols, íí. HZZVahd speciíic
conLrol by name
adding, 50
aligning wiLh snap lines, 58-59
common, lisL oí, 72-74
daLabinding, í47
dialog boxes, 88-98
properLies íor, 48
renaming, 68-7í
z-order, 95
íorms
adding, 82-87
adding weaLher iníormaLion, í79-í8í
main, íinishing weaLher Lracker,
í90-í92
modal íorms, 9í
noLiíicaLion areas, í67-í70
:;
CeLLing SLarLed secLion, 29
handling excepLions, í2í
headlines, 29
help
accessing, 88
Help menu, adding, 89
insLalling, 20
<
icons
creaLing and desLroying noLiíicaLion,
í87-í89
X1175551Z02.indd 206 10/19/05 2:30:22 PM
>cYZm '%,
cusLomizing wiLhin applicaLions,
í00-í06
noLiíicaLion areas, í67-í70
selecLing íor applicaLions, 85
lDF, 28-8í
componenLs oí, 80-88
cusLomizing, 89-4í
idenLiLy speciíicaLions, í87, í44
lmage properLy (Lool sLrip buLLons), í0í
lmmediaLe window, í28
inheriLance, 6-7
insLalling visual 8asic 2005 Fxpress
FdiLion, í5-28
inLegriLy, daLa, í27-í29, í44
íoreign keys, í82-í88
lnLelliSense, íí, 59-64
lnLelliSense FilLering, 62
inLeracLing wiLh relaLional daLabases,
í88-í84
inLeracLion, use (dialog boxes), 88-98
AbouL dialog box, 88-9í, í78-í75
NavigaLe dialog box, 9í-98
invoking code snippeLs, 68
>?
keys, daLabase, í80-í88
KeyUp evenL, í04
Label conLrol, 72
labels, aligning wiLh snap lines, 58-59
leíL parenLhesis ("("), 6í
LisL8ox conLrol, 74
local daLabase íiles, í52
Local Help, 85
Locals Lab (debugging), íí4-í22
@
main íorm, weaLher Lracker, íinishing,
í90-í92
main íorm conLrol, daLa source
íor, í66
main Loolbar, lDF, 80
managed applicaLions, deíined, 8. HZZ
Vahd applicaLions
managing daLa, í25-í6í
.mdí íile exLension, í85
menu bar, lDF, 80
menus, creaLing, 92-98
MenuSLrip conLrol, 75
MenuSLrip conLrols, 89
MicrosoíL UpdaLe, 22
misLakes in coding, deLecLing, 64-66
modal íorms, 9í
Modiíy ConnecLion... dialog box, í86
MSDN Fxpress Library, insLalling, 20.
HZZVahd help
MSDN Feeds secLion, 80
MSDN Online, 86
mulLiLhreaded programming, í82-í87
MvPs (mosL valuable
proíessionals), 87
My consLrucL, íí
My namespace, 66-68
A
names íor applicaLions, 46
naLural keys, í8í
NavigaLe dialog box, 9í-98
.NF1 Framework, 2-4, 66
new projecLs, sLarLing
console applicaLions, 8í
windows applicaLions, 4í
normalizaLion, daLa, í27-í29
noLiíicaLion areas, í67-í70
noLiíicaLion icon, creaLing and
desLroying, í87-í89
Null, í80
NumericUpDown conLrol, 74
B
objecL-orienLed programming (OOP),
4-9, 69
online help
accessing, 88
Help menu, adding Lo applicaLions,
89
insLalling, 20
OOP (objecL-orienLed programming),
4-9, 69
OpLions dialog box
example, í75-í76
LesLing, í97-í98
working wiLh, í94-í97
overridden meLhods, 6
C
packaging your applicaLion, í98-200
passing by reíerence/value, íí8
percenL (%) wildcard (SOL), í58
period (.) wiLh lnLelliSense, 60-62
plus symbol (-) íor properLies, 49
pre-release versions oí visual 8asic
2005 Fxpress FdiLion, í7
primary keys, í80-í8í
Proíessional value (RenderMode
properLy), 97
progress bars, 96-99
ProjecL Designer, 84
projecLs, 45
adding Web services Lo, í77
new console applicaLions, 8í
X1175551Z02.indd 207 10/19/05 2:30:22 PM
'%- B^Xgdhd[iK^hjVa7Vh^X'%%*:megZhh:Y^i^dc/7j^aYVEgd\gVbCdl
new windows applicaLions, 4í
properLies íor íorm conLrols, 48
publishing your applicaLion, í98-200
DE
queries, í88-í84
OuesLions secLion, 87
RAD (rapid applicaLion developmenL),
57
Radio8uLLon conLrol, 78
reading code, 88
real-Lime error deLecLion, 64-66
RecenL ProjecLs secLion, 29
reíerence.map íile, í79
reíerences Lo applicaLions, ííí-íí2
relaLional daLabases. HZZ daLabases
relaLionships beLween Lables, creaLing,
í89-í42
renaming, 68-7í
buLLons, 87
RenderMode properLy, 97
rows íor Lables, deleLing, í45
running (applicaLions), 58
F
SaveFileDialog conLrol, 76
saving code, 88
Search CriLeria 8uilder, í60
Search íeaLure, 69
SecuriLy CenLer, 22
Show All Files buLLon, 70
Show All SeLLings checkbox, 80
side-by-side execuLions, í6
simple LexL, 27
smarL deíaulLs, í55
smarL Lags, í2, 65
snap lines, 58-59
snippeLs (lnLelliSense), íí, 62-64
SoluLion Fxplorer, 80, 82-87
renaming symbols in, 7í
splash screens, 82-88, í78-í75
spyware prevenLion programs, í6
SOL (SLrucLured Ouery Language),
í88-í84
SLarL Page, lDF, 29
sLarLing applicaLions, 82-88
sLaLus bar, lDF, 8í
sLaLus bars, 96-99
sLepping Lhrough code, íí8
sLopping applicaLions, í70
surrogaLe keys, í80
symbol renaming, 68-7í
buLLons, 87
G
1-SOL (1ransacL-SOL), í57
Lable adapLers, í54
1able Designer, í87-í88
1able LayouL Panel conLrol, 88
1ableAdapLer Ouery ConíiguraLion
Wizard, í57
Lables, daLabase, í26
arranging, í4í
creaLing, í87-í88
creaLing relaLionships beLween,
í89-í42
enLering daLa inLo, í48-í46
normalizaLion and inLegriLy, í27-í29
primary and íoreign keys, í80-í88
querying, í88-í84
LesLing. HZZ debugging
1exLbox conLrol, 72
Lime íor splash screen display, 88
LiLle oí applicaLion, 85
1oggle 8reakpoinL opLion, íí7
Lool sLrip conLainers, 94-96
buLLon icons, í00-í06
Loolbar, lDF, 80
1oolbox, lDF, 80
1oolSLrip conLrol, 75
1ool1ip conLrol, 74
Lracking changes, 60
1ransacL-SOL, í57
Lriggering evenLs, 75-78
progress bars, 96-99
Lyped daLaseLs, í54
H
unhandled excepLions, íí8
uninsLalling previous versions oí
soíLware, í7
UniLed SLaLes weaLher Web
service, í68
user inpuL, validaLing, í94-í96
user inLeracLion (dialog boxes), 88-98
AbouL dialog box, 88-9í, í78-í75
NavigaLe dialog box, 9í-98
user seLLings, creaLing enLries in
weaLher Lracker applicaLion, í8í-
í82
Using sLaLemenLs, íí7
I
validaLing, user inpuL, í94-í96
veriíicaLion code, adding, í89-í90
visibiliLy, panels, 96
visual 8asic 2005 Fxpress FdiLion, 9-í2
insLalling, í5-28
visual 8asic Fxpress Headlines, 29
visual 8asic .NF1, 4-9
visualizers, í20
X1175551Z02.indd 208 10/19/05 2:30:23 PM
>cYZm '%.
J
WaLch Lab (debugging), í28
weaLher icons, adding, í92-í98
weaLher iníormaLion, adding Lo your
íorm, í79-í8í
weaLher Lracker applicaLion (example),
í68-20í
ClickOnce deploymenL, í98-200
compleLing core íuncLionaliLy,
í87-í98
creaLing user seLLings enLries,
í8í-í82
packaging, í98-200
publishing, í98-200
saving seLLings, í96
LesLing, í98, í97-í98
updaLing weaLher daLa, í96
user inLeríace, creaLing, í65-í77
web services wiLh, í77-í98
Web browser, creaLing (example),
45-55
cusLomizing and personalizing,
8í-í07
Web browser conLrols, 50
Web services, í77-í98
wildcard (%), SOL, í58
Windows applicaLions, í0
building (example), 4í-44
console applicaLions vs., 26-27
Windows conLrols, lisL oí, 72-74
Windows Form Designer, íí, 42-48
Windows íorms, adding, 82-87
Windows UpdaLe, 22
KM
XML Web Services, íí
.xsd íiles, í49
z-order, 95
X1175551Z02.indd 209 10/19/05 2:30:23 PM
X1175551Z02.indd 210 10/19/05 2:30:23 PM
X1175551Z02.indd 211 10/19/05 2:30:23 PM
X1175551Z02.indd 212 10/19/05 2:30:24 PM