Unit 4 NET
Unit 4 NET
m
TheMDIst andsforMul tiDocumentI nterf
ace.
I
tallowst oopenmul t
ipledocument s(orforms)inoneappl
i
cation.
Exampl esofMDIappl i
cationsi
ncludeMi cr
osoftWord,Mi
crosoftExcel,and
MicrosoftPowerPoi ntetc.
Multi-
documenti nterfacesconsistofapar entfor
m( al
socall
eda cont
ainer
)
whichcont ai
nsotherf ormsar ecal
ledChi ldForms.
TheMDIpar entf
orm andt heMDIchi ldformstakeonspeci
alfeatur
es:
Allchi
ldformsar edisplay edwi thi
ntheMDIpar ent '
s
client area.
Childformscanbemi nimi zedandt hei
riconwi l
lbedi splayedwi t
hinthe
parent'
scli
entarea.
Childfor
mscanbemaxi mi zedwi t
hintheparent'
scl i
entar ea.
Chi l
dformscannotbedi splay edmodal l
y.
TheMDIf orm canbemi ni mizedandonl yonei conwi llbedi spl
ayedont he
desktoprepresenti
ngt heMDIf orm andallofit
schi l
dren.
I
ftheMDIf or
mi sunloaded, all oftheloadedchildrenwi l
l alsobeunloaded.
Cr
eat
inganMDIPar entForm
Toc reat
eanMDIpar entform,youcansimplyt
akeoneofyourexist
ingf
orms
andseti
tsI
sMDI Contai
nerproper
tyt
oTrue.
Yo umayhaveoneormanycont ai
nerfor
mswi t
hiny
ourappl
icat
ion.
Cr
eat
inganMDIChi
l
dFor
m
AnMDIChi l
dformisanormal f
orml i
keotherf
orms.
Toc r
eateanMDIchildfor
m,y oucansimpl yt
akeoneofy ourexi
sti
ngforms
andsetit
sMDI Par
entpr
opertytothenameofMDIPar entf
orm.
Fol
lowingcodeshowstocreatechil
dfor
m.Thiscodegoesintheparentf
orm,
Pri
vateSubForm1_ Load(
ByVal
senderAsSy
stem.
Obj
ect
,By
Val
eAsSy
stem.
Event
Args)
HandlesMyBase.Load
MDIchi
l
d1.MdiParent=Me
MDIchi
l
d1.Show( )
MDIchi
l
d2.MdiParent=Me
MDIchi
l
d2.Show( )
EndSub
Except
ionHandl
i
ng
Except
ion:
Anexcepti
oni
saner r
orthatari
sesdur
ingtheexecut
ionofapr
ogr
am.Ther
ear
etwoway
s
ofhandl
i
ngexcept
ions(Runti
meError
)invb.net
.
St
ruct
uredErr
orHandl
ing
Unst
ruct
uredErr
orHandl
ing
St
ruct
uredEr
rorHandl
i
ng
Str
uct
uredexcepti
onhandlingt
estsspeci
ficpi
ecesofthecode.I
fanexcept
ionoccur
s,i
t
cal
lsy
ourexcepti
on-handl
i
ngcode.
Str
uct
uredexcept
ionhandli
ngusestheTry
..
.Cat
ch..
.Fi
nal
l
yst
atement.
TheTr
y..
.Cat
ch.
..
Final
l
ybl
ock:
Tr
y..
.Cat
ch.
..
Final
l
yst
ruct
uret
estapi
eceofcode.
TheTr
ybl
ockcont
ainscodewher
eexcept
ionscanoccur
.
TheCat
chbl
ockcont
ainscodet
ohandl
etheexcept
ionst
hatoccur
.
TheFi nal
lyblockisalwaysexecut ed,whetheranerroroccursornot.Youtypicall
y
addaFi nall
yblockt operform anycl eanupoperati
ons,suchascl osi
ngf i
lesor
cl
earingbuf f
ers.
Syntax:
ATr y/Catchblockispl
acedar oundthecodet hatmightgener
ateanexcept
ion.Code
wit
hinaTr y/Cat
chblockisr ef
erredtoaspr otect
edcode,andt hesynt
axforusi ng
Try/
Cat chlooksli
kethefol
lowing:
Tr
y
[tr
yStat
ement
s]
[Exi
tTry]
[Catch[except
ion[Asty
pe]]
[cat
chStatements]
[Exi
tTry]]
[Cat
ch.
..]
[Final
ly
[fi
nall
ySt
atement
s]]
EndTr
y
Example:
Here’
sanexampl e,
i
nthiscase,t
heexcepti
on-
pronecodeexecuat
esadivi
sionbyzer
o,which
generat
esanarit
hmeticoverf
lowexcepti
on.Here,Sensi
ti
vecodeisputi
nt het
rybl
ock,and
theexcept
ionhandl
ingcodeinthecat
chblock.
Modul
eexcept
ionPr
og
SubMai n()
Dimr esultAsI
nteger
Try
result=num1\num2
CatcheAsExcept ion
Console.Wri
teLi
ne("
Excepti
on:Ar
it
hmeti
cOverf
low”)
Fi
nally
Console.Wri
teLi
ne("
Execuati
onofsensi
ti
veCodeiscompl
ete”
)
EndTr y
EndSub
EndModul
e
Whent
heabov
ecodei
scompi
l
edandexecut
ed,
itpr
oducest
hef
oll
owi
ngr
esul
t:
Excepti
on:Ar
it
hmeti
cOverf
low
Execuati
onofsensi
ti
veCodeiscompl
ete
TheExcept
ionobjectprovi
desi nformat
ionaboutanyencounteredexcepti
on.Whenev
eran
excepti
oni
sthrown,thepr operti
esoftheErrobjectareset.Thesepropert
iesar
eusedto
deter
minet
hecodel ocat
ion,type,andcauseoftheexcept
ion.
Usi
ngMul
ti
pleCat
chSt
atement
Youcanusemulti
plecatchst
atement
.Here’
sexampl ethatspeci
fi
cal
l
yhandl
esov
erf
low,
i
nval
i
dargument
,andargumentoutofr
angeexcept
ions.
Modul
eexcept
ionPr
og
SubMai
n()
Try
resul
t=num1\num2
CatcheAsSy stem.Overf
lowExcept
ion
Console.
Writ
eLine(
"Except
ion:
Ari
thmet
icOv
erf
low”
)
Cat
cheAsSy
stem.
Argument
Except
ion
Consol
e.Wr
it
eLi
ne(
"Except
ion:
Inv
ali
dar
gumentv
alue”
)
Fi
nall
y
Consol
e.Wr
it
eLi
ne(
"Execuat
ionofsensi
ti
veCodei
scompl
ete”
)
EndTry
EndSub
EndModul
e
Unst
ruct
uredEr
rorHandl
i
ng
Unstr
ucturedexcepti
onhandlingrev ol
vesaroundt heOnEr r
orGoTostat
ement.
TheOnEr rorGoTostatementenabl esexceptionhandlingandspeci
fi
esthel
ocat
ion
oftheexcepti
on-handli
ngcodewi thinaprocedur e.
I
tneedst oplaceanOnEr r
orGoToatt hebegi nningofablockofcodet
ohandles
anyerr
orsoccurringwithi
nthatblock.
Here'
showt heOnEr rorGoTost at
ementwor ks:
OnEr
ror{GoTo[l
i
ne|
0|-
1]|
ResumeNext}
Her
ear
ethepar
tsoft
hisst
atement
:
OnEr rorGoToli
ne—speci fiesthatwhenanexcept ionoccur s,program
executiongoestothel i
near gumentspecifi
edi nt
heOnEr rorstatement.
Thelineargument,whichi sal i
nenumberorl inel
abel, i
ndicatest he
exceptionhandl
erlocation.
OnEr rorGoTo0—Di sabl esenabl edexceptionhandlerint hecur r
ent
procedureandresetsittoNot hing.
OnEr rorGoTo-1—SameasGoTo0.
OnEr rorResumeNext —speci fi
esthatwhenanexcept i
onoccur s,
executionski
psoverthest atementt hatcausedthepr oblem andgoest o
thestatementimmedi atelyf ol
lowing.Executi
oncont i
nuesf r
om t hatpoi
nt.
OnEr
rorGoTol
i
ne
Here'
sanexampl
eshowinghowt
ousetheOnEr
rorGoTol
i
net
hatusesadi
vi
sionby
zer
otocreat
eanover
fl
owexcept
ion.
Modul
eModul
e1
SubMai
n()
Di
mint
1=0,
int
2=1,
int
3AsI
nteger
OnEr
rorGot
oHandl
er
i
nt3=i
nt2/i
nt1
Exi
tSub
Handl
er:
Sy
stem.
Consol
e.Wr
it
eLi
ne(
"Ov
erf
lower
ror
!"
)
EndSub
EndModul
e
Whent
heabov
ecodei
scompi
l
edandexecut
ed,
itpr
oducest
hef
oll
owi
ngr
esul
t:
“
Over
fl
ower
ror
!"
.
Youcanalsohandl
especi
fi
cexceptionsi
ndiff
erentway sdependingwhichexcept
ion
occur
redbychecki
ngtheErrobj
ect'
sNumberpr opert
y,whichholdstheexcepti
on'
snumber
.
Forhandl
i
ngonlyari
thmet
icoverf
lowexcept
ions,it
’sexcepti
onnumberis6.
Modul
eModul
e1
SubMai
n()
Di
mint
1=0,
int
2=1,
int
3AsI
nteger
OnEr
rorGot
oHandl
er
i
nt3=i
nt2/i
nt1
Exi
tSub
Handl
er:
I
f(Er
r.
Number=6)Then
Sy
stem.
Consol
e.Wr
it
eLi
ne(
"Ov
erf
lower
ror
!"
)
EndI
f
EndSub
EndModul
e
OnEr
rorGoTo0/OnEr
rorGoTo-
1
Totur
noffunst
ruct
uredexcept
ionhandl
i
ng,
youcanuset
heOnEr
rorGoTo0orOn
Er
rorGoTo-1.
Modul
eModul
e1
SubMai
n()
Di
mint
1=0,
int
2=1,
int
3AsI
nteger
OnEr
rorGot
oHandl
er
i
nt3=i
nt2/i
nt1
OnEr
rorGot
o0
Exi
tSub
Handl
er:
Sy
stem.
Consol
e.Wr
it
eLi
ne(
"Ov
erf
lower
ror
!"
)
EndSub
EndModul
e
OnErr
orResumeStatement
Oneofthemostusef
ulunst
ruct
uredexcepti
onhandli
ngistheResumeStatement
,
whi
chletsy
ouresumeprogram execut
ionevenaf
terexcept
ionhasoccur
red.
Her
ear
ethepossi
bil
i
ties:
Resumer
esumesexecut
ionwi
tht
hest
atementt
hatcausedt
heer
ror
.
ResumeNextst
atementspeci
fi
est
hati
ntheev
entofar
un-
ti
meer
ror
,cont
rol
passestot
hest
atementi
mmedi
atel
yfol
l
owi
ngt
heonei
nwhi
cht
heer
ror
occurr
ed.
Resumeli
neresumesexecut
ionatl
i
ne,
ali
nenumberorl
abel
thatspeci
fi
es
wheret
oresumeexecuti
on.
Modul
eModul
e1
SubMai
n()
OnEr
rorResumenext
Di
mint
1=0,
int
2=1,
int
3AsI
nteger
i
nt3=i
nt2/i
nt1
Exi
tSub ‘
ResumeExecut
ionf
rom t
hisl
i
ne
EndSub
EndModul
e
Whent heabov
ecodeiscompi
l
edandexecuted,i
tresumeexecut
ionfr
om nextl
i
ne
ofexcept
ionoccur
.Inabov
eexampl
e,executi
onwi llber
esumedf r
om “Exi
tSub
st
atement”.
CommonExampl
ethati
ncl
udesal
lty
pesofexcept
ionhandl
i
ngt
echni
ques.
Publ
i
cSubf
nEr
ror
s()
OnEr
rorGoToUnst
ruct
ured'
Enabl
eer
rorhandl
er
Di
m Resul
tAsI
nteger
Di
m Val
ue1AsI
nteger=9
Di
m Val
ue2AsI
nteger=0
OnEr
rorGoTo0'
Disabl
est
heer
rorhandl
er
'
Mov
esexecut
iont
othel
i
nef
oll
owi
ngt
hel
i
net
hatcausedt
heer
ror
.
OnEr
rorResumeNext
Resul
t=Val
ue1/Val
ue2'
Div
isi
onbyzer
o,causeanov
erf
lower
ror
.
'
Cat
cht
heov
erf
lower
rorcausedbydi
vi
dingbyzer
o.
I
fEr
r.
Number=6Then
MessageBox.
Show(
"Er
rorNumber
:"&Er
r.
Number
.ToSt
ri
ng)
Er
r.
Clear
()'
ClearEr
ror
s
EndI
f
Exi
tSub
Unst
ruct
ured:'
Locat
ionofer
rorhandl
er
Sel
ectCaseEr
r.
Number
Case6
'
Displ
ayt
heer
rornumber
.
MessageBox.
Show(
"Di
vi
dedbyzer
o")
CaseEl
se
'
Cat
chal
lot
hert
ypeofer
ror
s.
MessageBox.
Show(
Err
.Descr
ipt
ion)
EndSel
ect
'
Resumeexecut
iont
othel
i
nef
oll
owi
ngt
hel
i
net
hatcausedt
heer
ror
.
ResumeNext
EndSub
VB.
NETSub-
procedur
esandFunct
ions
Pr
ocedur
e
A procedureisagr oupofstatementsthatt
ogetherperfor
m aspeci
fi
ct ask.Aft
erthe
procedureisexecuted,t
hecont
rolret
urnst
othestatementcal
li
ngt
hecode.VB.Nethast
wo
ty
pesofpr ocedures:
Subprocedures(
SubProcedur
edoesnotr
etur
nav
alue.
)
Funct
ions(Funct
ionsr
etur
nav al
ue)
SubPr
ocedur
e
TheSubpr ocedur
eperfor
msat askandt henretur
nscontroltothecall
ingcode,butitdoes
notret
urnav al
uetothecall
i
ngcode.
ASubpr ocedureisaseriesofVi sualBasi
cst at
ementsenclosedbyt heSubandEndSub
stat
ements.
Eachtimet heprocedureiscal l
ed,i t
sst at
ementsar eexecuted,start
ing withthef i
rst
stat
ementoft heSubpr ocedureandendi ngwi t
hthef i
rstEndSub,Exi tSub,orRet urn
stat
ementencounter
ed.
Youcandef i
neaSubpr ocedureinmodul es,cl
asses,andstruct
ures.Bydef aul
t,i
tisPubli
c,
whichmeansy oucancalli
tfrom anywhereintheappli
cat
ion.
Def
ini
ngSubPr
ocedur
e
Thesy
ntaxf
ordecl
ari
ngsubpr
ocedur
eisasf
oll
ows:
[
Modi f
ier
s]SubSubName[
(Par
amet
erLi
st)
]
[
Statements]
EndSub
Wher
e,
Modifi
ers: possi
ble val
ues ar
e: Publi
c,Pr i
vat
e,Pr ot
ect
ed,Fri
end,
Prot
ected Friend and i
nfor
mati
on regardi
ng over
loadi
ng,ov
err
idi
ng,
shar
ing,andshadowing.
SubName:
indi
cat
est
henameoft
heSub
Par
amet
erLi
st:
speci
fi
est
hel
i
stoft
hepar
amet
ers
Cal
l
ingSubPr
ocedur
e
YouinvokeaSubpr ocedureexpli
cit
lywit
hast and-
alonecall
ingstat
ement
.
YoucancallaSubmet hodfrom out
sidethecl
assthatdefinesit
.
Thesyntaxf
orcalli
ngaSubpr ocedureisasfoll
ows:
[
Cal
l]subname[ (argumentli
st)]
TheuseoftheCal l
keywordisoptional
.
ASubpr ocedurecant akearguments,suchasconstant
s,var
iabl
es,orexpr
essi
ons,whi
ch
arepassedtoitbythecall
ingcode.
Youmustpr ov
idevaluesforall
argumentsthatar
enotopti
onal
.
Exampl
e
Wi
thoutPassi
ngAr
gument
stoPr
ocedur
e
Publ
i
cCl
assFor
m1
Pri
vateSubForm1_Load(
ByVal
senderAsSy
stem.
Obj
ect
,By
Val
eAs Sy
stem.
Event
Args)
HandlesMy Base.
Load
I
ncrement()
EndSub
Pri
vat
eSubIncrement(
)
Di
m aAsInteger=10
a=a+1
MsgBox(
a)
EndSub
EndCl
ass
Passi
ngAr
gument
stoPr
ocedur
e
Publ
i
cCl
assFor
m1
Pri
vateSubFor m1_Load(
ByVal
senderAsSy
stem.
Obj
ect
,By
Val
eAsSy
stem.
Event
Args)
HandlesMy Base.Load
I
ncrement(10)
EndSub
Pri
vat
eSubIncr
ement
(By
Val
aAsI
nteger
)
a=a+1
MsgBox(
a)
EndSub
EndCl
ass
Funct
ion
TheFunctionprocedur eperformsat askandt henret
ur nscontr
olt othecalli
ngcode.Unlike
SubProcedure,itretur
nsav aluetothecal l
ingcode.
AFunctionprocedur eisaser iesofVisualBasi cstatementsencl osedbyt heFunct i
onand
EndFuncti
onst atement s.
Eachtimet hepr ocedureiscal l
ed,itsst atementsar eexecuted,start
ing withthef i
rst
stat
ementoft heFunct ionandendi ngwi ththefir
stEndFunct ion,ExitFunction,orReturn
stat
ementencount ered.
Youcandef i
neaFunct ioninmodules, classes,andstructur
es.Bydef aul
t,iti
sPubl i
c,which
meansy oucancal litf
rom anywhereint heappl i
cat
ion.
Def
ini
ngFunct
ion
Thesy
ntaxf
ordecl
ari
ngf
unct
ioni
sasf
oll
ows:
[
Modi f
ier
s]Functi
onFunct
ionName[
(Par
amet
erLi
st)
]AsRet
urnTy
pe
[
Statements]
EndFunction
Wher
e,
Modifier
s:speci f
yt heaccessl evelofthefunct i
on;possibl
ev al
uesare:Publ
ic,
Pri
vate,Pr otected,Fr i
end,Pr otected Friend and i nfor
mat i
on regardi
ng
overl
oading,over r
iding,shari
ng,andshadowi ng.
Functi
onName: indicatesthenameoft hefunct i
on
ParameterList:specifi
est heli
stoftheparamet er
s
Retur
nTy pe:specifiesthedatat ypeofthev ariabl
ethefunctionret
urns
Cal
l
ingFunct
ion
YouinvokeaFunct i
onProcedureexplici
tl
ywi t
hast and-
alonecal
l
ingst
atement
.
Youcancal l
aFunct i
onfrom outsi
dethecl assthatdefi
nesit
.
Thesyntaxforcall
ingaf unct
ionisasf ol
lows:
Returnv
alue=Functi
onname[ (argumentli
st)]
Af unct
ioncant akear guments,suchasconst
ant
s,var
iabl
es,orexpr
essi
ons,whi
char
e
passedtoitbythecall
ingcode.
Youmustpr ovi
dev al
uesf oral
lar
gumentst
hatar
enotopt
ional
.
Exampl
e
Wi
thoutPassi
ngAr
gument
stof
unct
ion
Publ
i
cCl
assFor
m1
Pri
vat
eSubFor m1_Load(
ByVal
senderAsSy
stem.
Obj
ect
,By
Val
eAs Sy
stem.
Event
Args)
HandlesMy Base.Load
Dim ansAsI nteger
ans=Increment(
)
Msgbox (ans)
EndSub
Pri
vateFuncti
onIncrement
()AsI
nteger
Dim aAsInteger=10
a=a+1
Retur
n(a)
EndFuncti
on
EndCl
ass
Passi
ngAr
gument
stof
unct
ion
Publ
i
cCl
assFor
m1
Pri
vat
eSubFor m1_Load(
ByVal
senderAsSy
stem.
Obj
ect
,By
Val
eAs Sy
stem.
Event
Args)
HandlesMy Base.Load
Dim ansAsI nteger
ans=Increment(
10)
Msgbox (ans)
EndSub
Pri
vat
eFuncti
onI
ncr
ement
(By
Val
aasI
nteger
)AsI
nteger
a=a+1
Ret
urn(
a)
EndFunct
ion
EndCl
ass
Ret
urni
ngv
alue
Theval
ueaFunct
ionpr
ocedur
esendsbackt
othecal
l
ingcodei
scal
l
edi
tsr
etur
n
val
ue.
Thepr
ocedur
eretur
nsthi
sval
ueinoneoft
woway s:
1).Byusi
ngther
etur
nstat
ement
2).Byassi
gni
ngtheval
uetot
hefuncti
onname
1)
.Byusingther
etur
nstatement
I
tusestheRetur
nstat
ementtospecif
yther
etur
nval
ue,
andr
etur
nscont
rol
i
mmediatel
ytothecal
li
ngprogram.
Thefol
l
owingexampleil
l
ustr
atesthi
s.
Funct
ionFuncti
onName[
(Par
amet
erLi
st)
]AsRet
urnTy
pe
Ret
urnExpressi
on
EndFuncti
on
2).Byassigningthevaluetothefuncti
onname
Itassignsav aluetoitsownfuncti
onnamei noneormor est
atementsoft
he
procedur e.
Cont rol
doesnotr eturntothecall
i
ngpr ogr
am unt
ilanExi
tFunct
ionorEndFunct
ion
statementi sexecuted.
Thef ol
lowingexampl eil
l
ustrat
esthis.
Functi
onFunctionName[
(Parameter
List
)]AsRet
urnTy
pe
Funct
ionName=Expression
EndFuncti
on
Exi
tst
atement
Exi
tstatementexi
tsaprocedur
eorbl ockandt
ransf
erscont
rol
immedi
atel
ytot
hest
atement
fol
lowingthepr
ocedur
ecallorfunct
ioncall
.
Syntax:
Exi
t{Sub|
Funct
ion}
Exi
tFunctioncanbeusedonl yi
nsideaFunct
ionpr
ocedur
eandExi
tSubcanbeusedonl
y
i
nsideaSubpr ocedure.
I
nf unct
ion,t
ospecifyaret
urnvalue,y
oucanassi
gntheval
uet
othefunct
ionnameonali
ne
beforet
heExitFunctionst
atement.
Example:
ForSubProcedure:
Priv
ateSubexi
tNow(
)
ExitSub
EndSub
ForFunct
ion:
Pri
vat
ef uncti
onexit
Now()
Dim aasi nt
eger
=10
Return(a)
Exi
tfuncti
on
EndSub
Passi
ngPar
amet
ers.
Youdeclar
eeachpr ocedureparametersimil
arl
ytohowyoudecl
areav
ari
abl
e,
speci
fyi
ngt heparamet ernameanddat atype.
Thesyntaxf oreachparameterintheparameterli
sti
sasfol
l
ows:
[Opt
ional
][By Val|ByRef][
ParamArray]par
ameternameAsdatat
ype
I
ftheparameteri
soptional
,youmustal
sosuppl
yadefaul
tval
ueaspar
tofi
ts
decl
arat
ion.Thesynt
axforspeci
fyi
ngadef
aultv
aluei
sasfoll
ows:
Opt
ional
[By
Val
|By
Ref
]par
amet
ernameAsdat
aty
pe=def
aul
tval
ue
Ar
gument
syoucanpassi
npr
ocedur
etwoWay
s:
Passi
ngPar
amet
ersbyv
alue
Passi
ngPar
amet
ersbyr
efer
ence
PassingParameter
sbyVal ue
Thisi
st hedef
aultmechanism f orpassi
ngpar ameterstoamet hod.
Inthismechanism,whenamet hodiscall
ed,anewst or
agelocati
oni screat
edfor
eachv al
ueparameter.Thevaluesoft heactualparametersarecopiedintothem.So,
thechangesmadet ot heparamet eri
nsidethemet hodhavenoef fectont he
argument.
InVB.Net,y
oudeclaret her
eferenceparamet ersusingtheByValkey word.The
f
oll
owi
ngexampl
edemonst
rat
est
heconcept
:
Publ
i
cCl
assFor
m1
Pri
vateSubFor m1_ Load(
ByVal
senderAsSy
stem.
Obj
ect
,By
Val
eAsSy
stem.
Event
Args)
HandlesMy Base.Load
Dim aasinteger=1
I
ncrement(a)
MsgBox(a)
EndSub
Pri
vat
esubI
ncr
ement
(By
Val
aAsI
nteger
)AsI
nteger
a=a+1
Endsub
EndCl
ass
Whent
heabov
ecodei
scompi
l
edandexecut
ed,
itpr
oducest
hef
oll
owi
ngr
esul
t:
Themessageboxpr
int
s1.
Publ
i
cCl
assFor
m1
Pri
vateSubFor m1_ Load(
ByVal
senderAsSy
stem.
Obj
ect
,By
Val
eAsSy
stem.
Event
Args)
HandlesMy Base.Load
Dim aasinteger=1
I
ncrement(a)
MsgBox(a)
EndSub
Pri
vat
esubI
ncr
ement
(By
RefaAsI
nteger
)AsI
nteger
a=a+1
Endsub
EndCl
ass
Whent
heabov
ecodei
scompi
l
edandexecut
ed,
itpr
oducest
hef
oll
owi
ngr
esul
t:
Themessageboxpr
int
s2.
CommonDi
alogBox
Ther
earemanybuil
t-
indi
alogboxestobeusedinWindowsformsf
orvar
ioust
asks
l
ikeopeni
ngandsavingfi
les,pr
int
ingapage,prov
idi
ngchoicesf
orcol
ors,f
onts,
pageset
up,et
c.,
Vb.
netsuppor
tsf
oll
owi
ngt
hebui
l
t-i
ndi
alogbox:
Col
ordi
alogbox,
f
ontDi
alogBox
,open
Fi
l
eDi
alogBox,
Sav
eFi
l
edi
alogBox,
Pr
intDi
alogbox.
TheShowDial
ogmet hodi
susedtodi
spl
ayal
lthedi
alogboxcont
rol
satr
un-
ti
me.
Thefol
l
owingdiagr
am showsthecommondi
alogcl
assinher
it
ance:
ToAddDialogBoxintoAppl
icati
on,
DragandDr opthepar
ti
cul
arDial
ogContr
olfr
om
Di
alogcat
egaroyoftheToolBoxont heform.ex.
ToaddColorDial
ogBox,Dragand
dr
optheColorDial
ogContr
olfrom t
oolboxontheform.
Col
orDi
alogBox
TheCol
orDi
alogcont
rol
all
owst
heusert
osel
ectcol
or.
Fol
l
owi
ngi
stheCol
ordi
alogbox:
Pr
oper
ti
esoft
heCol
orDi
alogCont
rol
S.
Pr
oper
ty Descr
ipt
ion
N
I
fwesettr
uet
hendi
alogboxshowscust
om col
or
1 Al
l
owFul
l
Open
but
ton.
2 Col
or I
tgi
vest
hecol
orsel
ect
edbyt
heuser
.
I
fwesettruethenthecustom col
orsect
ionoft
he
3 Ful
l
Open
di
alogboxisi
niti
all
ydi
splayed.
4 ShowHel
p I
fwesett
ruet
hendi
alogboxshowshel
pbut
ton.
Met
hodsoft
heCol
orDi
alogCont
rol
:
S.
Met
hodName&Descr
ipt
ion
N
ShowDial
og
1
I
tisusedtodi
spl
aydi
alogbox.
Exampl
e
Pr
ivat
eSubBut
ton1_
Cli
ck(
senderAsObj
ect
,eAsEv
ent
Args)
Handl
esBut
ton1.
Cli
ck
I
fCol
orDi
alog1.
ShowDi
alog<>Wi
ndows.
For
ms.
Dial
ogResul
t.
Cancel
Then
Label
1.For
eCol
or=Col
orDi
alog1.
Col
or
EndI
f
EndSub
FontDi
alogBox:
FontDi
alogsal
l
owst
heusert
osel
ectaf
ontsi
ze,
Fontt
ype,
fontCol
or,
etc.
Fol
l
owi
ngi
stheFontdi
alogbox:
Pr
oper
ti
esoft
heFont
Dial
ogCont
rol
S.
N Pr
oper
ty Descr
ipt
ion
1 Col
or I
tgi
vest
hesel
ect
edf
ontcol
or.
2 Font I
tgi
vest
hesel
ect
edf
ont
.
Ifwesett
ruet
hendi
alogboxdi
spl
ayst
hecol
or
3 ShowCol
or
choi
ce.
Ifwesett
ruet
hent hedial
ogboxal
l
owt
heusert
o
4 ShowEf
fect
s
speci
fyst
ri
ket
hrough,underl
i
ne.
I
fwesettr
uet
hent
hedi
alogboxshowsAppl
y
5 ShowAppl
y
but
ton.
I
fwesettr
uet
hent
hedi
alogboxshowsHel
p
6 ShowHel
p
but
ton.
Met
hodsoft
heFont
Dial
ogCont
rol
:
S.
N Met
hodName&Descr
ipt
ion
ShowDial
og
1
I
tisusedtodi
spl
aydi
alogbox.
Ev
ent
soft
heFont
Dial
ogCont
rol
:
S.
N Ev
ent Descr
ipt
ion
Occurswhent
heAppl
ybut
tonont
hef
ontdi
alogbox
1 Appl
y
i
sclicked.
Exampl
e:
Pr
ivat
eSubBut
ton1_
Cli
ck(
senderAsObj
ect
,eAsEv
ent
Args)
Handl
esBut
ton1.
Cli
ck
I
fFont
Dial
og1.
ShowDi
alog<>Wi
ndows.
For
ms.
Dial
ogResul
t.
Cancel
Then
Text
Box1.
ForeCol
or=FontDialog1.
Col
or
Text
Box1.
Font=FontDi
alog1.Font
EndI
f
EndSub
OpenFi
l
eDi
alogBox
TheOpenFi
l
eDi
alogcont
rol
all
owst
heusert
osel
ectf
il
etoopen.
Fol
l
owi
ngi
stheOpenFi
l
edi
alogbox:
Pr
oper
ti
esoft
heOpenFi
l
eDi
alogCont
rol
:
S.
N Pr
oper
ty Descr
ipt
ion
Ifwesett
ruethendi
alogboxaut
omat
ical
l
y
1 AddExt
ensi
on
addsanextensi
ontofi
lenames.
I
fwesettr
uethenthedial
ogboxdi
spl
ays
2 CheckFi
l
eExi
sts war
ningi
ftheuserspeci
fi
esafi
l
ename
t
hatdoesnotexi
st.
I
fwesett r
uethenthedial
ogboxdispl
ays
3 CheckPat
hExi
sts warni
ngiftheuserspecif
iesapat
hthat
doesnotexist
.
I
tgivesnameoffi
lewhichi
ssel
ect
edby
4 Fi
l
eName
userinf
il
edi
alogbox.
5 Fi
l
ter I
tisusedt
ofi
l
ter
sfi
l
es.
I
fwesettr
uethent
hedi
alogboxshows
6 ShowHel
p
Hel
pbut
ton.
Met
hodsoft
heOpenFi
l
eDi
alogCont
rol
:
S.
N Met
hodName&Descr
ipt
ion
OpenFi
l
e
1
Opensthef
il
esel
ect
edbyt
heuser
,wi
thr
ead-
onl
yper
missi
on.
ShowDial
og
2
I
tisusedtodi
spl
aydi
alogbox.
Exampl
e:
Pr
ivat
eSubBut
ton1_
Cli
ck(
senderAsObj
ect
,eAsEv
ent
Args)
Handl
esBut
ton1.
Cli
ck
I
fOpenFi
l
eDi
alog1.
ShowDi
alog<>Wi
ndows.
For
ms.
Dial
ogResul
t.
Cancel
Then
Pi
ctur
eBox1.
Image=I
mage.
FromFi
l
e(OpenFi
l
eDi
alog1.
Fil
eName)
EndI
f
EndSub
Sav
eFi
l
eDi
alogBox
TheSav
eFi
leDi
alogcont
rolal
l
owst
heusert
osel
ectl
ocat
ionandspeci
fi
est
hename
off
il
etosavedata.
Fol
l
owi
ngi
stheSav
eFi
l
edi
alogbox:
Pr
oper
ti
esoft
heSav
eFi
l
eDi
alogCont
rol
:
S.
N Pr
oper
ty Descr
ipt
ion
Ifwesett
ruethendi
alogboxaut
omat
ical
l
y
1 AddExt
ensi
on
addsanextensi
ontofi
lenames.
I
fwesettr
uethenthedial
ogboxdi
spl
ays
2 CheckFi
l
eExi
sts war
ningi
ftheuserspeci
fi
esafi
l
ename
t
hatdoesnotexi
st.
I
fwesett r
uethenthedial
ogboxdi
spl
ays
3 CheckPat
hExi
sts warni
ngiftheuserspeci
fi
esapat
hthat
doesnotexist
.
I
tgivesnameoffi
lewhichi
ssel
ect
edby
4 Fi
l
eName
userinf
il
edi
alogbox.
5 Fi
l
ter I
tisusedt
ofi
l
ter
sfi
l
es.
I
fwesettr
uethent
hedi
alogboxshows
6 ShowHel
p
Hel
pbut
ton.
Met
hodsoft
heSav
eFi
l
eDi
alogCont
rol
:
S.
N Met
hodName&Descr
ipt
ion
1 OpenFi
l
e
Openst
hef
il
esel
ect
edbyt
heuser
,wi
thr
ead-
onl
yper
missi
on.
ShowDial
og
2
I
tisusedtodi
spl
aydi
alogbox.
Exampl
e:
Pr
ivat
eSubBut
ton1_
Cli
ck(
senderAsObj
ect
,eAsEv
ent
Args)
Handl
esBut
ton1.
Cli
ck
Sav
eFi
l
eDi
alog1.
Fil
ter="
TXTFi
l
es(
*.t
xt*
)|*
.txt
"
I
fSav
eFi
l
eDi
alog1.
ShowDi
alog<>Wi
ndows.
For
ms.
Dial
ogResul
t.
Cancl
eThen
My
.Comput
er.
Fil
eSy
stem.
Wri
teAl
l
Text
(Sav
eFi
l
eDi
alog1.
Fil
eName,
Text
Box1.
Text
,Tr
ue)
EndI
f
EndSub
Pr
int
Dial
ogBox
ThePr
int
Dial
ogcont
rol
all
owst
heusert
opr
intdocument
sbysel
ect
ingapr
int
er.
Fol
l
owi
ngcont
rol
isusedf
orpr
int
ing:
ThePri
ntDocumentcont
rol-itpr
ovi
dessuppor
tforact
ual
oper
ati
onsofpr
int
ingand
set
stheproper
ti
esforpr
int
ing.
Fol
l
owi
ngi
sthePr
intdi
alogbox:
Pr
oper
ti
esoft
hePr
int
Dial
ogCont
rol
:
S.
N Pr
oper
ty Descr
ipt
ion
I
fwesettr
uet
hent
heCur
rentPageopt
ionbut
toni
s
1 Al
l
owCur
rent
Page
di
spl
ayed.
Ifwesett
ruet
hent
hePr
intt
ofi
l
echeckboxi
s
2 Al
l
owPr
int
ToFi
l
e
enabl
ed.
Ifwesett
ruet
hent
heSel
ect
ionopt
ionbut
toni
s
3 Al
l
owSel
ect
ion
enabl
ed.
Ifwesett
ruet
hent
hePagesopt
ionbut
toni
s
4 Al
l
owSomePages
enabl
ed.
I
fwesettruet hent
hePr
int
Documentusedt
oobt
ain
5 Document
Pr
int
erSet
tings.
I
fwesett r
uet
hent
hepr
int
erset
ti
ngsmodi
fi
est
he
6 Pr
int
erSet
ti
ngs
di
alogbox.
7 ShowHel
p I
fwesett
ruet
hent
hedi
alogboxshowsHel
pbut
ton.
Met
hodsoft
hePr
int
Dial
ogCont
rol
:
S.
N Met
hodName&Descr
ipt
ion
ShowDial
og
1
I
tisusedtodi
spl
aydi
alogbox.
Exampl
e:
Pr
ivat
eSubBut
ton1_
Cli
ck(
senderAsObj
ect
,eAsEv
ent
Args)
Handl
esBut
ton1.
Cli
ck
Pr
int
dial
og1.
document
=Pr
int
Document
1
I
fPri
ntDi
alog1.ShowDi
alog=Dial
ogResul
t.
OKThen
Print
Document1.Pr
int
()
EndI
f
Pr
ivat
eSubPr
int
Document1_Pri
ntPage(ByValsenderAsSystem.Obj
ect,
ByValeAs
Sy
stem.Dr
awi
ng.
Pri
nti
ng.
Print
PageEv entAr
gs)HandlesPri
ntDocument1.
Pri
ntPage
e.
Graphi
cs.
DrawSt
ri
ng(
Text
Box1.
Text
,Text
Box1.
Font
,Br
ushes.
Black,
100,
100)
EndSub
EndSub
Desi
gnaf
ormt
hati
ncl
udesal
lDi
alogBox.
Publi
cClassForm1
Di
m pat hAsStri
ng
-
--
--
---
--
---
--
--
--
--
--
--
---
--
--
---
--
--
--
---
--
--
--
--
---
--
--
--
--
--
--
--
--
---
--
--
--
--
--
--
---
--
---
--
--
--
--
--
--
--
--
--
Pri
vateSubBtnOpen_ Cl
ick(ByValsenderAsSystem.Object,ByVale
AsSystem.EventArgs)HandlesBtnOpen.Cli
ck
Di
mr esultAsDialogResult=OpenFi l
eDial
og1.ShowDialog()
Ifresult=Wi ndows. For ms. Di alogResul t.
OKThen
path=OpenFi l
eDi alog1. Fil
eName
Label 1.Text=pat h
RichText Box1. LoadFi le( path, Ri chText Box StreamTy pe. PlainText)
EndI f
EndSub
--
---
---
---
-----
----
---
-----
---
---------
----
-----
-------
--
----
---
---
----
---
--
---
-----
--
---
----
---
--
--
--
--
--
--
--
-
PrivateSubBt nsav e_ Cl ick( ByVal senderAsSy stem. Obj ect,By Vale
AsSy stem. Ev ent Args)Handl esBt nsav e.Click
RichText Box1. Sav eFi l
e( pat h, RichText BoxSt reamTy pe. PlainText)
EndSub
--
---
---
---
-----
----
---
-----
---
---------
----
-----
-------
--
----
---
---
----
---
--
---
-----
--
---
----
---
--
--
--
--
--
--
--
-
PrivateSubBt nImage_ Cl ick(By Val senderAsSy stem. Obj ect ,ByVale
AsSy stem. Ev ent Args)Handl esBt nImage. Click
IfOpenFi leDi al
og1. ShowDi alog
<>Wi ndows. Forms. Di alogResul t .Cancel Then
Pict ureBox 1.Image=I mage. FromFi le(OpenFi l
eDi alog1.Fil
eName)
EndI f
EndSub
--
---
---
---
-----
----
---
-----
---
---------
----
-----
-------
--
----
---
---
----
---
--
---
-----
--
---
----
---
--
--
--
--
--
--
--
-
PrivateSubBt ncol or_Cl ick( By Val senderAsSy stem. Obj ect,By Vale
AsSy stem. Ev ent Args)Handl esBt ncol or.Cli
ck
IfCol orDial og1. ShowDi alog<>Wi ndows. For ms. DialogResul t.
CancelThen
l
blcol or.
For eCol or=Col or Dialog1. Col or
EndI f
EndSub
--
---
---
---
-----
----
---
-----
---
---------
----
-----
-------
--
----
---
---
----
---
--
---
-----
--
---
----
---
--
--
--
--
--
--
--
-
PrivateSubBt nfont _Cl ick( By Val senderAsSy stem. Obj ect,By Val e
AsSy stem. Ev ent Args)Handl esBt nfont .Cli
ck
IfFont Dial og1.ShowDi al og<>Wi ndows. For ms. DialogResul t.
CancelThen
l
blfont .
For eCol or=Font Dialog1. Col or
l
blfont .
Font=Font Di alog1. Font
EndI f
EndSub
--
---
---
---
-----
----
---
-----
---
---------
----
-----
-------
--
----
---
---
----
---
--
---
-----
--
---
----
---
--
--
--
--
--
--
--
-
PrivateSubBt nsav eas_ Cl ick(By Val senderAsSy stem. Object ,ByVale
AsSy stem. Ev ent Args)Handl esBt nsav eas. Click
Sav eFi l
eDi alog1. Filter=" TXTFi l
es( *.
txt *)
|*.t
xt"
IfSav eFileDi alog1. ShowDi alog=Wi ndows. Forms. Di alogResul t.
OKThen
My .Comput er .
FileSy st em. Wr iteAl lText (Sav eFil
eDi alog1. Fil
eName,
RichText Box1. Text ,Tr ue)
EndI f
EndSub
--
--
----
---
--
---
---
----
---
---
--
--
--
----
---
--
---
---
-----
--
---
--
---
---
----
---
---
--
--
--
---
--
----
--
---
--
---
--
--
---
--
PrivateSubBt nPrint_
Click(ByVal senderAsSy stem. Obj
ect, ByVal e
AsSy stem. Ev entArgs)Handl esBt nPrint.Cli
ck
IfPrintDialog1. ShowDi alog=Di alogResul t.OKThen
PrintDocument 1.Pr i
nterSet ti
ngs=Pr i
ntDi al
og1.PrinterSetti
ngs
PrintDocument 1.Pri
nt()
EndIf
EndSub
--
--
----
---
--
---
---
----
---
---
--
--
--
----
---
--
---
---
-----
--
---
--
---
---
----
---
---
--
--
--
---
--
----
--
---
--
---
--
--
---
--
PrivateSubPr intDocument 1_Pr i
nt Page( ByVal senderAsSy stem. Object,
ByVal eAsSy stem.Drawi ng.Printing.PrintPageEv entArgs)Handl es
PrintDocument 1.Pri
ntPage
Dimf ont1AsNewFont ("Ar i
al",
14, FontSt y
le.Bold)
e.Graphics. DrawSt ri
ng( RichText Box1. Text,font 1,Brushes.Black, 100,
100)
EndSub
EndCl ass
(SetPr oper tyPr i
ntDial
og1. Document=Pr i
ntDocument 1)