Anda di halaman 1dari 286

TRNG CAO NG K THUT CAO THNG KHOA IN T TIN HC

GIO TRNH

LP TRNH

VISUAL BASIC

Trang 1/286

Gio trnh Visual Basic

LI M U
K thut lp trnh l mn hc c s cho sinh vin chuyn cng ngh thng tin v cng l kin thc m sinh vin khi khng chuyn tin hc cn phi bit, t bit l cc chuyn ngnh k thut: in, in t, t ng ha, c kh Kin thc k thut lp trnh hin ang c ging dy cho tt c sinh vin cao ng, trung cp cc khi khng chuyn cng ngh thng tin ti nhiu trng Cao ng i Hc Trong qu trnh hc mn hc ny, sinh vin ngoi vic hc v k thut lp trnh cn phi hc cch s dng mt ngn ng lp trnh c th, v y l Visual Basic. Qua nhiu bi thc hnh hng dn chi tit, gio trnh hng dn cch s dng ngn ng lp trnh Microsoft Visual Basic, mt ngn ng lp trnh trc quan, mnh m, ang c s dng rt nhiu ti nc ta. Microsoft Visual Basic hin l ngn ng gip ch rt nhiu cho cc nhn vin vn phng s dng nhng phn mm ca Microsoft Office nh WinWord, MS Excel, MS Access. Trong qu trnh ging dy v bin son gio trnh ny, chng ti nhn c nhiu ng gp qu bu ca cc ng nghip B mn Tin hc - khoa in t Tin hc. Chng ti xin cm n v hy vng rng gio trnh ny s gip cho vic ging dy v hc tp ca tp th gio vin v hc sinh ngy mt tt hn. Chng ti hy vng nhn c nhiu kin ng gp gio trnh ngy cng hon thin. TP H Ch Minh, ngy 09 thng 12 nm 2007 Nguyn Vn Hng (ch bin) v cc gio vin dy mn K thut lp trnh.

Trang 2/286

Gio trnh Visual Basic

MC LC
CHNG 1: GII THIU VISUAL BASIC .............................11 1. 2. 3. 4. Lp Trnh Trn Windows ........................................................11 Cc Control Chun Ca Windows ..........................................12 Gii Thiu Visual Basic ............................................................13 Mn Hnh Lm Vic Ca Visual Basic ...................................14 Toolbox ...........................................................................................15 Project Explorer ..............................................................................15 Properties Windows ........................................................................16 Ca s Form....................................................................................17 Ca s Code Edittor........................................................................20 Ca s Form Layout .......................................................................22 thit k giao din nhanh chng..................................................22 Cu trc chng trnh Visual Basic........................................23 Cc Thao Tc C Bn Vi Project ..........................................24 Cc control c bn ca Visual Basic .......................................25 To v thc thi chng trnh u tin ....................................25 Bi tp ........................................................................................27

5. 6. 7. 8. 9.

CHNG 2: CC CONTROL C BN .................................29 1. i Tng ..................................................................................29 c im ca i tng ..................................................................29

Trang 3/286

Gio trnh Visual Basic

2. 3. 4. 5. 6. 7. 8.

i Tng Form........................................................................30 i Tng Label .......................................................................32 i Tng Textbox ...................................................................34 i Tng Commandbutton....................................................36 Vit Lnh Cho i Tng ........................................................36 Mt S Lnh C Bn ................................................................38 Bi tp thc hnh 1...................................................................39 Thit k Form..................................................................................40 Vit lnh cho chng trnh .............................................................40 Bi tp thc hnh 2...................................................................43 Frame .....................................................................................44 Image ......................................................................................45 Line.........................................................................................46 Shape ......................................................................................47 Bi Tp ...................................................................................48

9. 10. 11. 12. 13. 14.

CHNG 3: BIN HNG CU TRC LNH...................53 1. 2. 3. Cc Kiu D Liu Trong Vb....................................................53 Bin (Variable) ..........................................................................54 Hng ...........................................................................................58

Trang 4/286

Gio trnh Visual Basic

4. 5.

Cc Php Ton ..........................................................................58 Cc Hm C Bn Ca Vb ........................................................60 Cc hm ton hc............................................................................60 Hm to s ngu nhin (random)....................................................61 Hm x l chui.............................................................................61 Hm kim tra gi tr ........................................................................62 Hm k t........................................................................................62 Hm mu sc...................................................................................63 Hm nhp d liu inputbox.............................................................63 Hm to hp thng bo msgbox .....................................................64 Cc Cu Trc Lnh ..................................................................68 A. Cu trc r nhnh If Select case..............................................68 Bi tp thc hnh If ..................................................................75 Thit k giao din............................................................................75 Vit code cho chng trnh.............................................................75 B. Cu Trc Lp For While Do While......................................78 Check Box ..................................................................................89 Option Button............................................................................90 ListBox ...................................................................................93 Combo Box ............................................................................98 Bi Tp .................................................................................104

6.

7.

8. 9. 10. 11. 12.

CHNG 4: TH TC .........................................................111 1. Hm Format ............................................................................111

Trang 5/286

Gio trnh Visual Basic

2. 3.

Th Tc Hm .......................................................................112 Mng.........................................................................................119 Mng tnh 1 chiu .........................................................................119 Mng nhiu chiu tnh...................................................................123 Mng ng ....................................................................................125 Cc Thut Ton C Bn.........................................................128 Cng dn .......................................................................................128 Cng dn theo iu kin ...............................................................129 Tm kim tun t...........................................................................130 Tm phn t nh nht ln nht...................................................131 Sp xp mng ................................................................................132 S Dng Mt S Hm X L Chui .....................................133 G Ri Chng Trnh ............................................................138 In n.........................................................................................139 Mng i Tng .....................................................................140 i Tng Timer ....................................................................142 Bi tp ..................................................................................145

4.

5. 6. 7. 8. 9. 10.

CHNG 5: CHNG TRNH NHIU FORM .....................157 1. Chng Trnh Nhiu Form....................................................157 Thm Form vo project.................................................................157 Qun l Project........................................................................159 3. Vn s dng bin, th tc v i tng c trong cc Form 159 4. Quy nh Form thc hin trc.................................................161 Trang 6/286 Gio trnh Visual Basic

2.

5. Hin Form ln mn hnh ...........................................................161 3. 4. 5. 6. 7. 8. FORM MDI (Multi Document Interface)............................166 Splash Screen...........................................................................168 To Menu Cho Mt Form ......................................................171 End Sub....................................................................................174 Cc Hm V D Liu Ngy Gi ............................................177 Bi tp ......................................................................................178

CHNG 6: S KIN PHM ...............................................183 1. 2. 3. 4. 5. Cc S Kin Phm ...................................................................183 S Kin Mouse ........................................................................186 X L Li Run Time Error....................................................190 Common Dialog.......................................................................192 Bi tp ......................................................................................198 PHN NNG CAO.......................................199

CHNG 7: 1. 2. 3. 4.

Windows Common Control ...................................................199 ImageList .................................................................................199 Toolbar.....................................................................................202 Updown control .......................................................................207

Trang 7/286

Gio trnh Visual Basic

5. 6.

Slider ........................................................................................208 Lm vic vi tp tin (file) .......................................................208

CHNG 8: TY BIN MI TRNG IDE CA VISUAL BASIC....................................................................................211 1. 2. 3. 4. Danh sch cc Prefix c ngh nn s dng..................211 ng Nhng Ca S Ph .......................................................211 S dng Toolbar Standard.....................................................213 S Dng Short Cut (Phm Tt) Ca Vb Hin Th Cc Ca S 214 Th Ni Cc Ca S................................................................214 S Dng Edit Toolbar.............................................................215 Thay i Cc Xc Lp Ca Mi Trng VB.......................216 S Dng Shortcuts (Phm Tt) Khi Son Tho Code..........219 Tch Ca S Son Tho .........................................................221 Ko V Th ..........................................................................222 To Nhiu i Tng Nhanh.............................................222 S Dng Form Editor Toolbar ..........................................222 iu Chnh i Tng Bng phm....................................223 S Dng Colot Palette chn mu..................................224

5. 6. 7. 8. 9. 10. 11. 12. 13. 14.

Trang 8/286

Gio trnh Visual Basic

15. 16. 17. 18.

S Dng Shortcuts Thc Thi Mt Project ..................224 S Dng Object Brower .....................................................225 S dng t kha With ........................................................228 S Dng gip (Help)......................................................228

PH LC 1: CI T VISUAL BASIC .................................229 PH LC 2: BI TP LUYN TP ......................................237 1. 2. Bi tp tng hp ......................................................................237 thi trc nghim...................................................................245

TI LIU THAM KHO .........................................................286

Trang 9/286

Gio trnh Visual Basic

Trang 10/286

Gio trnh Visual Basic

CHNG 1: GII THIU VISUAL BASIC

NI DUNG GII THIU V NGN NG VISUAL BASIC MI TRNG IDE CA VISUAL BASIC VIT CHNG TRNH U TIN

1.

Lp Trnh Trn Windows

Khi vit chng trnh trn Windows ngha l chng ta to ra mt ng dng c thc thi trong h iu hnh Microsoft Windows. Trong Microsoft Windows, mt ng dng thng c hin th dng ca s (Window) m t gc nhn ca ngi lp trnh Viual Basic, ngi ta gi l FORM.

Trn ca s ca ng dng c cc iu khin (control) giao tip vi ngi s dng. Cc tc ng ca ngi s dng ln ca s hay cc iu khin tng ng s c mt on chng trnh c thc hin p tr li s kin . V d: Giao din mt chng trnh lm ton n gin

Trang 11/286

Gio trnh Visual Basic

2.

Cc Control Chun Ca Windows

Command button

Check box Option button Label v Text box

List box v Combo box

Frame

Thanh cun ngang dc (Hscrollbar, Vscrollbar) Image - Picturebox

Ngai ra cn nhiu control khc s dng trn mi trng Windows m chng ta s ln lt kho st sau ny.

Trang 12/286

Gio trnh Visual Basic

3.

Gii Thiu Visual Basic

Visual Basic (vit tt VB) l mt ngn ng lp trnh hng s kin (event-driven) trn mi trng pht trin tch hp (IDE) c pht trin bi hng phn mm Microsoft. Visual Basic c nng cp qua nhiu phin bn v hin nay l Visual Basic.NET. Visual Basic bt ngun phn ln t ngn ng BASIC v gip lp trnh vin pht trin cc giao din ha (GUI) theo m hnh pht trin ng dng nhanh (Rapid Application Development, RAD); truy cp cc c s d liu dng DAO (Data Access Objects), RDO (Remote Data Objects), hay ADO (ActiveX Data Objects); v lp cc iu khin v i tng ActiveX. IDE ca VB l ni tp trung cc menu, thanh cng c v ca s to ra chng trnh. Thanh menu cho php bn tc ng cng nh qun l trc tip trn ton b ng dng. Thanh cng c cho php truy cp cc chc nng ca thanh menu qua cc nt trn thanh cng c. Mt lp trnh vin c th pht trin ng dng dng cc thnh phn (component) c sn trong Visual Basic. Cc chng trnh bng Visual Basic cng c th gi v s dng Windows API. Mt lp trnh vin c th pht trin ng dng dng cc component c sn trong Visual Basic. VB cung cp cho ta nhng iu khin (control) cn thit to giao din chng trnh nhanh chng. Visual Basic c s dng rt nhiu trong lnh vc lp trnh thng mi. Trong gio trnh ny s dng phin bn Visual Basic 6.0 Profressional. Hng dn cch ci t Visual Basic t b Visual Studio 98 c ti phn ph lc ca gio trnh ny.

Trang 13/286

Gio trnh Visual Basic

4.

Mn Hnh Lm Vic Ca Visual Basic

Giao din u tin ca chng trnh cho php chng ta chn lai ng dng. Vi yu cu ca mn hc ny, chng ta chn lai Standard Exe.

Sau khi khi ng Starndard ExE ca VB, giao din chnh ca chng trnh nh sau (giao din trn my ca bn s thay i ty thuc cc thit lp ca IDE).

Trang 14/286

Gio trnh Visual Basic

Toolbox
Cha cc i tng c th t ln Form. Chng ta click chut chn iu khin, sau to ra trn Form. Hoc c th bm double click, control s t ng a ln Form. bt tt toolbox, chng ta dng nt lnh trn thanh cng c.

Project Explorer
Cha thng tin v chng trnh: cc Form, Module, class trong project. T ca s ny chng ta di chuyn n cc thnh phn trong chng trnh bng cch double click chut vo Form hoc module

Trang 15/286

Gio trnh Visual Basic

bt hay tt ca s Project Explorer, s dng phm tt: Ctrl+R hoc dng nt trn thanh toolbar.

Properties Windows
Dng hin th v thit lp cc thuc tnh (property) ca Form hoc ca cc control c trn Form lc thit k. Thuc tnh ca mt i tng l cc tnh cht, c im m t i tng ny. Cc control mi to ra u c gi tr thuc tnh c ly mc nh (default) hoc k tha t control cha n.

Trn ca s properties gm hai ct: Danh sch thuc tnh Gi tr ca thuc tnh

thit lp thuc tnh cho mt i tng: Chn i tng trn Form bng cch click chut Click chut vo ct gi tr ca thuc tnh cn t trn ca s thuc tnh, sau chn hoc nhp gi tr cho thuc tnh.

Mt s thuc tnh thng dng nn nh: Trang 16/286 Gio trnh Visual Basic

Backcolor: mu nn ca i tng Caption: tiu , y l ni dung hin th ln trn i tng. Enabled: cho php hay cm i tng hat ng. Font: thit lp v font ch ca i tng Forecolor: mu ca cc k hiu xut hin trn i tng Height: chiu cao ca i tng Left: khong cch tnh t cnh tri ca Form (hay control cha i tng ) Name: tn ca i tng (khc vi caption). Top: khong cch tnh t cnh pha trn ca Form hay control cha i tng Visible: cho php i tng hin th hay khng thi im run-time Width: chiu di ca i tng

Ca s Form

y l ni thit k giao din ca chng trnh. Chng ta thit k giao din bng cch a i tng ln Form. m ca s Form Desiner, chng ta thc hin bng nhiu cch: Trang 17/286 Gio trnh Visual Basic

T ca s Project Explorer, bm Double Click vo Form thit k T ca s code, bm phm Shift+F7 Bm vo nt View Code trn ca s Project Explrer

chn mt i tng trn Form: Dng chut r v chn nhm i tng Gi phm Ctrol v click chut nht v chn cc i tng di chuyn i tng trn Form Dng chut r cc i tng n v tr cn thit. Chn i tng v di chuyn bng cc phm mi tn: xa i tng trn Form Chn i tng hay nhm i tng v bm phm delete. iu chnh kch thc i tng a chut vo cc nt vung bao xung quanh i tng v r chut thay i kch thc. thay i kch thc, v tr ca nhm i tng, chng ta c th s dng menu Align ging hng v iu chnh kch thc.

Trang 18/286

Gio trnh Visual Basic

Chn menu Formmat -> Align thc hin ging hng i tng: theo left, center, right, top.

Chn menu Formmat -> Make Same Size nh dng cc i tng c cng kch c theo: chiu di (width), chiu cao (Height) hay c 2 (Both)

Trang 19/286

Gio trnh Visual Basic

Chn menu Formmat -> Horizontal Spacing (hoc Vertical Spacing) khi mun nh dng khang cch gia cc i tng.

Ca s Code Edittor
Ca s ny dng vit lnh (code) cho chng trnh.

m ca s Code Edittor, chng ta thc hin bng nhiu cch: T ca s Form, bm Double Click vo i tng cn vit code Trang 20/286 Gio trnh Visual Basic

T ca s Form, bm phm F7 Bm vo nt View Code trn ca s Project Explrer

Trong ca s code, chng ta thy c 2 hp chn: i tng (Control) S kin (Event) xy ra trn i tng

Trang 21/286

Gio trnh Visual Basic

Ca s Form Layout
Dng xc nh v tr cc ca s khi thc thi chng trnh. t bit khi chng trnh c nhiu ca s (Form), vic xc nh v tr trc quan ca ca s l rt quan trng. Chng ta ch vic ko th cc Form (Form1, Form2) xc nh v tr ca chng trn mn hnh.

thit k giao din nhanh chng


Nu trn Form c nhiu i tng cng loi v c thuc tnh ging nhau th chng ta to mt i tng v nh dng trc cc thuc tnh, sau sao chp ra thnh nhiu i tng khc bng phm tt Ctrl+C (copy) v Ctrl+V (Paste). Ngay khi copy paste, chng trnh s hi:

Chn No khng to mng i tng. Ch khi no cc bn hc mng i tng mi chn yes

Trang 22/286

Gio trnh Visual Basic

Nu c nhiu i tng mun nh dng ging nhau trn mt thuc tnh no th chn cc i ny cng lc ri tin hnh nh dng. S dng cc chc nng canh l, nh kch thc do VB cung cp, khi nhng thuc tnh ny ca nhiu i tng c lin quan vi nhau. Nn thit lp thuc tnh cho Form (hay i tng cha) nh mu ch, font ch trc, sau mi t control ln trn. Lm nh vy control k tha cc thit lp ca cc thuc tnh ny, v chng ta khng mt th gi chn cho tng control. 5. Cu trc chng trnh Visual Basic

Hai bc chnh khi xy dng mt chng trnh trn VB l: Thit k giao din: Thit k Form v b tr cc i tng c trn Form Vit lnh: Vit cc on m iu khin Form v cc control ng vi mi s kin xy ra. Khi thc hin hai bc ny gi l lp mt d n (project). Mi project khi lu ln da c th c nhiu tp tin. File *.vbp : file project ca chng trnh. Chng ta m chng trnh t file ny File *.frm: file Form cha giao din ca chng trnh File *.bas: cha module cua chng trnh File *.cls: cha class ca chng trnh V ngai ra cn nhiu lai file khc na (file resource, file temp) Trang 23/286 Gio trnh Visual Basic

Do trc khi lp mt project ta nn to mt folder trn da lu project. Khi chp chng trnh, chng ta chp ton b folder ny. Ch : Mi bi tp c lu trong mt folder

6.

Cc Thao Tc C Bn Vi Project

M mt project mi: File > New project > standard EXE > OK M mt project c: File Open project Xc nh ni lu tr project sau chn tn project mun m > OK Chy chng trnh: Bm phm F5 hoc Dng chng trnh Dng nt lnh ng mt project ang m: File remove project Lu project: File Save project

Trang 24/286

Gio trnh Visual Basic

7. Cc control c bn ca Visual Basic Visual Basic cha nhiu control, nhng trong ni dung ca mn hc ny chng ta ch tp trung vo cc control c bn.

8. To v thc thi chng trnh u tin

To project mi vi giao din nh sau: (Xem hnh) t tn (thuc tnh Name) cho cc nt lnh ln lt l Cmd1, Cmd2Cmd9, Cmd0, Cmdcong, Cmftru, cmdnhn, CmdChia, CmdBang. thay i name ca cc control, chng ta m ca s Properties, sau chn tng control mt, v i Name.

Nhp thuc tnh Caption ca cc button l cc gi tr tng ng. t tn cho textbox Trang 25/286 Gio trnh Visual Basic

l txtketqua. Nhp thuc tnh text ca textbox l rng Bm F5 thc thi chng trnh. Dng chng trnh Double click vo nt lnh CmdBang hin th ca s code. Nhp lnh nh hnh di. Bm F5 thc thi chng trnh. Bm vo nt = (CmdBang) v xem xt hp thoi hin th trn mn hnh.

Trang 26/286

Gio trnh Visual Basic

9.

Bi tp

1. Thc hin cc yu cu sau: To Project. EXE. Tt / m cc thnh phn ca VB: ToolBox, Form, Project Explorer, Code Windows, Properties Window. Lu Project vi tn BaiTap1 trong th mc BaiTap1 M Project v thc thi chng trnh 2. Thit k giao din cho chng trnh nh sau: Lu : Mi bi tp nn to trn da mt folder lu nhng tp tin project, Form.. ca bi tp . Hy to mt Form c dng sau: (trong bi tp ny ta cha cn quan tm n tn ca cc i tng, v vy c ly tn mc nhin.) Sau khi thit k xong hy lu ln da vi tn tp tin project l bt1_chuong1 ri cho thc hin chng trnh th.

3. Hy to cc Form c dng sau: Trang 27/286 Gio trnh Visual Basic

Trong bi tp ny ta cha cn quan tm n tn ca cc i tng, v vy c ly tn mc nhin. Sau khi thit k xong hy lu ln da vi tn tp tin project l Bai3_chuong1 ri cho thc hin chng trnh th. Bi tp ny nhm rn luyn cc thao tc sao chp, di chuyn, k nng s dng cc cng c nh dng i tng: Align Make same size Horizontal spacing Vertical spacing Lock controls

Trang 28/286

Gio trnh Visual Basic

CHNG 2: CC CONTROL C BN
NI DUNG I TNG V CCH S DNG CC CONTROL C BN: FORM LABEL TEXTBOX COMMAND BUTTON FRAME - SHAPE LINE IMAGE MT S LNH C BN LP TRNH TNG TC VI CC CONTROL

1.

i Tng

VB l mt ngn ng lp trnh hng i tng nn lm vic trn VB chnh l lm vic trn cc i tng. Mi chng trnh trn VB c nhiu i tng, mi i tng dng th hin mt yu t no trong chng trnh.

c im ca i tng
Mi i tng c mt tn (Name) phn bit v trong chng trnh dng tn ny truy xut n i tng. C th dng ch ci, ch s, _ t tn cho i tng v phi bt u bng mt ch ci. Tn di ti a 40 k t. Khng cha khong trng v cc du chm cu. Khng trng tn vi t kha. Tn i tng khng phn bit ch hoa hay ch thng.

Tuy nhin cho d qun l v thun li khi vit chng trnh, ngi ta xut cch t tn nh sau: Lun lun bt u bng 3 ch Trang 29/286 Gio trnh Visual Basic

ci thng cho bit loi i tng (gi l prefix.) v d nh cmd, lbl, frm, txt. Danh sch Prefix c m t cc phn sau.

Mi i tng c nhiu thuc tnh. Cc thuc tnh c th thay i trn ca s properties lc thit k (design time) hoc bng chng trnh lc thc thi (run time). Mi i tng u c cc hot ng c gi l phng thc (method). Khi mt chng trnh c thc hin mi i tng nhn c cc s kin (Event) tc ng ln n (chng hn nh click, doudvle click, right click..). Chng ta vit lnh trong cc event ny x l trc cc s kin nhn vo. Trong cc on m lnh ca chng trnh truy xut n cc thuc tnh, cc phng thc ca i tng ngi ta dng tn ca i tng km theo tn ca thuc tnh hoc phng thc theo c php: <tn i tng>.<tn property/tn method> o V d: txt_hoten.text=DurianGroup 2. i Tng Form

Form l mt i tng c s ca chng trnh. Hu nh mi chng trnh u bt u t mt Form. i tng ny c nhiu property v method. Chng ta s tm hiu tc dng ca mt gi tr trn cc thuc tnh c bn: Caption: t tiu cho Form. Name: tn ca Form, t 3 k t u l Frm Borderstyle: Quy nh kiu khung cho Form Trang 30/286 Gio trnh Visual Basic

o 0-none: Khng c khung, km theo khng c control menubox, Maximize box, minimize box, khng hin tiu v khng th thay i kch thc khi chng trnh thc hin. o 1-Fix single: Khung n, c control menu box, khng thay i kch thc c. o 2-Sizable: C y cc yu t Control menu box, maximize, minimize box, tiu , khung kp v c th thay i kch thc c. o 3-Fixed dialog: Ca s dng hp thoi, c tiu , control menu box, khng thay i kch thc o 4-FixtoolWindow: Ging Fixed single nhng tiu v control menu box c kch thc nh. o 5-Sizable ToolWindow: Ging FixTollWindow nhng kch thc c th thay i c. ConttrolBox: Nu t l True th Form s c menu control box, d nhin n cn ph thuc vo loi border chn. Nu t l false th khng c menu control box, Maximize, Minimize button. Maxbutton: Nu t l True th Form s c nt maximize, d nhin n cn ph thuc vo gi tr ca controlbox chn. Nu t l false th khng c nt Maximize Minbutton: Nu t l True th Form s c nt minimize, d nhin n cn ph thuc vo gi tr ca controlbox chn. Nu t l false th khng c nt minimize

Trang 31/286

Gio trnh Visual Basic

Icon: Quy nh biu tung i din cho Form khi Form trng thi minimize. Thuc tnh ny cng ph thuc vo gi tr ca thuc tnh controlbox Picture: t mt hnh ln nn ca Form, c th l nhng loi :BMP, DIB, GIF, JPG, ICO,CUR Mun xa hnh t trn Form ch cn xa ni dung ang c trn dng ny (bm phm Delete ti hng Picture trong ca s Properties). ShowInTaskbar: Nu t l true th tn ca ca s c hin ln taskbar, ngc li th khng WindowState: Quy nh trng thi ca ca s khi Form va c m (chng trnh thc hin), thuc tnh ny c gi tr s: o 0: Ca s c kch thc ging nh lc thit k. o 1: Hin ca s ch cc tiu o 2: Hin ca s ch cc i 3. i Tng Label

i tng ny dng hin mt cu vn bn trn Form, ni dung cu vn bn ny khng th thay i bi ngi s dng trong qu trnh chng trnh thc hin. Nu mun hin th thng tin m khng cho php ngi s dng nhp, th chng ta s dng Label.

Cc thuc tnh c bn ca Label Name: tn ca Label, t 3 k t u l Lbl Caption: Ni dung hin trn i tng ny Trang 32/286 Gio trnh Visual Basic

Font: Quy nh font ch cho ni dung hin trnllabel Aligment: Canh l cho ni dung hin trn label Backcolor: Mu nn ca i tng BorderStyle: Khung vin Forecolor: Mu ca ch Backstyle: Quy nh cch th hin nn ca label o 0-Transparent: Nn trong sut, khng b nh hng bi backcolor o 1-Opaque: Nn c th hin bi gi quy nh trong backcolor Autosize: Nu t bng true th kch thc label t ng co gin va vi ni dung. Ngc li t bng false th phi iu chnh bng tay Wordwrap: Nu t bng true th t ng xung dng di khi ng l phi ca label. Ngc li t bng false th ni dung c th b che khut di hn chiu di ca label. Appearance: c 2 gi tr (3D Flat): cho php hin th dng phng hay 3D Left: v tr tnh t bn tri control cha n Top: v tr tnh t nh control cha n Height: chiu cao Width: chiu di Visible: (True/False) cho php i tng c c hin th hay khng thi im run-time S kin (Event) c bn hay s dng ca Label Trang 33/286 Gio trnh Visual Basic

Label_Click: xy ra khi ngi s dng click chut trn Label

4.

i Tng Textbox

ny cho php nhp v th hin i tng Textbox mt ni dung no dng chui. Khi chng trnh mun nhn d liu c nhp bi ngi s dng, chng ta c th s dng Textbox.

Cc thuc tnh c bn ca Textbox: Name: tn textbox, t 3 k t u l Txt Text: Ni dung cha trong teXtbox Maxlength: Chiu di ti a ca ni dung th hin trong textbox Enable: cho php textbox c nhn s kin, chnh sa ni dung. Font: cc thit lp v font Trang 34/286 Gio trnh Visual Basic

Aligment: ging Label Backcolor: ging Label Forecolor: ging Label Appearance: ging Label BorderStyle: ging Label Left: v tr tnh t bn tri control cha n Top: v tr tnh t nh control cha n Height: chiu cao Width: chiu di Visible: (True/False) cho php i tng c c hin th hay khng lc run-time S kin (Event) c bn ca Textbox Text_Change: xy ra khi ni dung ca textbox b thay i Text_Click: xy ra khi ngi s dng click chut trn textbox Text_DblClick: xy ra khi ngi s dng double click chut trn textbox

Trang 35/286

Gio trnh Visual Basic

5.

i Tng Commandbutton

L nt lnh khi mun chung trnh thc hin mt x l no da trn xc nhn ca ngui s dng. Ngi s dng click chut, v nt lnh c tc ng.

Ngoi cc thuc tnh tng t nh Label, Textbox m t trn, thuc tnh c bn ca command button quan trng c s dng nhiu nht l: Caption: ni dung s th hin trn nt lnh. Name: tn command button, t 3 k t u l Cmd Visible: (True/False) cho php i tng c c hin th hay khng lc run-time Enabled: Cho php button c hat ng (sng/m) hay khng S kin c bn hay s dng ca command button Click: xy ra khi ngi s dng Click chut trn command button

6.

Vit Lnh Cho i Tng

Khi ta t mt i tng ln Form th i tng ny vn cha hot ng, bi ta cha quy nh cho n phi lm g thng qua cc lnh. Trang 36/286 Gio trnh Visual Basic

Chng ta c th quy nh cc phn ng cho i tng ty thuc vo s kin xy ra trn bn thn i tng , v vy truc khi vit lnh cn ch hai vic: Xc nh s kin no trn i tng no s gy ra cc x l Vit nhng lnh g thc hin x l Cc bc vit lnh cho i tng: Trn mn hnh thit k, double click vo i tng mun vit lnh, khi ca s vit lnh s xut hin.

Chn s kin m ta mun vit lnh. Khi hai dng (tn hm s kin v dng kt thc ca th tc) tng ng ca s kin xut hin. Vit cc lnh vo gia hai dng va xut hin. Mi lnh vit trn mt dng. Nu lnh qu di mun ngt dng th mt khong trng v du _ cui dng trn. VB c ch h tr cho ngi lp trnh lc nhp m lnh: V d: Khi cn truy xut n mt thuc tnh hay phng thc no ca mt i tng ta g tn i tng ri g Trang 37/286 Gio trnh Visual Basic

ri du chm, lc ny mt danh sch cc thuc tnh v phng thc ca i tng ny hin ln ta bm tip cc k t u ca thuc tnh/s kin m ta cn ri bm phm Tab hay CtrlEnter hay Double click. Trong trng hp danh sch ny khng hin ln th phi xem li c th ta vit tn i tng sai. Nu thuc tnh m ta cn khng c trong danh sch th c ngha l thuc tnh ny khng th thay i c bng chng trnh Lu 1: Khi vit lnh cho cc i tng th k t lc ta khng nn thay i gi tr thuc tnh name ca i tng, v khi cc lnh s dng tn c ca i tng s khng hiu c i tng ny. Do , nn t tn cho cc i tng trc, ri sau mi bt u vit Code. Lu 2: Khng sa i trn hai dng tiu v dng kt thc. 7. Mt S Lnh C Bn Lnh End: Dng chm dt mt chng trnh VB ang thc hin.

Trang 38/286

Gio trnh Visual Basic

Lnh gn: K hiu =, lnh ny c cng dng gn gi tr vo bin hoc gn gi tr mi vo mt thuc tnh ca i tng. Lnh Val: chuyn t chui sang s. Chi tit v kiu d liu s c cp cc bi sau. 8. Bi tp thc hnh 1

Thit mt Form v vit chng trnh cho from hot ng nh sau:

Trn Form c mt textbox c t tn txt_mess, ni dung ban u l rng. Font ch ting Vit, kch thc 14pt 3 i tng cn li l 3 commandbutton c tn tun t nh sau: cmd_mess, cmd_del, cmd_end. Khi s kin vo cmd_mess th cu cho Chc mng bn n vi VB xut hin trn textbox txt_mess. Khi click vo cmd_del ni dung trn textbox b xa. Khi click vo trn cmd_end th chng trnh kt thc. Khi double click vo textbox txt_mess th hin th website ca bn

Trang 39/286

Gio trnh Visual Basic

Trnh t thc hin:

Thit k Form

To mt folder mi trn da Open mt project mi. t tn cho Form l frm_main, caption l chng trnh u tin a mt textbox ln Form, t tn l txt_mess, thuc tnh text=, font ch VNI_DUFF, c ch 14, Aligment: center a mt command button ln Form, chnh kch thc, chn font ch VNI-APTIMA, c ch 10. t caption=In li cho, Name=cmd_mess Chn nt lnh th hai, t name=cmd_del, caption=Xo ni dung trong textbox Chn nt lnh th ba, t name=cmd_end, caption=kt thc

Vit lnh cho chng trnh

Nhp kp vo nt lnh cmd_mess: Nhp thm hng quy nh cch khai bo bin: Option Explicit Trang 40/286 Gio trnh Visual Basic

ngay u (xem hnh). T nay v sau, trong tt c cc bi tp, bn phi khai bo hng ny vo u tt c cc Form. V chi tit cho khai bo ny s c cp chi tit cc chng sau. Nhp dng lnh sau:txt_mess.text=Chc mng bn n vi VB vo gia dng tiu v dng kt thc

Chn i tng cmd_del trn combo box object, chn s kin click. g vo lnh sau: txt_mess= (Xa ni dung ca textbox)

Chn i tng cmd_end trn hp chn pha trn bn tri, chn s kin click trn combobox even. G vo lnh sau: end

Trang 41/286

Gio trnh Visual Basic

M ca s Form, bm double click trn txt_mess m ca s code. Trong khi con tr chut ang nm gia vng Private Sub txtmess_Change() . End Sub th chn hp danh sch (cha s kin) bn gc trn phi, chn vo s kin DblClick

Vit code x l cho s kin double click trn textbox

Trang 42/286

Gio trnh Visual Basic

9.

Bi tp thc hnh 2

Vit chng trnh thay i v tr v ni dung ca Label s dng cc command button (xem hnh). Khi ngi s dng bm vo nt tng ng - v d: top left) th Label s nm v tr trn pha tri vi ni dung l Top Left. Sau khi thit k giao din v t tn cc i tng ln lt l Lbl1, cmdTopLeft, CmdTopRight, CmdButtomLeft, CmdButtomRight, double click chut vo ln lt tng command button v vit lnh nh sau: Trang 43/286 Gio trnh Visual Basic

10.

Frame

L mt control dng vt cha (container), dng cha cc control khc. Khi a ln Form n hin th dng khung hnh ch nht c tiu . Trang 44/286 Gio trnh Visual Basic

Sau khi t mt frame trn Form nu ta t cc control khc vo trong frame ny th frame tr thnh vt cha ca control .

Frame cn c dng nhm cc i tng hot ng theo nhm nh Option button. Thuc tnh chnh ca Frame l : Name: tn ca Frame, t 3 k t u (prefix) l Fla Caption: ni dung hin th trn tiu ca Frame Backcolor: mu nn Forecolor: mu ch ca cc i tng trn Frame 11. Image

i tng ny dng hin th hnh nh. Cc thuc tnh chnh: Name: tn ca i tng, vi 3 k t u (prefix) l Img Picture: hnh nh cn hin th Stretch (true/false): cho php co dn hnh hay hin th theo kch thc tht ca nh Visile: cho php hin th hay khng Trang 45/286 Gio trnh Visual Basic

Height: chiu cao nh Width: chiu di nh

a hnh nh ln Image c 2 cch: Quy nh ti thi im thit k (Design-time): thng qua thuc tnh picture. Chn hnh cn hin th Ti thi im thc thi chng trnh (run-time): bng cch gn thuc tinh picture bng kt qu ca hm loadpicture(). V d Img.Picture = LoadPicture("c:\DurianGroup.jpg") 12. Line

i tng ny n gin ch trnh v mt ng thng trn Form dng cho vic trang tr giao din.

Cc thuc tnh ca Line: Name: tn ca line vi 3 k t u l Lin Bordercolor: mu vin ng line BorderWidth: kch thc ng line BorderStyle: kiu v ng line X1,x2,y1,y2: ta (x,y) ca 2 im u v cui ca line

Trang 46/286

Gio trnh Visual Basic

13.

Shape

Ging nh Line i tng shape dng trang tr vi kh nng biu din cc hnh c bn: vung, trn, Ellipse.. Thuc tnh shape l: chnh ca

Name: tn ca shape vi 3 k t u l Shp Shape: o o o o o o 0: Trnh by hnh ch nht 1: Trnh by hnh vung 2: Trnh by hnh ellipse 3: Trnh by hnh trn 4: Trnh by hnh ch nht gc trn 5: Trnh by hnh vung gc trn.

FillStyle: kiu t mu bn trong Shape. Bn ch nhn thy mu bn trong khi FillStyle khc 0 (Transparent: trong sut).

FillCorlor: mu t bn trong

Trang 47/286

Gio trnh Visual Basic

14.

Bi Tp

Lu : Mi bi tp nn to trn da mt folder lu nhng tp tin project, Form ca bi tp . 1.Hy thit k mt Form nh sau: t tn cho 3 textbox ln lt l : txthoten, txtho, txtten t tn cho 3 nt lnh ln lt l btnHoLot, btnTen, btnHovaten Hy vit lnh cho cc i tng Form c th hot ng nh sau: Ngi s dng s g h v ch lt vo txtholot, v tn vo txtten Nu nt lnh cmd_holot c click th ni dung ca txthoten =txtholot.text Nu nt lnh cmd_ten c click th ni dung ca txthoten=txt_ten.text Nu nt lnh cmd_hoten c click th ni dung ca txthoten =txt_holot.text & & txtten.text Nu double click (s kin dlbclick) trn txthoten th ni dung ca txthoten b xa. Trang 48/286 Gio trnh Visual Basic

2.Thm vo bi tp trn vi yu cu sau: Ni dung ca txtholot v txtten c th hin ngay trn txtHoTen khi ngi s dng nhp d liu vo txt_holot hay txtten (gi : vit lnh cho s kin change ca txtholot v txtten

3.Hy thit k mt Form nh sau: t tn i tng nh hnh m t.

Hy vit lnh cho cc i tng Form c th hot ng nh sau: Ngi s dng s nhp cc gi tr vo txt1, txt2 (l nhng gi tr s) Nu nt lnh cmd1 c click th ni dung ca Lbl_mess=val(txt1.text)+ val(txt2.text) Nu nt lnh cmd2 c click th ni dung ca Lbl_mess=val(txt1.text) - val(txt2.text) Trang 49/286 Gio trnh Visual Basic

Nu nt lnh cmd3 c click th ni dung ca lbl_mess=val(txt1.text) * val(txt2.text) Nu nt lnh cmd4 c click th ni dung ca lbl_mess=val(txt1.text) * val(txt1.text) Nu nt lnh cmd5 c click th ni dung ca lbl_mess=val(txt1.text)* val(txt1.text)* val(txt1.text) Nu nt lnh cmd5 c click th ni dung ca lbl_mess=sqr(val(txt1.text)) Nu nt lnh cmd_exit c click th kt thc chng trnh Nu double click (s kin dblclick) trn lbl_mess, hoc txt1 hoc txt2 th xa ni dung ang c ca hai textbox txt1, txt2 v label lbl_mess.

4. Hy to mt Form nh sau: Khung mu trng l mt picture box vi appearrance = flat Hnh trn l mt shape c v bn trong Picture box Khi chng trnh chy mi ln ngi s dng click vo cc nt mi tn th hnh trn s dch chuyn mt khong (ty

Trang 50/286

Gio trnh Visual Basic

) theo hng mi tn click (s dng thuc tnh left, top ca shape)

5. Vit chng trnh chn hnh dng v mu ca mt shape nh sau:

t tn Shape l Shp, thit lp thuc tnh fillStyle ca Shp l 0-Solid Vit code chn hnh dng (Shape) v mu t (FillColor) nh sau: Sub CmdVuong_Click() Shp.Shape=1 End sub Trang 51/286 Gio trnh Visual Basic

Sub CmdMauDo_Click() Shp.FillColor=VBRed End sub

Cho bit tp cc hng s mu c bn ca VB nh hnh.

Trang 52/286

Gio trnh Visual Basic

CHNG 3: BIN HNG CU TRC LNH

NI DUNG: KIU D LIU - BIN HNG - CC TON T CC HM C BN CC CU TRC LNH OPTION BUTTON CHECK BOX LIST BOX COMBO BOX

1.

Cc Kiu D Liu Trong Vb

Trong VB6 c rt nhiu kiu d liu. Tuy vy, vi phm vi gio trnh ny, ch mt s kiu d liu quan trng v hay s dng nht c cp. Cc kiu cn li sinh vin c th tham kho thm ti phn gip (Help) ca b Visual Studio. Kiu d liu Byte Boolean Integer Long (long integer) Double Date Phm vi 0. . 255 True / False -32,768 32,767 C (+-) 2 t C (+-)-10308 1/1/100 31/12/9999

Trang 53/286

Gio trnh Visual Basic

String Variant

2 t k t

Trong : Byte, Integer, Long l cc kiu s nguyn Double l cc kiu s thc. Date lu gi tr ngy gi (th t ngy thng nm ph thuc vo quy nh ca h thng trong Control Panel ca Windows). Mt bin kiu date c th cha gi tr ngy thng nm, gi hoc ch cha gi tr ngy thng nm Object: tham chiu n mt i tng no trong chng trnh. String: cha mt chui k t. Variant: L loi bin c th cha bt k mt loi d liu s hoc chui. Kiu d liu ca n ch xc nh khi c gn gi tr. Ty theo gi tr thc t, chng ta s chn lai d liu ph hp. 2. Bin (Variable)

Khi nim: Dng lu cc gi tr tnh ton c trong qu trnh chng trnh ang thc hin. Gi tr lu trong bin c th thay i trong qu trnh s dng. Mi bin c mt tn phn bit v thuc mt loi d liu no . Qui tc t tn bin:

Trang 54/286

Gio trnh Visual Basic

Di ti a 255 k t, c th dng ch ci, ch s, du _ t tn Bt u bng mt ch ci Khng cha khong trng v cc k t: +-*/ ! @ # $ % ^ & * ( ) cc du chm cu Khng trng tn vi t kha Trnh t tn bin trng nhau Tn bin nn r rng, ni ln phn no mc ch s dng ca n Nn bt u bng ba k t vit tt ca kiu d lliu Khai bo bin: Dim <tn bin> As <kiu d liu>

V d: Dim X As integer Dim a As Double Dim b As Long, D as Long, E as Long Ch : khai bo nhiu bin, chng ta phi a kiu d liu ln lt vo, khng th vit gom i vi VB6 V d: o Dim 1a as Integer: khai bo ny sai v tn bin bt u bng s o Dim a- as Integer: khai bo ny sai v tn bin cha k t t bit -

Trang 55/286

Gio trnh Visual Basic

o Dim a,b,c as Integer: khai bo ny khng hp l (a,b : khng phi kiu Integer) o Dim a as Integer, b as Integer, c as Integer: khai bo ny ng

Qun l bin: Trong VB c th khng cn phi khai bo bin trc khi s dng, v khi bt gp mt tn bin cha khai bo th VB t ng to ra bin ny. Tuy nhin iu ny d pht sinh cc li trong chng trnh m rt kh pht hin. V d nh trong on lnh sau:

o Dim x As Integer o Dim result As Integer o X=10 o Resul=X*2 Dng lnh ny ta mun gn gi tr x*2 vo bin result khai bo nhng li vit thiu k t t, lc ny VB s to ra bin mi Resul. trnh tnh trng ny ta c yu cu VB khng t ng to ra bin nu nh n cha c khai bo bng cch t dng lnh Option Explicit trong phn General ca ca s code.

V Tr Khai Bo V Phm Vi S Dng Ca Bin

Trang 56/286

Gio trnh Visual Basic

Cc bin khai bo trong mt hm (SUB): phm vi s dng ca cc bin ny ch tn ti trong SUB khai bo n. Nhng bin ny xut hin trong b nh khi SUB c gi thc hin v xa khi b nh khi SUB ny thc hin xong. Bin loi ny gi l bin cc b.

Cc bin khai bo khai bo trong phn General ca mt Form th n c tc dng vi bt k mt on lnh no c trong Form ny. Nhng bin ny xut hin trong b nh khi Form c m v ch xa khi b nh khi Form c b ng.

Trang 57/286

Gio trnh Visual Basic

Mt bin mun c phm vi s dng trong ton b project, th phi khai bo bng t kha public (c th khai bo trong Form hoc module). Nhng bin ny tn ti trong b nh t u cho n khi kt thc chng trnh.

Lu : Cc bin kiu s khi c khi to trong b nh c gi tr =0 3. Hng

Ta c th t tn cho cc gi tr khng i v c dng nhiu trong chng trnh theo c php: Const <Identifier> as datatype = value V d: Cont Pi as Double=3.1416 4. Cc Php Ton

Cc php ton trn s:

Trang 58/286

Gio trnh Visual Basic

K hiu ^ * / + \ Mod V d: Dim A as Integer A=3^2 -> A=9 -> A=2

Php ton Ly tha (vd: x=3^8) o du (vd: x= - y) Nhn (vd: x= Y * 3) Chia (vd: x= Y / 3) Cng (vd: x= Y + 3) Tr (vd: x= Y - 3) Chia Nguyn (vd: x= 34 \ 3) Ly d trong php chia nguyn

A=12\5

A = 8 mod 3 -> A=2

Php ton trn chui (string) : php ni : & : Dng ni 2 chui Cc php ton quan h K hiu = <> < <= Bng Khc Nh hn Nh hn hoc Bng Quan h

Trang 59/286

Gio trnh Visual Basic

> >=

Ln hn Ln hn hoc Bng

Cc php ton lun l: K hiu NOT AND OR 5. Ph nh V Hoc Quan h

Cc Hm C Bn Ca Vb

Cc hm ton hc
Hm ABS(x) FIX(x) INT(x) EXP(x) LOG(x) SQR(x) SIN(x) COS(x) TAN(X) ATN(x) M t Tr v tr tuyt i ca x Tr v gi tr l phn nguyn ca x Tr v gi tr l s nguyn ln nht nh hn hay bng x Hm m ex Hm loge x (c s e) Cn bc 2 ca x Hm lng gic Sin(x) Hm lng gic Cos(x) Hm lng gic Tan(x) Hm lng gic Arctg ca x Trang 60/286 Gio trnh Visual Basic

Hm to s ngu nhin (random)


Hm RANDOMIZE RND( ) M t khi ng b to s ngu nhin Tr v 1 s ngu nhin t 0 n 1

Hm x l chui

Hm LEN(chui) VAL(chui) LEFT(chui,n) RIGHT(chui,n)

M t Tr v gi tr l di ca chui i chui l k t s thnh s Ly ra 1 chui l n k t bn tri ca chui nhp vo Ly ra 1 chui l n k t bn phi ca chui nhp vo Ly ra 1 chui l n k t - bt u t v tr nhp - ca chui nhp vo i tt c k t ch thng thnh ch HOA i tt c k t ch HOA thnh ch thng B tt c k t trng bn tri chui Trang 61/286 Gio trnh Visual Basic

MID(chui,v tr, n)

UCASE(chui) LCASE(chui) LTRIM(chui)

RTRIM(chui) TRIM(chui)

B tt c k t trng bn phi chui B tt c k t trng bn tri v bn phi chui + Kiu = 1: i tt c k t ch thng thnh ch HOA

STRCONV(chui,kiu)

+ Kiu = 2: i tt c k t ch HOA thnh ch thng + Kiu = 3: i tt c k t u t thnh thnh ch Hoa. Tr v v tr u tin ca chui 1 bn trong chui ( 0 nu khng c). Dng tm kim 1 chui bn trong 1 chui khc. i gi tr ca biu thc s thnh chui s, In gi tr biu thc s theo chui nh dng.

INSTR(chui, chui 1)

STR(s) FORMAT(biu thc chui nh dng)

Hm kim tra gi tr
Isnumeric(chuoi) IsDate(chuoi) Tr v true/false xc nh chui phi l dng s hay khng Tr v true/false xc nh chui phi l dng DateTime hay khng

Hm k t
ASC(k t): Tr v gi tr l m ASCII ca k t Trang 62/286 Gio trnh Visual Basic

CHR(ASCII Code): SPACE(n):

Tr v k t c m ASCII l ASCII Code Tr v n k t trng

Hm mu sc
RGB(Red,Green,Blue): Vi Red,Green,Blue c gi tr t 0 n 255 Vi Color c gi tr t 0 n 15 0: Black, 1: Blue, 2: Green, 3: Cyan, 4: Red, 5: Magenta, 6: Yellow, 7: White, 8: Gray, 9:Light Blue, 10: Light Green, 11: Light Cyan, 12: Light Red, 13: Light Magenta, 14: Light Yellow, 15: Light White

QBCOLOR(color):

Hm nhp d liu inputbox


Mc ch: Hin hp nhp d liu khi c nhu cu C php: Bin = InputBox ( Prompt, Title, Default) Prompt: Cu nhc (dng chui) yu cu thc hin vic nhp Title: Ta ca hp nhp d liu Default: Gi tr mc nhin khi hin hp nhp d liu ln Kt qu ca vic nhp liu c gn vo bin Trang 63/286 Gio trnh Visual Basic

V d: n=Inputbox(Nhap vo mt s:,Nhp liu)

Nu ngi s dng Click vo nt Cancel th kt qu ca hm l mt chui rng. V d: Vit chng trnh nhp mt tn bng Inputbox v hin th li tn nhp

Hm to hp thng bo msgbox
Mc ch: Hin hp thng bo khi c yu cu trong cc tnh hung khc nhau. C php: Trang 64/286 Gio trnh Visual Basic

Dng 1: Khng nhn v g tr sau khi hp thoi xut hin. Thng dng khi thng bo mt thng ip no . MsgBox C Li

hin th chui thng bo xung hng, chng ta c th ghp thm hng s VBCrlf hay VBNewline hay Chr(13) & Chr(10) ngt thng bo thnh nhiu hng MsgBox "Cho bn & vbCrLf & "DurianGroup MsgBox "Cho bn & VbNewLine & "DurianGroup MsgBox "Cho bn & Chr(13) & Chr(10) & "DurianGroup

Dng 2: Bin= MsgBox ( Prompt, Style, Title )

Prompt: Cu nhc (dng chui) yu cu thc hin vic nhp Title: Ta ca hp nhp d liu Style: Kiu th hin ca hp hi thoi Gi tr s 0 1 2 3 Gi tr do VB nh ngha VbOKOnly VbOKCancel VbAbortRetryIgnore VbYesNoCancel Mc ch Nt lnh OK Nt lnh OK + CanCel Nt lnh Abort + Retry + Ignore Nt lnh Yes + No + CanCel Trang 65/286 Gio trnh Visual Basic

4 5 16 32 48 64

VbYesNo VbRetryCancel VbCritical VbQuestion VbExclamation VbInFormation

Nt lnh Yes + No Nt lnh Retry + Cancel Biu tng Stop Biu tng Question Biu tng Exclamation Biu tng InFormation

Cc gi tr tr v cho bin ca MsgBox:

GI TR S 1 2

GI TR NGHA VbOK VbCancel

DO VB NH

HIN TNG

Nt lnh c chn l: OK Nt lnh c chn l: CanCel Nt lnh c chn l: Abort Nt lnh c chn l: Retry Nt lnh c chn l: Ignore Nt lnh c chn l: Yes Trang 66/286 Gio trnh Visual Basic

3 4 5 6

VbAbort

VbRetry VbIgnore VbYes

VbNo

Nt lnh c chn l: No

Gi s mun to hp thoi sau: ta s dng lnh: traloi=MsgBox(Bn c mun khng,VbYesNo+VbQuestion,Stop) Hoc traloi= MsgBox(Bn c mun tip tc khng,36,Stop) Tuy 2 cch to trn l tng ng, nhng chng ta nn s dng cch th nht v n r rng, d c, v VB gip chng ta thc hin iu ny m khng cn phi nh s. tip tc

Trang 67/286

Gio trnh Visual Basic

6.

Cc Cu Trc Lnh

A. Cu trc r nhnh If Select case


1. Cu trc r nhnh khng y IFTHEN

Trang 68/286

Gio trnh Visual Basic

Dng 1: Ch lm 1 lnh duy nht nu tha iu kin

V d: Vit code cho nt lnh Thot ca chng trnh. Trc khi thot, chng trnh hin th hp thoi xc nhn vic thot chng trnh. Nu ngi s dng ng (chn Yes) th chng trnh kt thc.

Dng 2: Lm nhiu lnh nu tha iu kin

Trang 69/286

Gio trnh Visual Basic

V d: Vit li nt lnh thot cho v d trn. Nu ngui s dng chn Yes (ng thot), chng trnh hin th li cho tm bit, sau thot chng trnh.

Trang 70/286

Gio trnh Visual Basic

2. Cu trc r nhnh y IFTHENELSE Dng 1: Thc thi cc lnh a1,a2..aN nu tha iu kin, ngc li thc hin nhm lnh b1,b2,..bN

V d: Vit chng trnh nhp vo 1 s nguyn. Kim tra s va nhp l s chn hay s l. Giao din c cho nh hnh di. Tn cc i tng l txtso, cmdKiemTra Trang 71/286 Gio trnh Visual Basic

Dng 2: Lm nhiu trng hp vi nhiu iu kin khc nhau

Trang 72/286

Gio trnh Visual Basic

V d: vit chng trnh nhp 1 s nguyn ca th (2, 3, 4.8). Chng trnh s thc hin c sang Th tng ng (THU HAI, THU BA, THU TU..)

Trang 73/286

Gio trnh Visual Basic

Trang 74/286

Gio trnh Visual Basic

7.

Bi tp thc hnh If

Thit k mt chng trnh ng nhp (Log In) cho php ngi s dng nh tn v mt m (Password) vo sau kim tra xem password c ng vi chui c quy nh trc hay khng?

Thit k giao din


To cc control (label, textbox, command button) nh hnh t tn ln lt cmdOK,CmdCancel l TxtUser, TxtPassword,

Thit lp thuc tnh PasswordChar cho txtPassword l * (che k t nhp vo)

Vit code cho chng trnh

Trang 75/286

Gio trnh Visual Basic

V d: Vit chng trnh gii phng trnh bc nht Ax+B=0 Option Explicit Private Sub Form_Load()Lu : s dng s kin load ci t cc yu t ban u lbl_tua.Caption = "CHNG TRNH" & Chr(13) & " GII PHNG TRNH BC I Ax+B=0" S dng k t chr(13) xung dng txt_A.Text = "" txt_b.Text = "" txt_kq.Text = "" End Sub

Private Sub cmd_giai_Click() If txt_A.Text = "" Or txt_b.Text = "" Then MsgBox "Bn cha nhp d liu " ElseIf Val(txt_A.Text) <> 0 Then txt_kq.Text = "Nghim ca PT l: " & Val(txt_b.Text) / Val(txt_A.Text) ElseIf Val(txt_b.Text) = 0 Then txt_kq.Text = "Phng trnh v s nghim" Else txt_kq.Text = "Phng trnh v nghim" End If Trang 76/286 Gio trnh Visual Basic

End Sub

Private Sub cmd_moi_Click() txt_A.Text = "" txt_b.Text = "" txt_kq.Text = "" txt_A.SetFocus Chuyn focus v textbox txt_a tin li cho ngi s dng nhp liu End Sub Private Sub cmd_kt_Click() End End Sub

3. Cu trc la chn SELECT CASE END SELECT

V d: thc hin li v d ni trn, xut ra mn hnh chui pht m ca th nhp vo

Trang 77/286

Gio trnh Visual Basic

B. Cu Trc Lp For While Do While


1. Cu trc lp FORNEXT

Ch : C th s dng lnh Exit For thot sm khi vng lp FOR V d: Vit chng trnh nhp vo 1 s (n) v tnh tng: S=1+2+3+n Trang 78/286 Gio trnh Visual Basic

(doi kp txt thanh dang chuoi)

V d: Vit chng trnh nhp vo 1 s (n) v xut chui cc s 1 2 3 n ln mn hnh

Trang 79/286

Gio trnh Visual Basic

2. Cu trc lp WHILE LOOP

DO

Ch : Trong vng lp phi c lnh tc ng ln iu kin vng lp khng lp v tn Trang 80/286 Gio trnh Visual Basic

Ch : Vng lp Do While thc hin kim tra u kin trc. Nu tha iu kin th mi thc thi.

V d: V d: Vit chng trnh nhp vo 1 s (n) v xut chui cc s 1 2 3 n ln mn hnh

Trang 81/286

Gio trnh Visual Basic

3. Cu trc lp DO. .. LOOP UNTIL: Ch : Vng lp Do Loop Until khng thc hin kim tra u kin trc, m thc hin lnh trc, sau mi kim tra iu kin quyt nh c thc hin tip hay khng. Do , n thc hin t nht 1 ln.

Trang 82/286

Gio trnh Visual Basic

V d: vit chng trnh nhp s bng hp thoi Inputbox. Khi ngi s dng nhp 1 s, chng trnh hin th vo label lblKetqua. Nu ngi s dng nhp khc s, chng trnh kt thc vic nhp.

Trang 83/286

Gio trnh Visual Basic

V d: Chng trnh xc nh s nguyn t:

Option Explicit Private Sub cmd_giai_Click() Dim i As Integer, n As Integer, so As Integer If txt_nhap.Text <> "" Then so = Val(txt_nhap.Text) n = Int(Sqr(so)) i=2 Do While i <= n And so Mod i <> 0 i=i+1 Trang 84/286 Gio trnh Visual Basic

Loop If i > n Then txt_kq = so & " l s nguyn t" Else txt_kq = so & " khng l s nguyn t" End If Else MsgBox "Bn cha nhp s" txt_nhap.SetFocus End If End Sub Private Sub Cmd_kt_Click() End End Sub

V d : Chng trnh quay s may mn

Qui lut chi nh sau: Khi bt u chi s tin c trong sn my l 10 ng v s tin thng l 0. Mi ln quay s th ngi chi phi b vo 30 ng (s tin c trong my tng 30) Nu kt qu quay s =0 --> ngi chi nhn c cng 10 ng vo tin thng

Trang 85/286

Gio trnh Visual Basic

Nu kt qu quay s =9 --> ngi chi c cng 30 ng vo tin thng Nu kt qu quay s =7 -> ngi chi c cng 100 v 50 % s tin ang c trong my vo tin thng (--> s tin trong my gim 50%). Khi nt Game mi c n th s tin thng phi c gi tr=0

Khi c thng th chng trnh phi hin dng thng bo cho bit s tin thng hin ti l bao nhiu. M lnh ca chng trnh nh sau: Option Explicit Dim thuong As Long, sotien As Long Khai bo bin ton cc

Private Sub Form_Load() sotien = 10 thuong = 0 txt_sotien.Text = sotien Trang 86/286 Gio trnh Visual Basic

End Sub

Private Sub CMD_QUAYSO_Click() Dim i as Long, j as Long, t As Long Randomize If txt_kq.Visible = True Then txt_kq.Visible = False txt_kq.Refresh Cp nht nhng thay i trn textbox Form1.Refresh Cp nht nhng thay i trn Form End If sotien = sotien + 30 txt_sotien = sotien txt_sotien.Refresh For i = 1 To 60 Step 1 J= Timer Do while Timer<J+1 Loop txt1.Text = Int(Rnd() * 10) txt1.Refresh Next i t = thuong If Val(txt1.Text) = 7 Then t = t + 100 + Int(sotien / 5) Trang 87/286 Gio trnh Visual Basic
Ba dng lnh ny c tc dng delay chng trnh

sotien = sotien - Int(sotien / 5) End If If Val(txt1.Text) = 9 Then t = t + 30 If Val(txt1.Text) = 0 Then t = t + 10 If t > thuong Then txt_kq.Visible = True thuong = t txt_kq.Text = "Xin chc mng bn. " & Chr(13) & Chr(10) & "S tin bn c thng l: " & thuong txt_kq.Refresh End If End Sub Private Sub Cmd_newgame_click() thuong = 0 txt_sotien.Text = sotien End Sub Private Sub Cmd_kt_Click() End End Sub

Trang 88/286

Gio trnh Visual Basic

8.

Check Box

L i tng c t cho php ngi s dng chn gi tr bng cch nh du chn (check). Hai trng thi ca check box c s dng nhiu nhtr l: c chn hoc khng c chn.

Cc thuc tnh ca check box: Name: tn ca checkbox vi prefix l Chk Aligment: Quy nh v tr ca nt chn: o o 0: Left justify 1: Right justify

Caption: Tiu (dng vn bn hin k nt chn) Value: Gi tr ca thuc tnh ny quy nh trng thi ca check box o o o 0: Unchecked (khng chn) 1: Check (ang chn) 2: Grayed (khng c php thay i trng thi ca check box.

S kin (event) ca checkbox Click: xy ra khi ngi s dng click trn checkbox Trang 89/286 Gio trnh Visual Basic

bit c Checkbox c chn hay khng, ngi ta kim tra thuc tnh Value V d: Vit chng trnh vi mt checkbox dng cho php mt hnh trn Form c hin th hay khng

9.

Option Button

C cng dng ging nh Chexk box, tuy nhin khi mt nhm cc check box t chung vi nhau th ta c th chn nhiu trng hp trong nhm ny hoc khng chn g c, cn trong mt nhm option button Trang 90/286 Gio trnh Visual Basic

chng ta ch chn c mt trong cc trng hp ny m thi. Cc Option Button i thnh tng nhm. Mi nhm c a vo mt Frame (hay i tng cha khc). V d Trong hp thoi trn c hai nhm Option button, Aligment v Leader th trong mi nhm ta ch chn c mt trng hp.

Cc thuc tnh Option button:

ca

Name: tn ca Option Button vi prefix l Opts Aligment: Quy nh v tr ca nt chn: o o 0: Left justify 1: Right justify

Caption: Tiu (dng vn bn hin k nt chn) Value: Gi tr ca thuc tnh ny quy nh trng thi ca option buton o o True: ang chn False: Khng chn.

Ch : Khi mt option trong nhm c gi tr true th tt c cc option cn li s c gi tr false Trong mt nhm cc option button, bit option button no ang c chn, chng ta kim tra thuc tnh Value ca cc option button

Trang 91/286

Gio trnh Visual Basic

hoc dng ngay s ken click v khi option button va nhn s kin click, th khi gi tr Value=True. V d.To mt chng trnh vi giao din nh sau:

Trn Form c Mt i tng Shape Mt mng Option button t vo trong Frame Shape (To Frame Shape trc, sau mi t cc option button vo frame ny) Mt mng Option button t vo trong Frame Color (To Frame color trc, sau mi t cc option button vo frame ny) Mt Command button vi caption Exit. (Nt ny c thuc tnh Cancel bng True ) dng kt thc chng trnh. Khi mt option trong nhm shape c chn th dng hnh ca i Trang 92/286 Gio trnh Visual Basic

tng Shape s thay i tng ng (s dng thuc tnh shape) Khi mt option trong nhm Color c chn th mu ca i tng Shape s thay i tng ng (s dng thuc tnh Fillcolor v hm Qbcolor) 10. ListBox

i tng ny dng lit k mt danh sch v cho php ngi s dng chn lc cc mc trong danh sch ny.

Cc thuc tnh ca Listbox: ListCount: Cho bit s mc hin c trong ListBox. Thuc tnh ny khng th thay i ic. List: L thuc tnh chnh ca listbox. N lu g cc mc ca listbox. Cc mc ca listbox c th nhp vo ti giai on thit k hoc trong chng trnh. Truy xut cc mc trong listBox thng qua thuc tnh list: Tn_listBox.list(index) Multiselect: o 0: Ch cho php chn mi lc mt mc duy nht o 1: Cho php chn mi lc nhiu mc bng chut Trang 93/286 Gio trnh Visual Basic

o 2: Cho php chn mi lc nhiu mc bng chut v phm shift hoc Control ListIndex: C gi tr l mt con s cho bit mc th my ca listBox ang c chn. Trong trng hp list box c php chn nhiu mc cng lc th thuc tnh ny cho bit th t ca mc u tin c chn. Selected: Cho bit trng thi ca mt mc trong danh sch hin c c chn hay khng. Hoc t trng thi chn cho mt mc no . C php: mc Selected(index) Kim tra trng thi ca mt

o Select(index)=True/False t trng chn hoc khng chn cho mt mc trong danh sch. Text: Ni dung ca mc ang chn trong listbox Sorted: Quy nh cc mc trong ListBox c sp xp theo th t hay khng

Cc phng thc (method) ca Listbox: AddItem: Thm mt mc mi vo danh sch. o C php: Tn_ListBox.AddItem item[,Index] RemoveItem: Xa mt mc trong ListBox o C php: Tn_ListBox.RemoveItem index o Index: s th t ca mc cn xa. Clear: Xa ton b cc mc trong danh sch. Sau khi xa gi tr Listcount=0 C php: Tn_ListBox.Clear Trang 94/286 Gio trnh Visual Basic

V d: Vit chng trnh nhp danh sch sinh vin theo yu cu sau:

Cc ListBox c php chn nhiu mc (kt hp gia phm Shift, Control v chut) Cc nt Cmd1 v Cmd3 Khi c Click s sao chp tt c cc mc ang chn sang ListBox bn kia, sau xa cc mc ang chn Nt lnh Xa cho php xa cc mc ang chn trong Lst1 (ListBox bn tri)

Trang 95/286

Gio trnh Visual Basic

Option Explicit Private Sub Cmd_exit_Click() End End Sub Private Sub Cmd1_Click() If Lst1.ListIndex <> -1 Then Lst2.AddItem Lst1.List(Lst1.ListIndex) Lst1.RemoveItem Lst1.ListIndex End If End Sub Private Sub Cmd2_Click() Dim i As Integer For i = 0 To Lst1.ListCount - 1 Lst2.AddItem Lst1.List(i) Trang 96/286 Gio trnh Visual Basic

Next Lst1.Clear End Sub Private Sub Cmd3_Click() If Lst2.ListIndex <> -1 Then Lst1.AddItem Lst2.List(Lst2.ListIndex) Lst2.RemoveItem Lst2.ListIndex End If End Sub Private Sub Cmd4_Click() Dim i As Integer For i = 0 To Lst2.ListCount - 1 Lst1.AddItem Lst2.List(i) Next Lst2.Clear End Sub Private Sub Command2_Click() Dim Traloi As Byte Traloi = MsgBox("Bn c chn cha ", 32 + 4, "VB msgbox") If Traloi = 6 Then Lst1.Clear End Sub Private Sub Command5_Click() Lst1.AddItem txt1.Text Trang 97/286 Gio trnh Visual Basic

txt1.Text = "" txt1.SetFocus End Sub Private Sub Lst1_DblClick() If Lst1.ListIndex <> -1 Then Lst2.AddItem Lst1.List(Lst1.ListIndex) Lst1.RemoveItem Lst1.ListIndex End If End Sub Private Sub Lst2_DblClick() If Lst2.ListIndex <> -1 Then Lst1.AddItem Lst2.List(Lst2.ListIndex) Lst2.RemoveItem Lst2.ListIndex End If End Sub 11. Combo Box

Combo Box l mt control kt hp gia Textbox v Listbox, nn n c cac c tnh ca c hai i tng ny. Combo Box cho php trnh by, chn cc mc c trong danh sch v c th nhp thm cc mc mi.

Ngoi cc thuc tnh ging nh ListBox, Combo Box cn c thuc tnh c trng l: Trang 98/286 Gio trnh Visual Basic

Style: Dng quy nh kiu ca ComboBox o 0 : Kiu ComboBox chun, ch trnh by listbox khi kch chut vo mi tn bn phi o 1 : Kiu ny lun th hin listbox ca n bn di TextBox o 2 : Cch s dng ging nh mt listbox (Khng th nhp trn textbox) ch khc cch trnh by. Text: Ni dung ang c trong phn Textbox. V d: Vit chng trnh c s dng CheckBox v OptionButton theo yu cu sau: Thm trn Form hai Image c tn Img5 v Img6, mt ListBox c tn Lst1 (nm trong frame peripherals), mt ComBoBox c tn Cmb1(nm trong frame Payment method).

Cc mc ca Lst1 v Cmp1 l: Lst1: Extra hard disk Printer Cmb1: US. Dollars Trang 99/286 Gio trnh Visual Basic

L 1

Check English Pound Cc mc ny c a vo Lst1 v Cmb1 khi Formload (s dng s kin Formload) Khi mc Extra hard disk ca Lst1 c chn th picture trong Img5 l: c:\program files\Microsoft visual studio\ common\ graphics\ metafile\ business \Harddisk.wmf Khi mc Printer ca Lst1 c chn th picture trong Img5 l: Printer.wmf (cng folder vi file hnh trn) Khi mc US.dollars ca Cmb1 c chn th picture trong Img6 l: dollar.wmf Khi mc Check ca Cmb1 c chn th picture trong Img6 l: check.wmf Khi mc English Pound ca Cmb1 c chn th picture trong Img6 l: poundbag.wmf

Trang 100/286

Gio trnh Visual Basic

Option Explicit Private Sub chk1_Click() If chk1.Value = 1 Then img1.Visible = True Else img1.Visible = False End If End Sub

Trang 101/286

Gio trnh Visual Basic

Private Sub Chk2_Click() If Chk2.Value = 1 Then img2.Visible = True Else img2.Visible = False End If End Sub Private Sub Chk3_Click() If Chk3.Value = 1 Then img3.Visible = True Else img3.Visible = False End If End Sub Private Sub Cmb1_KeyPress(KeyAscii As Integer) If KeyAscii = 13 Then cmb1.AddItem cmb1.Text cmb1.Text = "" End If End Sub Private Sub opt1_Click(Index As Integer) Dim i As Byte Select Case Index Trang 102/286 Gio trnh Visual Basic

Case 0 Image2.Picture = LoadPicture("c:\program files\Microsoft visual studio\common\graphics\metafile\business\pcomputr.wmf") Case 1 Image2.Picture = LoadPicture("computer.wmf") ng dn nh trn Case 2 Image2.Picture = LoadPicture("laptop2.wmf") End Select End Sub

Trang 103/286

Gio trnh Visual Basic

12.

Bi Tp

Lu : Mi bi tp nn to trn da mt folder lu nhng tp tin project, Form.. ca bi tp . 1. Vit chng trnh gii phng trnh bc 2 : ax2 + bx + c = 0 2. Vit chng trnh gii phng trnh trng phng: ax4 + bx2 + c = 0 3. Vit chng trnh i nm dng lch qua nm m lch Cho Bit:

Nm MOD 10 = 09: ==> Canh - Tn Nhm Qu Gip At Bnh inh Mu - K Nm MOD 12 = 011: ==> Thn Du Tut Hi T Su Dn Mo Thn T Ng Mi 4. Vit chng trnh tnh tng N s nguyn dng u tin: 1+ 2+ 3+. .N (N l s nguyn nhp t bn phm) 5. Vit chng trnh tnh tng N s nguyn dng chn u tin: 2 + 4 + 6 +... 6. Vit chng trnh nhp vo 2 s nguyn a, b. Xut ra BSCNN v USCLN ca a v b. 7. Vit li chng trnh quay s may mn theo mu sau:

Trang 104/286

Gio trnh Visual Basic

Khi chng trnh bt u chy th s tin c trong my l 100 ng. Mi ln quay s ngi chi phi b vo my 30 ng (s tin trong my tng 30 ng mi ln quay s) Cc hp cha ch s t tri qua phi c tn nh sau: txt1, txt2, txt3. Gi tri trong hp s c to bi cng thc: txt1.Text = Int(Rnd() * 7) txt2.Text = Int(Rnd() * 8) txt3.Text = Int(Rnd() * 10) Nu kt qu ca txt1 = 7 th s tin c thng l 100 +50% s tin ang c trong my (s tin trong my gim 50%) Nu kt qu ca txt2 = 7 th s tin c thng l 30 +10% s tin ang c trong my (s tin trong my gim 10%) Nu kt qu ca txt1 = 7 th s tin c thng l 10. Khi trng thng th s tin thng c cng dn v s hin ln mn hnh cho ngi chi bit s tin thng m h ang c.

Trang 105/286

Gio trnh Visual Basic

Khi nt Game mi c click (bt u cho mt ngi chi khc) th s thng quay v 0

8. Vit chng trnh to mt Form nh sau:

Nt cp nht c thuc tnh Default=true Nt Kt thc c thuc tnh Cancel=true Khi ngi s dng nhp mt s vo textbox ri Enter hoc nhp vo nt cp nht th s ny c thm vo listbox, ng thi ni dung trong textbox b xa v focus c chuyn v textbox Tnh tng cc phn t ca List (hin th kt qu trn msgbox) Xa phn t u v cui Xa phn t ang chn Trang 106/286 Gio trnh Visual Basic

Tng mi phn t ln 2 n v Thay th mi phn t bng bnh phng ca chnh n Chn phn t chn Chn phn t l 9. Vit chng trnh i mu ca Form nh sau: khi chn vo Option no, th mu ca Form s thay i tng ng vi mu ca Option .

10. Vit chng trnh thay i font ch ca textbox Yu cu: Khi chn vo thanh cun, Option, hay CheckBox no, th thuc tnh tng ng ca TextBox s thay i tng ng vi cc mc chn la.

Trang 107/286

Gio trnh Visual Basic

11. Vit chng trnh thay i hnh dng kiu t nn ca hnh (Shape) Cc Option button dng thay i hnh dng hay kiu t nn ca hnh v.

12. Yu cu: Khi chn cc Options ng vi cc quc gia khc nhau, th s hin th c tng ng vi quc gia . (Cc icon hnh nh trong th mc: Graphics\icons\flags)

Trang 108/286

Gio trnh Visual Basic

13. Vit chng trnh theo yu cu: Chn tn ngi bn c sn t combobox Chng trnh t tm s in thoi v a ch.

Trang 109/286

Gio trnh Visual Basic

Trang 110/286

Gio trnh Visual Basic

CHNG 4: TH TC
NI DUNG: HM FORMAT TH TC HM - MNG CHUI MNG I TNG TIMER DEBUG V IN N CHNG TRNH 1. Hm Format

Cng dng ca hm l nh dng mt gi tr theo mt mu no . Kt qu ca hm l chui gi tr nh dng. C php ca hm: Format(Value, Format). Value: Gi tr mun nh dng Format: Mu nh dng, s cha nhng k t nh dng nh: 0 - Ti v tr ny l mt ch s. Nu con s m chng ta cn nh dng khng c gi tr ti v tr ny th ch s 0 s c in vo. o V d: Format$(123,0000)=0123 # - Quy nh ti v tr l mt con s bt k. . - Du ngn cch phn thp phn vi phn nguyn , - Du cch ngn % - Nu c k hiu ny trong chui nh dng th con s s c nhn ln 100 v thm k hiu % pha sau

Trang 111/286

Gio trnh Visual Basic

Ngoi cc k t trn nu trong chui nh nh dng cha bt k mt k t no khc th k t s c chn vo trong chui kt qu.

Cc v d: Format$(8315.4,######.##)=8315.4 Format$(8315.4,###,###.00)=8,315.40 Format$(8315.4,$######.00)=$8,315.40 Format$(0.5 ,000%)=050% 2. Th Tc Hm

Ngoi cc th tc s kin gn lin vi cc i tng i khi ngi lp trnh cn phi xy dng thm cc th tc ca ring mnh d dng trong vic lp chng trnh, chng hn nh gim bt m lnh, trnh vit lp i lp li mt on lnh no . 1. Th tc (Sub): Ta c th vit mt th tc trong ca s Code vi c php khai bo nh sau: Private Sub <tn th tc>[(danh sch tham s)] Lnh ca th tc End sub Th tc c khai bo vi t kha private th ch c th dng c trong phm vi Form cha n m thi. Nu mun th tc c th s dng trong ton b chng trnh (cc Form khc u s dng c) th phi khai bo vi t kha Public Public Sub <tn th tc>[(danh sch tham s)] Trang 112/286 Gio trnh Visual Basic

Lnh ca th tc End sub V d: Private Sub Hienthi(x As Integer) Print x*x*x Sub Li gi th tc: Ta c th dng lnh call gi thc hin mt th tc theo c php: call <tn th tc>

Tham s ca th tc: Tham s ca th tc c khai bo trong ngoc sau tn th tc. Nu l tham s dng tham tr th c thm t kha byval trc tn tham s. Gi tr khng th thay i sau khi ra khi hm. Nu l tham s dng tham bin th c thm t kha byref trc tn tham s. Gi tr c thay i sau khi ra khi hm. Nu khng ch r l tham tr hay tham bin th xem nh tham bin. Trang 113/286 Gio trnh Visual Basic

V d: Chng trnh gi hm vi tham bin (byref) v tham tr (byval) Trong hm sub1(A as Integer,Byval B as Integer) th A l tham bin, B l tham tr. Do , ch c kt qu ca txt3 khc gi tr txt1 (gp 10 ln) cn gi tr ca txt4 v txt2 l bng nhau.

Trang 114/286

Gio trnh Visual Basic

V d: vit chng trnh m s ln Click trn mt Form. Khi ngi s dng Click chut trn Form, chng trnh s tng bin m s click, v reset v 0 khi s ln click qu 4. o Chng trnh s dng hm CheckNumberofClick vi 1 tham bin l Numclick. y khng cn khai bo byref do mc nh l byref.

1. Hm (Function): Vic vit mt hm cng ging nh th tc ch khc phn khai bo v trong ni dung ca hm s c mt cu lnh tr gi tr v cho hm. Ta c th vit mt hm trong ca s Code vi c php khai bo nh sau: [Private/Public] Function <tn hm>[(danh sch tham s)] As <Kiu d liu>

Trang 115/286

Gio trnh Visual Basic

Lnh ca hm Tn hm=Gi tr tr v End Funtion

V d: Vit chng trnh tnh cnh huyn tam gic s dng function

Trang 116/286

Gio trnh Visual Basic

V d: vit hm tnh giai tha n!=1*2*3*.n v vn dng hm ny vit chng trnh sau y.

Option Explicit Private Function Giaithua(ByVal K As Integer) As Double Dim I As Integer Dim Kq As Double Kq = 1 For I = 1 To K Kq = Kq * I Next Giaithua = Kq End Function

Private Sub Cmd_exit_Click() End End Sub Private Sub Cmd_N_Click() Txt_ketqua = Txt_nhaplieu.Text & "!= Format(Giaithua(Val(Txt_nhaplieu.Text)), "#") Txt_nhaplieu.Setfocus End Sub " &

Private Sub Cmd_n1_Click() Trang 117/286 Gio trnh Visual Basic

Dim N As Integer N = Val(Txt_nhaplieu.Text) Txt_ketqua = N & "! /" & N + 1 & "! =" & Format(Giaithua(N) / Giaithua(N + 1), "0.###") Txt_nhaplieu.Setfocus End Sub Ch l, nt lnh th hai, nu th chng ta thy rng kt qu khng cn tnh di dng nh vy m ch l 1/(n+1). Tuy nhin, chng trnh ny c m t cch s dng l nhiu ln khi vit mt hm. V d: cho bit kt qu xut ra sau khi thc hin chng trnh sau y l g

Trang 118/286

Gio trnh Visual Basic

3.

Mng

Mng tnh 1 chiu


Khai bo: Dim ArrayName(LowerIndex To UperIndex) As <Kiu d liu> V d: Dim strTen(0 to 25) As String: strTen(0), strTen(1), strTen(25) Dim SoThuc(1 to 100) As Double: SoThuc(1), SoThuc(2) SoThuc(100) Dim Volume(-10 to 10) As Long: Volume(-10), Volume(-9), Volume(10) V d: Mng numbers cha cc s nguyn c nh s t 0..5

Lu : Khi truy xut mng phi truy xut ng vi cn trn v cn di khai bo. V d: nu khai bo Dim a(10 to 20) Th khng c phn t a(1),a(2)..a(9).

xc nh cn di ca mng dng Lbound(TnMng) :

Hm

Xc nh cn trn ca mng: Hm Ubound(TnMng) : Text1.Text=Ubound(StrTen)

Trang 119/286

Gio trnh Visual Basic

Khi khi to, cc gi tr trong mng c khi to mc nh bng 0 (i vi kiu s) hay bng chui rng (i vi kiu chui). Truy xut mng truy xut mt phn t trong mng ta truy xut theo c php TenMang(ch s) duyt ht mng, s dng vng lp For..Next vi 1 bin chy V d: Vit chng trnh x l mng nh sau

Nhp xut mng: Option Explicit Dim a(1 To 10) As Integer Sub Cmd_nhap_Click() Dim i As Integer Txt_xuat.Visible = False For i = 1 To 10 Step 1 a(i) = Val(InputBox("a(" & Str(i) & ")= ", "Nhap mang")) Next End Sub

Sub Cmd_xuat_Click() Dim i As Integer Txt_xuat.Visible = True

Trang 120/286

Gio trnh Visual Basic

For i = 1 To 10 Txt_xuat.Text = Txt_xuat.Text & Str(a(i)) & " " Next End Sub

Private Sub Form_Load() Txt_xuat.Visible = False End Sub

V d: Nhp v xut mng ra ListBox Khi chng trnh va thc hin:

Khi nt nhp mng c bm: Trang 121/286 Gio trnh Visual Basic

Khi nhp xong:

Trang 122/286

Gio trnh Visual Basic

Khi bt Xut mng c n:

Mng nhiu chiu tnh


C th l mng 2 chiu, 3 chiu. Mng 2 chiu c th c coi nh ma trn.

Khai bo: Tng t mng 1 chiu: o Dim a(1 to 2,1 to 3) As Integer o A(1,1), A(1,2), A(1,3) ; A(2,1), A(2,2), A(2,3) Xc nh cn di ca mng: Hm Lbound(TnMng,chiu) : Text1.Text=Lbound(A,1) Xc nh cn trn ca mng: Hm Ubound(TnMng, chiu) : Text1.Text=Ubound(A,2) Khi duyt mng nhiu chiu s dng cc vng lp lng vo nhau. Trang 123/286 Gio trnh Visual Basic

V d: duyt na trn ma trn 2 chiu, khng tnh ng cho chnh

V d: duyt na trn ma trn 2 chiu, tnh c ng cho chnh

Trang 124/286

Gio trnh Visual Basic

Mng ng
Mng tnh l mng c s phn t khng i sau khi khai bo mng Mng ng l mng c s phn t c th thay i c sau khi khai bo mng, trong qu trnh thc thi chng trnh. Khai bo mng ng: o Dim A() as Integer Thay i s phn t trong mng: tng t khai bo mng o ReDim A(5 To 10) o ReDim A(1 to 10) Do , sau khi khai bo mng ng : Dim A() as Integer, cha th dng ngay m phi khai bo s phn t mng ng lc u tin: ReDim A(10). n lc no cn thay i kch thc v tr mng th ReDim li. Xc nh cn di Lbound(TnMng,chiu) ca mng: Hm

Xc nh cn trn ca mng: Hm Ubound(TnMng, chiu)

Private Command1_Click() ReDim b(3) For i = 0 To 3 b(i) = i

Sub

Private Sub Command4_Click() ReDim b(5) For i = 0 To 5 b(i) = i Next

Trang 125/286

Gio trnh Visual Basic

Next End Sub

End Sub

V d:

Chng trnh nhp mng ng bng InputBox

Khi nt Nhp mng c chn:

M lnh:

Option Explicit Dim a() As Integer Dim n As Integer Private Sub Cmd_ketthuc_Click() End Trang 126/286 Gio trnh Visual Basic

End Sub Private Sub Cmd_nhap_Click() Dim i As Integer n = Val(InputBox("nhap mang bao nhieu phan tu")) ReDim a(n) Txt_xuat.Visible = False For i = 1 To n a(i) = Val(InputBox("a(" & Str(i) & ")= ", "Nhap mang")) Next End Sub Private Sub Cmd_xuat_Click() Dim i As Integer Txt_xuat.Visible = True Txt_xuat.Text = "" For i = 1 To n Txt_xuat.Text = Txt_xuat.Text & Str(a(i)) & " " Next End Sub Private Sub Form_Load() Txt_xuat.Visible = False End Sub

Trang 127/286

Gio trnh Visual Basic

4.

Cc Thut Ton C Bn

Cng dn
Lp ln lt mt thao tc, trong cc gi tr c dn vo mt bin. Trc khi thc hin cng dn, gn gi tr bin dn bng gi tr khi to. (Nu tnh tng th khi to thng l bng 0, nu tnh tch th khi to thng l bng 1. Gi tr khi to ph thuc vo thut ton).

V d: thut ton cng dn cho chui cc s Tnh tng Sum = 1 + 2+ 3 +n

V d: s dng thut ton cng dn cho kiu string Chng ta xut mt mng cc s nguyn vo textbox

Trang 128/286

Gio trnh Visual Basic

V d: cng dn cho mng mt chiu Tng t cho vic tnh cng dn trn chui cc s

Cng dn theo iu kin


Lp ln lt mt thao tc, trong cc gi tr c dn vo mt bin nu tha iu kin no . Trc khi thc hin cng dn, gn gi tr bin dn bng gi tr khi to. Chng trnh s c lnh r nhnh (Ifthen, Select case) lng trong vng lp. V d: Cng dn trn mng 1 chiu

Trang 129/286

Gio trnh Visual Basic

V d: Cng dn trn mng 2 chiu: cn 2 vng lp

Tm kim tun t
Thc hin lp li vic duyt ln lt tng phn t trong danh sch. Nu tha mn iu kin th lu li v tr cn tm (v c thc hip lp tip hay khng ty thut ton. C th thot sm vng lp ngay sau khi tm thy).

V d: tm kim phn t trong mng

Trang 130/286

Gio trnh Visual Basic

Tm phn t nh nht ln nht

V d: tm gi tr ln nht trong mng

Trang 131/286

Gio trnh Visual Basic

Sp xp mng

Sp xp (Sort) mt danh sch l vic thng xuyn ca mt ngi vit chng trnh. C nhiu k thut sp xp khc nhau. Tuy nhin, trong ni dung mn hc ny yu cu bn ch cn nm r mt thut ton dng cho vit sp xp trn mng. Vic sp xp tng hay gim do cch so snh gia cc phn t trong mng. V d: sp xp mng 1 chiu tng dn Cho mng mt chiu Arr vi khi to nh sau

Thc hin sp xp mng Arr tng dn. Lu cc gi tr u v cui ca i v j.

Trang 132/286

Gio trnh Visual Basic

5.

S Dng Mt S Hm X L Chui

Khai bo chui: Dim Bin as string th y l bin chui c chiu di thay i v n c th cha khong hn 2 t k t. Hm Len, Left, Right, Mid: C php: Len(S): Cho bit chiu di thc ca chui S Left(S,N): Ly N StringExpression k t bn tri ca chui

Right(S,N): Ly N k t bn phi ca S Mid(S,M,N): Ly N k t ca S bt u t v tr th M

V d: Vit chng trnh x l chui nh sau Khi chng trnh bt u thc hin: Trang 133/286 Gio trnh Visual Basic

Khi nt Nhp d liu vo danh sch c chn:

Khi d liu c nhp vo textbox v Enter (tng ng nt cp nht c chn): Khi nt chn d liu theo mu c chn:

Trang 134/286

Gio trnh Visual Basic

M lnh: Option Explicit Private Sub Cmd_boChon_Click() Lbl_nhap.Caption = "Mu d liu: " txt_nhap.Text = "" cmd_capnhat.Caption = "&B chn" txt_nhap.Visible = True Lbl_nhap.Visible = True txt_nhap.SetFocus End Sub Private Sub Cmd_cMau_Click() Lbl_nhap.Caption = "Mu d liu: " txt_nhap.Text = "" Trang 135/286 Gio trnh Visual Basic

cmd_capnhat.Caption = "&Chn" txt_nhap.Visible = True Lbl_nhap.Visible = True txt_nhap.SetFocus End Sub Private Sub Cmd_cTat_Click() For i = 0 To Lst_dulieu.ListCount - 1 Lst_dulieu.Selected(i) = True Next End Sub Private Sub Cmd_ketthuc_Click() End End Sub Private Sub Cmd_nhap_Click() Lbl_nhap.Visible = True Lbl_nhap.Caption = "Nhp d liu: " cmd_capnhat.Visible = True cmd_capnhat.Caption = "&Cp nht" txt_nhap.Visible = True txt_nhap.Text = "" txt_nhap.SetFocus End Sub Private Sub Cmd_capnhat_Click() Trang 136/286 Gio trnh Visual Basic

Dim i As Integer If cmd_capnhat.Caption = "&Cp nht" Then Lst_dulieu.AddItem txt_nhap.Text txt_nhap.Text = "" txt_nhap.SetFocus End If If cmd_capnhat.Caption = "&Chn" Then For i = 0 To Lst_dulieu.ListCount - 1 If UCase(Left(Lst_dulieu.List(i), UCase(txt_nhap.Text) Then Lst_dulieu.Selected(i) = True End If Next End If If cmd_capnhat.Caption = "&B chn" Then For i = 0 To Lst_dulieu.ListCount - 1 If UCase(Left(Lst_dulieu.List(i), UCase(txt_nhap.Text) Then Lst_dulieu.Selected(i) = False End If Next End If End Sub Private Sub Form_Load() Trang 137/286 Gio trnh Visual Basic Len(txt_nhap.Text))) = Len(txt_nhap.Text))) =

Lbl_nhap.Visible = False txt_nhap.Visible = False cmd_capnhat.Visible = False Lst_dulieu.Clear Cmd_nhap.TabIndex = 0 End Sub

6.

G Ri Chng Trnh

Khi ta chy th chng trnh bng cch bm F5 , nu chng trnh c li th chng trnh s dng li v hin ra hp thoi cho bit chng trnh b li g:

Lc ny nu ta bm End th chng trnh kt thc, Nu bm Debug th mn hnh s quay v ca s lnh v dng lnh pht sinh li s c mu khc. i khi dng ny khng c li, nhng do li cu lnh no pha trn lm cho cu lnh ny khng thc hin c. Khi tm dng (Debug) th ca s Immediate hin ra. Ca s ny cho php ta thc hin tng cu lnh pht hin ra li. Mun xem gi tr Trang 138/286 Gio trnh Visual Basic

ca mt bin hoc mt thuc tnh th ta g tn bin hoc thuc tnh cn xem sau du ?. Da vo cc gi tr ny ta c th ln ra c li.

7.

In n

Khi cn in ni dng ca mt chng trnh ta chn lnh:

File Print th hp thoi in xut hin: nhga cc mc trong hp thoi: Range: Selection:In phn ang chn Current module: In m lnh ca module ang chn Project Curent: In ni dung ca Project hin hnh Print What: In m lnh ca chng trnh Form as text: In thng tin v Form v cc i tng trn Form. Print Quality: Cht lng in Print to file: In kt qu ra mt tp tin

Trang 139/286

Gio trnh Visual Basic

8.

Mng i Tng

Khi t mt control mi ln Form nu ta t tn cho n trng vi mt i tng c, hoc khi ta to mt i tng mi bng cch sao chp t mt i tng c th VB s hi chng ta c mun to cc i tng ny thnh mt mng hay khng?

Nu chng ta chn Yes th cc i tng ny s c cng tn v khi chng s c phn bit thng qua tn v ch s (index). V d: nu chng ta c mt option button tn opt sau ta sao chp thnh mng 3 option button na th cc nt s c phn bit nh sau: opt(0), opt(1), opt(2), opt(3). phn bit mt i tng c hat ng ch mng i tng hay khng, chng ta xem xt thuc tnh Index ca n. Nu Index l rng, th i tng l dng mng. Trang 140/286 Gio trnh Visual Basic

Khi mun to mt mng cc i tng ta cng c th lm bng cch to ra cc i tng ny sau t tn ca chng ging nhau Lu : Trong cc th tc s kin ca mt mng i tng lun c mt tham s index cho bit s kin xy ra trn thnh vin th my ca mng. Cn c trn gi tr Index ny, chng ta bit c i tng no trong mng control kch hat s kin .

V d: Vit chng trnh i mu cho control shape Shp t mng cc option button

y chng ta gi s cc Option button c t vo mng i tng c tn l Opt2 c ch s l Opt(0) Opt(3). Khi , c 3 button ny u c chung hm s kin l Private Sub Opt2_Click(Index As Integer) Private Sub Opt2_Click(Index As Integer) Select Case Index Case 0: Trang 141/286 Gio trnh Visual Basic

Shp1.FillColor = QBColor(12) Case 1: Shp1.FillColor = QBColor(1) Case 2: Shp1.FillColor = QBColor(5) Case 3: Shp1.FillColor = QBColor(14) End Select End Sub Phn lnh chn lai hnh (shape) ca shape Shp sinh vin t thc hin bng mng. 9. i Tng Timer

L b m thi gian. Sau mt khang thi gian nht nh do ngi lp trnh ch nh, timer s t ng thc thi hm s kin Timer_Timer. Sau mt khong thi gian nht nh (do ngi lp trnh ci t ti thuc tnh Interval) n s pht ra mt s kin gi l s kin thi gian - Timer_Timer, v ngi lp trnh s vit lnh thc hin cc x l mi khi s kin ny xy ra. Khi ta t i tng ny ln Form n s khng hin ln mn hnh. Cc thuc tnh ca timer: Enabled: Cho php hoc khng cho php Timer pht s kin thi gian

Trang 142/286

Gio trnh Visual Basic

Interval: thi gian nhy ca timer. n v l Milisecond (1/1000 giy) Ch : timer hat ng th: Thuc tnh Enabled=True Thuc tnh Interval>0 V d: Vit chng trnh cho 1 lablel t ng di chuyn t tri sang phi ca Form Sub Timer1_Timer() Lbl1.Left=Lbl1.Left + 50 End Sub V d: Vit chng trnh vi ct n giao thng t ng chuyn n theo th t Xanh Vng -

Option Explicit Private Sub Form_DblClick() Timer1.Enabled = False End Sub Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) If Shift = 0 Then Image1.Move X, Y Else Timer1.Enabled = True End If Trang 143/286 Gio trnh Visual Basic

End Sub Private Sub Timer1_Timer() Static count As Integer DoEvents If count = 0 Then Image1.Picture = LoadPicture("c:\program files\microsoft visual _ studio\common\graphics\icons\traffic\trffc10a.ico") count = count + 1 ElseIf count = 1 Then Image1.Picture = LoadPicture("\trffc10b.ico") count = count + 1 Else Image1.Picture = LoadPicture("\trffc10c.ico") count = 0 End If End Sub

Trang 144/286

Gio trnh Visual Basic

10.

Bi tp

1. Chng trnh tm c s ca mt s nhp vo

Khi chng trnh bt u thc hin th textbox nhp s c Setfocus (Gi : Dng s kin FormLoad v thc hin lnh TextBox_name.TabIndex=0. Lu trong s kin ny khng th s dng Phng thc SetFocus) Sau khi nhp s nu n Enter hoc Click vo nt thc hin th hin kt qu ln TextBox kt qu, v TextBox nhp vn c Focus Nu n Esc hoc click vo nut1 kt thc th dng chng trnh

Trang 145/286

Gio trnh Visual Basic

2. Vit chng trnh lm ton c bn theo yu cu sau:

Lu : Xy dng cc hm tnh 1+2+..+N, 1*2+2*3+..+N*(N+1)

Tnh

3. Vit chng trnh s dng mng vi cc chc nng c trnh by trn Form nh sau: Vic nhp gi tr cho cc phn t ca mng thc hin bi inputbox Lm li bi trn vi mng ng v gi tr ca mng c to ngu nhin.

Trang 146/286

Gio trnh Visual Basic

4. Vit chng trnh c giao din v thc hin cc yu cu sau:

Giao din: Trang 147/286 Gio trnh Visual Basic

Khi Form c load ln mn hnh th ch c nt Nhp d liu, nt Kt thc l s dng c, v nt Nhp d liu c Focus. Gi : S dng s kin Formload, cho gi tr Enabled ca cc i tng mun m bng false, Gn thuc tnh Tabindex ca nt nhp d liu =0 Khi nt Nhp d liu c n th cc nt lnh c sng ln, xa ni dung ca Textbox nho liu v Setfocus cho textbox ny (s dng phng thc setfocus). V d hnh bn di l khi nt Nhp d liu c n. (hnh 1) Khi mt trong cc nt (ngoi tr nt Nhp d liu v nt Kt thc) c chn th kt qu s hin ln textbox v cc nt lnh s b m ngay c nt va n. V d hnh bn di l kt qu khi nt inch hoa c n. (hnh 2)

(Hnh 1)

Trang 148/286

Gio trnh Visual Basic

(Hnh 2) Trong cc th tc mun ly mt k t ra x l ta dng hm mid vi s k t cn ly l 1.

5. Da vo chng trnh cc v d trn chui ( hc trn lp) hy to mt chng trnh c trnh by v thc hin thao nhng yu cu sau:

Trang 149/286

Gio trnh Visual Basic

ListBox cha d liu c thuc tnh Sorted=True Khi khi ng th thuc tnh Enabled ca nt phc hi = false Chc nng nhp d liu vo danh sch ging nh v d trn lp Chc nng chn d liu theo mu c thm phn: Nu ni dung ca mu chn =* th chn tt c cc mc ang c trong ListBox d liu Chc nng b chn d liu theo mu c thm phn: Nu ni dung ca mu chn =* th b chn tt c cc mc ang c trong ListBox d liu Chc nng xa cc mc ang chn s xa cc mc ang chn trong ListBox d liu. (xem li thut gii xa cc mc trong listbox cc v d trc). Tuy nhin c th

Trang 150/286

Gio trnh Visual Basic

phc hi cc mc xa th trc khi xa mt mc ta phi lu ni dung ca mc ny vo mt mng. Sau khi xa th s bt thuc tnh Enabled ca nt phc hi =True Gi : Khai bo mt mng ng kiu chui (di khong 25 k t) v mt bin SPT kiu Integer. Cc bin ny l nhng bin ton cc. Trc khi xa mt mc th a ni dung ca vo mt phn t ca mng ny ri tng bin SPT ln 1. Chc nng phc hi s s dng phng thc AddItem a cc mc ang c trong mng ton cc vo LixtBox d liu. Sau khi a ht cc phn t ca mng vo ListBox th Redim mng ton cc v 0 v cho gi tr ca bin SPT v 0, ng thi cho thuc tnh Enabled ca nt phc hi = false

6. Vit chng trnh nhp N s t nhin u tin: t 1N vo ListBox. Khi ngi s dng chn 1 s, th cc c s t nhin ca s c chn c hin th trong ListBox th 2.

Trang 151/286

Gio trnh Visual Basic

7. Vit chng trnh nhp N s t nhin u tin: t 1N vo ListBox. Khi ngi s dng chn 1 s trong Listbox th nht, th s c chn c phn tch thnh tha s nguyn t v c hin th trong ListBox th 2

Trang 152/286

Gio trnh Visual Basic

8. Vit hm tnh n! ( n!=1.2.3..n ) S dng hm trn tnh chui: S = 1 + 1/2! + 1/3! + 1/4! + +1/n! 9. Vit hm kim tra 1 s c phi l s nguyn t hay khng? (Function KiemTraSNT(n): tra v 0 nu n khng phi l s nguyn t, 1 nu n l s nguyn t.) 10.S dng hm s trn tm M s nguyn t u tin(M>0: s nhp vo) 11. Vit hm tnh tng cc c s ca 1 s nguyn dng n nhp vo (TongUoc(6)=6) 12. Vit hm gii phng trnh bc 1. (GiaiPTB1(a,b,x1) as Integer). Hm ny tr v 0 nu VN, 1 nu 1 nghim, v 2 nu VSN. 13. Bt ch t ng i mu (nhp nhy) t xanh -> . Tc nhy do ngi s dng chnh trn thanh cun, (Cc icon bt ch trong th mc: Graphics\icons\writings)

Trang 153/286

Gio trnh Visual Basic

14. Vit chng trnh lm mt trng xoay trn. Tc nhy do ngi s dng chnh trn thanh cun, (Cc icon trong th mc: Graphics\icons\elements)

15. Vit chng trnh: qu bng di ln xung khng ngng trong mt Form.

16.Vit hm tm phn t l s nguyn t trong tt c cc phn t ca mng.

Trang 154/286

Gio trnh Visual Basic

17. Vit chng trnh nhp vo mt dy n s thc a[0], a[1],, a[n-1], sp xp dy s theo th t t ln n nh. In dy s sau khi sp xp. 18. Vit mt hm o ngc th t cc phn t ca mt mng s thc: 19. Tm phn t ln nht/nh nht trong mng. 20. Vit chng trnh to ra ma trn vung n x n, vi n c nhp vo (3<=n<=10) v gi tr cc phn t trong mng l ngu nhin(t 1 n 100). In mng s va c ra mn hnh. Sau : in ra phn t ln nht phn t b nht tng cc phn t ca ma trn in ra cc phn t trn ng cho chnh ca ma trn in ra cc phn t chn cc phn t l c trong ma trn in ra cc s c xut hin trong dy

21. Vit chng trnh nhp vo mt mng, hy xut ra mn hnh: Phn t ln nht ca mng. Phn t nh nht ca mng. Tnh tng ca cc phn t trong mng. 22. Vit chng trnh nhp vo mt mng s t nhin. Hy xut ra mn hnh: Dng 1 : gm cc s l, tng cng c bao nhiu s l.

Trang 155/286

Gio trnh Visual Basic

Dng 2 : gm cc s chn, tng cng c bao nhiu s chn. Dng 3 : gm cc s nguyn t. Dng 4 : gm cc s khng phi l s nguyn t

23. Vit chng trnh tnh tng bnh phng ca cc s m trong mt mng cc s nguyn. 24. Vit chng trnh thc hin vic o mt mng mt chiu. V d : 1 2 3 4 5 7 9 10 o thnh 10 9 7 5 4 3 2 1. 25. Vit chng trnh nhp vo mng, hy xut ra mn hnh: Dng 1: Phn t m ln nht ca mng. Dng 2: Phn t dng nh nht ca mng. Dng 3: Tng cc phn t c cn bc hai nguyn. Dng 4: Gm cc s l, tng cng c bao nhiu s l. Dng 5: Gm cc s chn, tng cng c bao nhiu s chn. Dng 6: Gm cc s nguyn t. Dng 7: Gm cc s khng phi nguyn t.

26 Vit chng trnh nhp vo mt s nh hn 1000. Trnh by dng ch cho bit gi tr ca s .

Trang 156/286

Gio trnh Visual Basic

CHNG 5: CHNG TRNH NHIU FORM


NI DUNG: CHNG TRNH C NHIU FORM FORM MDI SPLASH FORM MENU CC HM V D LIU NGY GI

1.

Chng Trnh Nhiu Form

Thm Form vo project


Mt chng trnh c th cha nhiu Form. thm mt Form vo chng trnh ta chn Project AddForm. T ca s Add Form chn biu tng Form > Open

Trang 157/286

Gio trnh Visual Basic

Ta cng c th thm mt Form c vo Project bng cch: Chn Project - Add File Chn tp tin Form c sn.

Trang 158/286

Gio trnh Visual Basic

2.

Qun l Project

Khi mt Project c nhiu Form, module, d dng trong vic qun l chng ta thng s dng ca s Project Explorer hin ca s ny ta chn: View Project Explorer (hoc Ctrl+R)

Trong ca s ny cha cc thnh phn ang c ca Project nh Form, Module Ta c th cho ni dung ca mt thnh phn no hin ln mn hnh bng cch nhp kp trn tn thnh phn . Ta cng c th Click phi trn mt thnh phn ri chn chn chc nng cn thit nh: View Object, View Code, Save, Save As, Remove..

Lu : Khi ta remove mt thnh phn no trong ca s Windows Explorer th xem nh tp tin lu thnh phn ny trn da cng b xa.

3. Vn s dng bin, th tc v i tng c trong cc Form


Bin Th tc : Bin, Th tc khai bo trong mt Form no Trang 159/286 Gio trnh Visual Basic

mun s dng c trong cc Form khc th ta phi khai bo vi t kha Public V d: Public V as integer Public Sub1() .... End sub Lu : o Cc bin Public phi khai bo trong phn General ca Form. Mun truy xut n mt bin hoc gi mt th tc Public trong Form khc ta phi thm tn Form cha bin v th tc pha trc v cch nhau bi du chm. V d: o Form2.v=100 Gn gi tr 1000 vo bin V c khai bo trong Form2 o Form2.sub1 Truy xut n cc i tng ca Form khc: Mun truy xut n cc i tng v thuc tnh ca i tng nm trn mt Form khc ta phi t thm tn Form pha trc tn i tng v cch nhau bi du chm. V d: Form2.text1.text=DurianGroup Form2.shape.Fillcolor=QBColor(Rnd() * 10)

Trang 160/286

Gio trnh Visual Basic

4. Quy nh Form thc hin trc


Khi mt Project c nhiu Form ta phi quy nh Form no thc hin trc (Fom s hin ln mn hnh u tin khi chng trnh thc hin). Cch thc hin: Chn menu Project Project properties Chn General Chn tn Form chnh trong Combo box Star Object

5. Hin Form ln mn hnh


Khi chng trnh thc hin n ch hin Form chnh ln mn hnh. Trong qu trnh x l, nu mun mt Form khc xut hin ta phi gi phng thc Show ca Form theo c php : tnForm.show Trang 161/286 Gio trnh Visual Basic

Mt Form c hin th ln mn hnh theo mt trong hai trng thi: Modeless: Sau khi Form c show ln mn hnh cc cu lnh sau lnh show s c thc hin tip. y l trng thi mc nhin. Modal: Nu Form c Show theo trng thi ny th sau khi Form hin ra th chng trnh ch lm vic vi Form ny cho n khi no Form ny ng li th mi thc hin tip cc cu lnh sau show. Thng thng cc hp thoi trong cc chng trnh ng dng c Show trng thi Modal. hin th Form trng thi modal ta thm s mt (1) hoc hng s VBmodal sau show o Vd: Form2.Sow 1 Np v hy Form Lnh Load: Lnh ny load mt Form vo b nh nhng khng hin Form ln mn hnh. Lnh ny thng c s dng khi ta cn ly cc gi tr c to ra khi Form ny c load vo b nh m khng cn n hin ln mn hnh. Lnh Unload: Khi mun hy b mt Form ra khi b nh, chng ta dng lnh Unload theo c php Unload <tn Form> o V d: unload Form2 Trong trng hp ch mun lm cho Form khng hin th trn mn hnh nhng vn tn ti trong b nh ta c th dng lnh hide o V d: Form2.Hide

Trang 162/286

Gio trnh Visual Basic

T kha Me: T kha ny c dng thay th cho tn Form trong cc cu lnh c vit trong Form ny V d: Trong Form2 thay v ghi cu lnh unload Form2 ta c th ghi unload me

V d: Vit chng trnh s dng 2 Form. Form u s dng iu khin Form th 2 nh cc m t di y: Trong Form th 2 c 1 bin ton cc V v 1 hm Sub1 Form th 1 s gi v hin th Form 2 dng Modelesss Form th 1 s thay i bin V ca Form 2 Form th 1 s gi hm Sub1 ca Form 2 Form th 1 i mu nn Form 2

Code ca Form1 Option Explicit Private Sub cmd_show_Click() Form2.Show 'vbModal Cmd1.SetFocus End Sub Private Sub Cmd1_Click() Form2.V = Form2.V + 1000 End Sub

Trang 163/286

Gio trnh Visual Basic

Private Sub Cmd2_Click() Form2.Sub1 End Sub Private Sub Cmd3_Click() Form2.Shape1.FillColor = QBColor(Rnd() * 10) End Sub Private Sub Cmd4_Click() Unload Form2 End End Sub Private Sub Form_Click() Trang 164/286 Gio trnh Visual Basic

cmd_show.Caption = Form2.V + 2000 End Sub Private Sub Form_Load() Load Form2 End Sub

Code ca Form2

Option Explicit Public V As Integer Private Sub Cmd_unload_Click() Unload Form2 'unload me 'Hide End Sub Private Sub Form_Load() V = 1000 txt1.Text = V End Sub Public Sub Sub1() txt1.Text = V End Sub

Trang 165/286

Gio trnh Visual Basic

3.

FORM MDI (Multi Document Interface)

1. c im ca mt MDI Form: L mt ca s m bn trong c th cha nhiu Form con khc (cc Form dng SDI (Single document interface). Trong mt Project ch c th c mt Form MDI duy nht Cc Form con ca Form MDI lun nm bn trong Form MDI v kch thc lun nh hn hoc bng Form MDI Khi mt Form con ca Form MDI c cc tiu th icon ca n nm trn Form MDI ch khng nm trn Desktop Khi di chuyn Form MDI th cc Form con ca n s di chuyn theo

2. a Form MDI vo project: Chn Project Add MDI Form

Trang 166/286

Gio trnh Visual Basic

3. Cc thuc tnh v phng thc c bn ca Form MDI: Thuc tnh Active Form: Cho php truy xut n Form ang trong trng thi hot ng Phng thc Arrange: Sp xp cc Form bn trong Form MDI o C php Arrange n n: l mt trong cc gi tr t 0 --> 3 cho bit cch sp xp. 4. Quy nh mt Form l Form con ca Form MDI: quy nh mt Form l Form con ca Form MDI th ta t thuc tnh MDIchild ca Form ny l true

Trang 167/286

Gio trnh Visual Basic

5. To mt Form mi trong chng trnh: Khi chng trnh ang chy, nu mun to thm mt hay nhiu Form mi ging nh mt mu Form thit k ta thc hin cc dng lnh sau: Dim <Tn bin> As New <Tn Form mu> Sau nu mun np Form mi vo b nh dng lnh: Load < Tn bin> Mun hin Form ln mn hnh <tn bin>.Show

V d: Dim frm as new Form2 Load Form2 Frm.Show 4. Splash Screen

L mt Form thng xut hin trn mn hnh trong thi gian vi giy khi chng trnh va khi ng. Thng tin trn Form ny tm tt mt vi thng tin v chng trnh, ngoi ra Form ny cn c mc ch lp bt khong trng trong khi cc phn ca chng trnh ang np vo b nh (Form ny s c np ch Modeless) Mc ch ca Splash Screen Gii thiu thng tin v phn mm, tc gi phn mm Lm cho ngi s dng bt c cm gic chng trnh khi ng chm hoc khng chy Trang 168/286 Gio trnh Visual Basic

Mt splash screen c cc c tnh: Hin ln mn hnh trong mt khong thi gian no ri t ng li nhng ch Form chnh ca chng trnh. Trong thi gian ny nu c mt phm c n, hoc click chut th splash screen cng c ng li. (Ty chn) To Spalsh Screen Mt Project c splash screen th thng c khi u bng th tc sub main (khai bo trong module) chn mt module vo chong trnh ta thc hin: Project add module Ta c th t to ra Form ny theo ring hoc c th s dng chc nng : o Project Add Form Splash Screen o Sau thay i mt s mc cho va

Trang 169/286

Gio trnh Visual Basic

n nh thi gian xut hin ca Form trn mn hnh ta c th s dng i tng Timer

V d: Chng trnh sau gm mt splash screen, mt MDI Form, mt Form mu c tn Form1, mt module1 trong c Sub main()

Option Explicit Sub Form_KeyPress(KeyAscii As Integer) Unload Me End Sub Sub Form_Unload(Cancel As Integer) MDIForm1.Show End Sub Private Sub Frame1_Click() Unload Me End Sub Sub Timer1_Timer() Unload Me End Sub

Option Explicit Private Sub MDIForm_DblClick() Trang 170/286 Gio trnh Visual Basic

Me.Arrange 2 End Sub Private Sub MDIForm_click() Static n As Integer Dim newForm As New Form1 n=n+1 newForm.Caption = "Form thu" & Str(n) newForm.Show End Sub

Module1.bas Option Explicit Sub main()

frmSplash.Show Load MDIForm1 End Sub

5.

To Menu Cho Mt Form

Menu l i tng thng c x dng do tnh phn cp v s gn gng ca n. to menu cho mt Form ta thc hin cc sau: Chn Tools Menu Editor (Hoc bm Ctrl_E) Trang 171/286 Gio trnh Visual Basic

Nhp ni dung ca menu vo caption nu mun to phm tt th thm & pha trc k t mun to phm tt. Nu mun to mt vch ngang trn menu th caption du tr t tn cho menu vo Name. Nu c nhiu mc menu trng nhau th ta phi quy nh thm index ca mi mc menu Nhp vo nt next ri lp li cc bc trn cho mt mc menu mi. Nu mun mt mc menu l con ca mt menu khc th chn trc khi nhp ni dung ca menu Dng cc phm mi tn sp xp li v tr cc menu Sau khi nh ngha xong menu th click vo nt OK

Trang 172/286

Gio trnh Visual Basic

Vit lnh cho menu: Trn mn hnh thit k click chut vo mc mun vit lnh th ca s code hin ra ta vit lnh Cc thuc tnh ca menu Thuc tnh Caption: c th thay i lc Run Time hoc Design Time Thuc tnh Enabled: o Khi mun mt Menu tr nn mt tc dng (b m, khng chn c), s dng thuc tnh Enabled v ch nh gi tr FALSE: MnuFileExit.Enabled = False Thuc tnh Visible: o Khi mun n mt Menu, s dng thuc tnh Visible v ch nh gi tr FALSE. Khi menu m b n i, cc menu con cng b n theo: o MnuFile.Visible = False Thuc tnh Checked: o Mt menu c th cha 1 Check mark. Check mark thng c s dng nh 1 Option chn la c 2 trng thi: On hay Off. Khi mun hin mt Check mark, s dng thuc tnh Checked v ch nh gi tr TRUE. V d: Vit chng trnh gm c mt menu Format Bold dng nh dng mt textbox l in m hay in thng. Khi ngi s dng click vo menu Bold, th ty theo trng thi ang chn ca menu Bold m textbox v menu s chuyn sang trng thi cn li.

Trang 173/286

Gio trnh Visual Basic

6.

End Sub

Context menu Menu xut hin khi ngi s dng b, right- click S dng Lnh Popupmenu: o Lnh ny cho xut hin menu ti v tr hin hnh ca mouse C php Popupmenu <tn menu> V d: Chng trnh sau c 3 Form: splash Form, MDI Form, Form1.

Trang 174/286

Gio trnh Visual Basic

Trong Form1 l Form mu. Trn MDIForm c mt menu cho php to ra Form mi, ng Form v kt thc chng trnh Ni dung lnh ca Form splash , module1.bas cng ging nh bi trc

Option Explicit Dim n As Integer Private Sub MDIForm_DblClick() Me.Arrange 2 End Sub Private Sub MDIForm_click() Dim newForm As New Form1 n=n+1 newForm.Caption = "Form thu" & Str(n) newForm.Show Trang 175/286 Gio trnh Visual Basic

End Sub Private Sub MDIForm_Load() n=0 End Sub Private Sub MDIForm_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) If Button = 2 Then PopupMenu mnu_qlForm End Sub Private Sub mnu_end_Click() End End Sub Private Sub mnu_tao_Click() Dim newForm As New Form1 n=n+1 newForm.Caption = "Form thu" & Str(n) newForm.Show End Sub Private Sub mnu_xoa_Click() If n > 0 Then Unload MDIForm1.ActiveForm n=n-1 End If End Sub

Trang 176/286

Gio trnh Visual Basic

7.

Cc Hm V D Liu Ngy Gi Hm Date Tr v ngy h thng (dng th hin ph thuc vo Regional setting) Hm Now tr v ngy gi h thng (12/07/97 10:41:10 PM) Cdate(Variable or Property or Constant ) Chuyn mt gi tr (chui hoc s) sang dng date. o V d: CDate("12/31/1899")=#12/31/1899# o cdate(1)=#12/31/1899#

Trang 177/286

Gio trnh Visual Basic

8.

Bi tp

1. Thm vo cc chng trnh lm Form flash cho bit mc ch ca chng trnh, tn ngi thc hin, ngy thc hin. Form ny t ng thot khi mn hnh sau mt thi gian nht nh hoc khi c mt phm/chut c n. 2. To menu v chng trnh son tho vn bn vi giao din v thng menu nh sau:

Trang 178/286

Gio trnh Visual Basic

3. Da vo chng trnh Timer hc trn lp vit chng trnh c giao din v yu cu nh sau:

Vi h thng menu nh sau

Cc mc trong Color ( White, Blue, Red, Magenta, Yelow) c to thnh mt mng c ch s t 0 n 4. Trang 179/286 Gio trnh Visual Basic

Cc mc trong Size of Ball (Small, Medium, Large) cng to thnh mt mng c ch s t 0 n 2. S dng Updown control Khi Form c load vo b nh th: Gn thuc tnh Backcolor cho cc option button theo mu nh Form trn (mu : 15, 2, 12, 5,14). Gn thuc tnh Value ca Option button th nht trong Option group = true (chn mu trng) Cn thuc tnh check ca menu white =true Gn thuc tnh Fillcolor ca qu banh = thuc tnh Backcolor ca Option button th nht (mu trng) Cho thuc tnh ListIndex ca combobox =0 Gn thuc tnh check ca menu Small=true Tc ca qu banh c th thay i bi: Updown Nhp trong Textbox tc Nhp bi inputbox khi chn speed trn menu option.

Lu : Tc ca qu banh khi thay i phi l mt tc hp l (10<=tc<=1000). Mi khi tc b thay i th vic thay i ny phi th hin trn textbox v thuc tnh value ca updown. Mu ca qu banh c chn trn Option button group hoc chn trn menu Option/color/.. Trang 180/286 Gio trnh Visual Basic

Mi khi ngi s dng chn mu th option button mu tng ng phi c chn v menu mu tng ng phi c du check (cc menu cn li phi khng c du check) Tng t nh mu ca qu banh, kch thc qu banh cng c chn bi Combo box hoc menu v combo box, menu phi th hin c kch thc hin ti ca qu banh.

Trang 181/286

Gio trnh Visual Basic

Trang 182/286

Gio trnh Visual Basic

CHNG 6: S KIN PHM


NI DUNG: CC S KIN PHM S KIN MOUSE BY LI COMMON DIALOG 1. Cc S Kin Phm Cc s kin ny ch xy ra trn i tng ang focus. Khi mt phm c n lun xy ra 3 s kin theo th t: KeyDown, Keypress, Keyup. C th c nhiu s kin Keydown xy ra nhng ch c mt s kin Keyup khi ngi s dng n v gi phm mt lc ri mi bung phm. Cc th tc x l cho cc s kin ny c dng nh sau:

Private Sub <tn i tng>_KeyDown(KeyCode As Integer, Shift As Integer) End Sub

Private Sub <tn i tng>_KeyUp(KeyCode As Integer, Shift As Integer) End Sub

Trang 183/286

Gio trnh Visual Basic

Keycode: Cho bit code ca phm c n. Lu mi phm trn bn phm u c mt Keycode c nh khng ph thuc vo trng thi ca cc phm chc nng nh Shift, Control, Capslock. Shift: Cho bit trng thi ca cc phm Shift, Control, Alt c c n hay khng. o Shift=0: Khng c phm no trong 3 phm k trn c n o Shift=1: Phm Shift c n o Shift=2: Phm Ctrl c n o Shift=4: Phm Alt c n

Private Sub <tn i tng>_KeyPress(KeyAscii As Integer)

End Sub KeyAscii: Cho bit m Ascii ca phm c n S kin keypress thng c s dng khi cn nhn dng cc phm k t v tham s KeyAscii cho bit chnh xc l k t no c n (phn bit ch hoa ch thng, phn bit trong trng hp phm Shift c n km vi cc phm c hai k hiu. Trong khi x l s kin ny nu ta gn li gi tr mi cho KeyAscii cng c ngha l sa phm c n trn i tng.

Trang 184/286

Gio trnh Visual Basic

V d: Chng trnh sau y minh ha vic x l cc s kin xy ra trn mt Textbox. Txt1 ch cho nhp s. Txt2 khi nhp ch ci th i sang ch hoa.

Khi bm Ctrl-R trn txt2 : mu ch l mu Khi bm Ctrl-B trn txt2: Mu ch l mu xanh

Option Explicit Private Sub Command1_Click() End End S ub

Private Sub txt1_KeyPress(KeyAscii As Integer) If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0 'Xoa phm End Sub Trang 185/286 Gio trnh Visual Basic

Private Sub txt2_KeyDown(KeyCode As Integer, Shift As Integer) If Shift = 2 Then Select Case KeyCode Case 82: txt2.ForeColor = QBColor(4) Case 66: txt2.ForeColor = QBColor(2) End Select End If End Sub Private Sub txt2_KeyPress(KeyAscii As Integer) Dim kytu As String kytu = Chr(KeyAscii) kytu = UCase(kytu) KeyAscii = Asc(kytu) End Sub

2.

S Kin Mouse

Mousedown, Mousemove, Mouseup

Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) End Sub Trang 186/286 Gio trnh Visual Basic

Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) End Sub Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) End Sub

S kin MouseDown

Gii thch Xy ra khi ngi s dng nhn 1 nt chut bt k

MouseUp

Xy ra khi ngi s dng th 1 nt chut bt k

MouseMove

Xy ra khi con tr chut di chuyn n 1 im mi trn mn hnh

Form hay Object u c th bt c s kin chut khi con tr chut i ngang qua.

Tham s truyn

Tham s Button Shift x,y

Gii thch Cho bit nt chut no c nhn Cho bit phm SHIFT hay CTRL hay ALT c nhn V tr con tr chut vi h ta ca i tng bt s kin Trang 187/286 Gio trnh Visual Basic

V d: Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) Label1.Move X, Y End Sub

Gi tr Button VbLeftButton =1 VbRightButton =2

Gii thch Left Button: phm tri chut c bm Right Button: phm phi chut c bm

VbMiddleButton =4

Middle Button: phm gia chut c bm

V d: Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) If Button = 1 Then hay Button=vbLeftButton Label1.Move X, Y End If If Button = 2 Then Label2.Move X, Y End If If Button = 4 Then Label2.Move 0, 0 Trang 188/286 Gio trnh Visual Basic

Label1.Move 0, 0 End If End Sub

Gi tr Shift VbShiftMask =1 VbCtrlMask = 2 VbAltMask = 4

Gii thch SHIFT: phm SHIFT c nhn CTRL: phm CTRL c nhn ALT: phm ALT c nhn

Ch : Nu mt t hp cc phm trn c nhn ng thi th gi tr SHIFT s l tng cc gi tr ca cc phm: v d: phm CTRL v ALT c nhn ng thi th Shift=6

V d: Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) Label2.Caption = Shift End Sub=============================================

V d: Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) If Shift = 1 Then Label2.Caption = "Nut SHIFT duoc bam" Trang 189/286 Gio trnh Visual Basic

ElseIf Shift = 2 Then Label2.Caption = "Nut CTRL duoc bam" ElseIf Shift = 4 Then Label2.Caption = "Nut ALT duoc bam" Else Label2.Caption = "To Hop phim duoc bam" End If End Sub=============================================

V d: Vit chng trnh hin th ta chut khi chut di chuyn trn Form. Chng ta s dng s kin MouseMove Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) Label1.Caption = "(X, Y) = " & str(X ) & ", " & str(Y) End Sub 3. X L Li Run Time Error

Li Runtime Error l nhng li pht sinh khi chng trnh ang thc hin. V d nh trong chng rnh c lnh load mt tp tin t da, nhng tp tin ny khng tn ti. Nu trong chng trnh ca chng ta khn c phn x l li th chng trnh phi kt thc ngay ti thi im ny. Cc lnh x l li: Trang 190/286 Gio trnh Visual Basic

On Error Goto <LineLabel>: Cu lnh ny thng c t trc cc lnh c th gy ra li (t by li). N c ngha Nu c li xy ra th i n dng nhn c tn c ch ra ri bt u thc hin cc lnh ngay di dng nhn ny. Mt dng nhn l mt chui cc k t c km theo du : pha sau. Lnh On Error goto ch c tc dng trong mt th tc hoc hm. On Error Resume Next: Khi c li th b qua On Error goto 0 : tt by li

i tng Err: y l mt i tng c sn trong VB. C hai thuc tnh m gi tr s c to ra khi c li l o Number (Cho bit m li) o Description (l chui vn bn m t li xy ra).

V d: trong chng trnh trn ta thm vo phn x l li trong hai th tc ca nt lnh Load v Luu

Private Sub Load_Click() Dim filename As String On Error GoTo Mess filename = InputBox("Cho biet ten tap tin") Rtxt1.LoadFile (filename) Trang 191/286 Gio trnh Visual Basic

Rtxt1.SetFocus Mess: 'Exit Sub MsgBox Err.Description End Sub

Private Sub Luu_Click() Dim filename As String On Error GoTo Mess filename = InputBox("Cho biet ten tap tin") Rtxt1.SaveFile (filename) Mess: 'Exit Sub MsgBox Err.Description End Sub

4.

Common Dialog

i tng ny h tr ngi lp trnh khi cn hin mt hp thoi cho ngi s dng chn khi thc hin cc thao tc: Mu Font ch, Open File Save Save Trang 192/286 Gio trnh Visual Basic

Print

Ch c mt Common Dialog nhng ph thuc vo phng thc Show i tng ny s h tr mt trong nhng chc nng trn. Tn Open file Save file Color Font Print Help Phng thc Showopen Showsave Showcolor ShowFont ShowPrint ShowHelp

Tn ca i tng ny trong hp thoi Component l: Microsoft Common Dialog Control 6.0 i tng ny khng hin trn Form khi chng trnh chy, tr khi ta gi phng thc Show ca n. Dialog dng chn Font: s dng Dialog cho vic chn font ta cn thit lp gi tr cho cc thuc tnh nh sau(cc thuc tnh ny thng thng c ci t trong chng trnh trc khi Show i tng ny): Flag: Thuc tnh ny quy nh dng trnh by ca dialog font. Gi tr ca n l mt hoc tng cc cc hng sau: Trang 193/286 Gio trnh Visual Basic

Hng CdlCFANSIOnl y CdlCFBoth CdlCFPrinterFo nts CdlCFScreenFo nts CdlCFEffects

Gi tr 1024 3 2 1 256

Cng dng Ch hin cc font chun ca Windows, khng hin cc font dng biu tng Hin tt cc cc font dng cho my in ln mn hnh Ch hin nhng Font dng cho my in Ch hin nhng font dng cho mn hnh Cho php chn cc c tnh ca font (Underline, Strikethrough) Cho php quy nh cc con s trong danh sch chn c ch nm trong khong xc nh bi hai thuc tnh Max v Min

CdlCFLimitSize

8192

Max, Min: Hai thuc tnh ny quy nh kch thc nh nht v ln nht hin trong Dialog font. Hai thuc tnh ny ch c tc dng khi thuc tnh Flag c cha gi tr CdlCFLimitSize (8192) Dialog hin font ta gi <tn Dialog>.Showfont Sau khi Dialog Font hin ln ngi s dng chn th ta c th truy xut n cc thuc tnh Fontname, Fontsize, FonBold, FonItalic, FontUnderline, Fonstrikethru Trang 194/286 Gio trnh Visual Basic

V d: Hy xem on lnh vit cho nt i Font ca v d trn Private Sub CMD_FONT_Click() C.Flags = 256 + 3 C.FontName = Rtxt1.SelFontName C.FontBold = True C.ShowFont Rtxt1.SelFontName = C.FontName Rtxt1.SelFontSize = C.FontSize Rtxt1.SelColor = C.Color Rtxt1.SelStrikeThru = C.FontStrikethru Rtxt1.SelItalic = C.FontItalic Rtxt1.SelBold = C.FontBold Rtxt1.SelUnderline = C.FontUnderline Rtxt1.SetFocus End Sub

Dialog chn File: s dng Dialog cho vic chn File (trong thao tc Open hoc Save) ta cn thit lp gi tr cho cc thuc tnh nh sau(cc thuc tnh ny thng thng c ci t trong chng trnh trc khi Show i tng ny): Dialog Title: Gi tr thuc tnh ny l mt chui quy nh tiu ca Dialog chn file. Trang 195/286 Gio trnh Visual Basic

InitDir: L mt chui cho bit da v th mc Dialog s trnh by khi mi hin ln mn hnh l da v th mc no. o V d: InitDir=C:\Windows Flag: Thuc tnh ny quy nh cch trnh by Dialog chn File. Thng thng n c t bng 0 (hoc 3 trong trng hp mun hin checkbox Open as read only trong hp thoi) hin hp thoi Open File ta gi phng thc ShowOpen theo c php: o <Tn Dialog>.Showopen hin hp thoi Save File ta gi phng thc ShowSave theo c php: <Tn Dialog>.ShowSave Sau khi Show hp thoi ta c th da vo cc thuc tnh sau thao tc: o Filename : Cha ng dn, tn tp tin chn o Filetitle: Cha tn tp tin chn Ta thng s dng thuc tnh FileName cho cc thao tc Open v Save V d: on m lnh cho nt Lu v Load vit li bng cch s dng Common Dialog

Trang 196/286

Gio trnh Visual Basic

Private Sub Luu_Click() C.Flags = 0 C.InitDir = "D:\" C.DialogTitle = "Save As" C.ShowSave Rtxt1.SaveFile (C.filename) End Sub

Trang 197/286

Gio trnh Visual Basic

5.

Bi tp

1. Vit chng trnh nhp liu, sau khi nhp xong trong mi Text Box, bm phm Enter chuyn Focus qua Text Box k tip. TextBox a ch sau khi nhp xong s chuyn Focus qua Command nhp liu (xa cc d liu nhp), v li chuyn Focus v Text H v Tn.

2. Vit chng trnh luyn tp s dng chut: Picture hin th ngu nhin trong chng trnh trong 1 thi gian nht nh (khong 1 giy) v nu ngi chi bm trng Picture v ng phm th im cng thm 1, nu khng trng th im c tr i 1.

Trang 198/286

Gio trnh Visual Basic

CHNG 7:
NI DUNG:

PHN NNG CAO

CNG C WINDOWS COMMON CONTROLS: CNG C IMAGELIST TOOLBAR UPDOWN CONTROL- SLIDER THAO TC TRN TP TIN

1.

Windows Common Control

L tp hp cc control ph bin cung cp bi Windows vi tn ca mc ny l: Microsoft Windows common controls... Cc cng c ny h tr ngi lp trnh trong vic to ra cc giao din thn thin vi nhng ngi s dng quen vi mi trng Windows. Sau y l mt s cng c thng dng: 2. ImageList

Mt control dng lu tr, qun l cc hnh nh. N thng c dng lu tr cc hnh nh ca nhng control khc nh Toolbar, Listview, Treeview, Image combo a hnh vo ImageList ti thi im thit k:

Trang 199/286

Gio trnh Visual Basic

Click phi vo biu tng ImageList trn Form Chn Properties phiu General cho php ta chn kch thc nh lu trong ImageList. Thuc tnh ny ch c chn trc khi a hnh nh vo. Nu chng ta chn nhiu hnh nh ng thi, cc nh pha sau s c nh dng theo nh u tin Nn a cc biu tng (ICO) vo ImageList v kch thc nh ca n. Hn ch a file hnh ln (BMP, JPG), v n s lm file EXE c kch thc ln v khi ng chm

Trang 200/286

Gio trnh Visual Basic

Phiu Image cho php a hnh nh vo ImageList. Click vo nt Insert Picture sau chn tp tin hnh nh. C th chn nhiu tp tin cng lc. Vi mi hnh nh a vo s c mt s Index qun l. Ngi lp trnh cng c th qun l n bng thng qua mt chui k t bng cch s dng thuc tnh Key (thuc tnh ny c phn bit ch hoa ch thng). Nt Remove cho php xa mt hnh nh trong ImageList. Vic ny ch thc hin c khi ImageList cha kt hp vi cc Control khc Cc tp tin nh gn vi cc nt cng c thng dng c t trong th mc: c:\program files\microsoft bitmaps\tlbr_w95 visual studio\common\graphics\

Trang 201/286

Gio trnh Visual Basic

3.

Toolbar

Thanh cng c dng cc nt lnh to mt thanh cng c, trc ht ta phi to mt ImageList cha cc hnh nh gn vi thanh cng c. a thanh cng c ln Form Right Click trn thanh cng c chn Properties

Trong phiu General ta n nh cc thuc tnh: o Mousepointer: Biu tng ca con tr chut khi khi Drag trn menu

Trang 202/286

Gio trnh Visual Basic

o ImageList:Tn ca Imagelist cha hnh nh gn vi cc nt trn thanh cng c o DisabledImageList:Tn ca Imagelist cha hnh nh gn vi cc nt trn thanh cng c khi nt lnh c thuc tnh Enabled bng false o HotImageList: tn ca Imagelist cha hnh nh gn vi cc nt trn thanh cng c khi Drag chut trn nt cng c. o ButtonHeight, ButtonWidth: Kch thc ca nt. o Appearance: Dng th hin ca thanh cng c. o Style:Dng th hin ca nt lnh o Textaligment:L ca text trn nt lnh. Trong th Button cho php thm, loi b nt lnh.

Trang 203/286

Gio trnh Visual Basic

Insert Button:Thm nt mi Remove Button: Loi b mt nt lnh Index: Ch s ca mt nt lnh trn thanh cng c. Ch s ny dng truy xut n nt lnh trong chng trnh. N c to ra mi khi ta chn mt nt mi. Caption: Ni dung hin trn nt lnh Style: o tbrDefault:c s dng ging nh mt mc trong menu. Khi nt cng c c Click th mt on lnh c thc hin o tbrCheck: c dng trong trng hp mun ci t gi tr ca mt thuc tnh no c gi tr True/False o tbrButtonGroup: n nh nt cng c ny thuc vo mt nhm nt cng c. Khi ti mt thi im ch mt trong cc nt cng c ny c chn. o tbrSeparator: Ngn cch gia cc nhm nt cng c. o tbrDropDown: Khi click vo nt cng c ny s xut hin Buttonmenu c gn km vi n. Mt nt c style l DropDown th ta phi nh ngha mt menu button cho n.

CC S KIN TRN TOOLBAR: ButtonClick: Xy ra khi ngi s dng click vo mt nt trn thanh cng c. Trang 204/286 Gio trnh Visual Basic

Private Sub tbtoolbar_ButtonClick(ByVal MSComctlLib.Button) End sub

Button

As

o S kin ny c mt tham s: Button cho bit Key ca nt c n

ButtonDropDown: Xy ra khi ngi s dng n vo mi tn ca mt nt cng c kiu dropdown. Private Sub tbtoolbar_ButtonDropDown(ByVal MSComctlLib.Button) End Sub o S kin ny c mt tham s: Button cho bit caption ca nt c n ButtonMenuClick: xy ra khi ngi s dng click vo mt mc ca ButtonMenu Private Sub tbtoolbar_ButtonMenuClick(ByVal ButtonMenu As MSComctlLib.ButtonMenu) Button As

End Sub o S kin ny c mt tham s: Button cho bit text ca mc menu c n

To ToolBar bng cng c Wizard s dng chc nng ny ta thc hin cc bc sau: Chn menu Add-in Add in Manager: Trang 205/286 Gio trnh Visual Basic

Double click vo mc VB 6 Application Wizard Click vo nt OK.

By gi chn mc Toolbar Wizard trong menu Add-in to thanh cng c. Trang 206/286 Gio trnh Visual Basic

4.

Updown control

L cng c dng tng hoc gim mt gi tr s. Khi ta kt ni value ca n vi mt thuc tnh ca i tng khc th thuc tnh ca i tng lin kt s ph thuc vo ga tr ca thuc tnh value Mt s thuc tnh c trng ca Updown: Aligment: Quy nh v tr ca Updown so vi v tr ca i tng m n lin kt.

Buddy Control: Tn ca i tng c thuc tnh lin kt vi thuc tnh value Updown Buddy Property: Thuc tnh mun lin kt vi thuc tnh value ca updown Value: Gi tr ca updown hin ti. Ta cng c th thay i gi tr ca thuc tnh ny. Trang 207/286 Gio trnh Visual Basic

Min, Max : Gi tr n nht v ln nht ca Updown Increment: Gi tr mi ln tng hoc gim khi click chut vo updown. 5. Slider

Cng c cng dng tng hoc gim mt gi tr s mi khi click chut hoc ko chut trn thanh trt. Thuc tnh Value ca i tng ny thng c dng thay i gi tr thuc tnh ca mt i tng khc.

Mt s thuc tnh ca Slider: Value:Cho bit gi tr ca Slider, c th thay i gi tr ca slider bng cch gn gi tr mi cho thuc tnh ny. Max, Min: Gi tr nh nht, ln nht ca thuc tnh value Smallchange, Largechange: Gi tr thay i mi khi Drag chut hoc Click chut trn Slider 6. Lm vic vi tp tin (file)

Tp tin l mt dy cc byte lu trn a. Khi ng dng truy cp n tp tin, n xem cc byte nh l k t, bn ghi,s, chui...C 3 kiu truy cp tp tin: Tun t: c v ghi cc tp tin vn bn theo cc chui lin tc. Trang 208/286 Gio trnh Visual Basic

Ngu nhin: c v ghi tp tin vn bn hoc nh phn c cu trc theo cc bn ghi c chiu di xc nh Nh phn: c v ghi cc tp tin c cu trc khng xc nh.

Trong ni dung gio trnh ny ch cp n lai tp tin tun t. M tp tin tun t Open pathname For | Input | Output | Append | As filenumber | Len = buffersize| Khi m mt tp tin tun t vi Input, tp tin phi c sn. Nu khng Visual basic s bo li. Khi ta m mt tp tin cha c sn vi Output hoc Append lnh Open s to mi tp tin v m n. Tham s tu chn Len ch ra s k t trong vng m khi sao chp d liu gia tp tin v chng trnh. Sau khi m tp tin vi Input, Output hoc Append ta phi ng n bng lnh Close. c ni dung tp tin vo chui M tp tin vi Input. Sau , dng dng lnh line Input #, Input() hoc Input # sao chp ni dung tp tin vo bin. Visual basic cung cp hm Line c ghi 1 k t hoc mt dng vo tp tin. V d: on code sau c tng dng: Dim LinesFromFile, NextLine As String Do Until EOF(FileNum) Line Input #FileNum, NextLine LinesFromFile = LinesFromFile + NextLine + Chr(13) + Chr(10) Trang 209/286 Gio trnh Visual Basic

Loop Mc d Line Input# nhn ra cui chui khi n c n k t xung dng, nhng n khng ly k t ny khi a v bin. Ghi chui v tp tin ghi ni dung bin vo tp tin, m tp tin vi Output hay Append, sau dng Print #. V d, ghi ni dung ca textbox vo tp tin: Print #FileNum, Thebox.text Visual basic cng h tr lnh Write #, dng ghi mt danh sch cc s, chui vo tp tin. Du phy c dng tch bit: Dim strString as String, intNumber as Integer strString = AnyCharacters intNumber = 23456 Write #Filenum, strString, intNumber V d sau y lu danh sch cc phn t trong combobox cbo1 vo file text c:\DurianGroup.txt

Trang 210/286

Gio trnh Visual Basic

CHNG 8: TY BIN MI TRNG IDE CA VISUAL BASIC


NI DUNG: DANH SCH PREFIX TY BIN GIAO DIN IDE S DNG TOOLBAR CC SHORTCUT MT S TH THUT KHC

Khi lm vic vi VB, c nhiu chn la tng tc thit k - pht trin chng trnh. 1. Danh sch cc Prefix c ngh nn s dng

2.

ng Nhng Ca S Ph Sp xp mn hnh lm vic cho thun li nht. Trang 211/286 Gio trnh Visual Basic

V d, khi ang son tho trong ca s Code, bn khng cn ToolBox (hp cng c), ca s Project Explorer, ca s Properties, hay ca s Form Layout. Cc ca s u c th nhanh chng c m, nn chng ta nn ti u mn mn hnh lm vic trong mt thi im cho ln nht. Chng ta c th xem xt mn hnh Code ca 2 giao din di y: mt giao din mc nh, v mt giao din c ti u.

Trang 212/286

Gio trnh Visual Basic

3.

S dng Toolbar Standard

S dng Toolbar Standard hin th nhanh cc ca s, ty bin mn hnh lm vic. S dng ToolBar Standard ca VB :

Trang 213/286

Project Explorer Windows

Properties Windows

Form Layout Windows

Object Brower Windows

Gio trnh Visual Basic

ToolBox

4. S

S Dng Short Cut (Phm Tt) Ca Vb Hin Th Cc Ca

Project Explorer Window Properties Window Form Window Code Window Di chuyn Focus gia cc ca s

Ctrl + R F4 Shift + F7 F7 Ctrl + Tab

5.

Th Ni Cc Ca S

Kha hay Th ni thng qua Menu: [Tool]-[Option], v chn Tab [Docking]

Ca s c Dock (c chn) v khng Dock: Trang 214/286 Gio trnh Visual Basic

6.

S Dng Edit Toolbar

Bt tt Edit ToolBar bng Menu: [View]-[ToolBar]-[Edit]

Clear All Bookmarks Previous Bookmark Next Bookmark Toggle Bookmark

List Constants List Properties/Methods

Block Comment: S dng chuyn 1 on m lnh (chn chng trong ca s Code) thnh li ch gii Trang 215/286 Gio trnh Visual Basic

Qick Info

Indent Complete Word Parameter Info

Outdent

Toggle Breakpoint

UnComment Block Comment Block

(comment). S dng tt 1 on m lnh m khng cn xa chng. Mi dng c thm 1 du vo pha u tri. UndoBlock Comment: Ngc li vi Block Comment, s dng m 1 on m lnh c du vo pha u tri. Cc du c xa i. Toggle Bookmark: thit lp hay xa i cc Bookmark trong m lnh. Cc Bookmark dng nh du cc v tr no , lm cho vic di chuyn gia cc v tr d dng v nhanh chng. Next Bookmark, Previous Bookmark: di chuyn ti v li gia cc Bookmark. Clear All Bookmark: xa tt c cc Bookmark Indent v Out dent: s dng tht hng vo hay ra 1 dng lnh hay khi lnh. Nn tht hng phn cp theo vai tr ca cc lnh khi vit Code, vic ny gip cho chng trnh tr nn trong sng v d sa cha, nng cp.

7.

Thay i Cc Xc Lp Ca Mi Trng VB

S dng Menu: [Tools]-[Option] v chon cc Tab xc lp Ty bin mi trng lm vic trn Visual basic: Tools > Option

Trang 216/286

Gio trnh Visual Basic

Ti th Advanced cho php chng ta chn mc SDI Development Environment dng chuyn mi trng lm vic ca Visual Basic t SDI (Single Document Interface) sang MDI (Multi Document Interface) hay ngc li

Trang 217/286

Gio trnh Visual Basic

Th Editor cho php chng ta ty chn: Auto Syntax Check: t ng kim tra li c php Require Variable Declaraion: yu cu bin phi khai bo. Tng t ch Option Explicit Tab Width: iu chnh kch thc nhy Tab (s khang trng) khi vit code

Th General Show Grid: Hin th li trn Form khi chng ta ang thit k giao din. Li gip chng ta d dng ging hng i tung. Gird Width, Grid Height: kch thc li. Kch thc cng nh, chnh cnh chnh xc, tuy nhin giao din s b ri. Trang 218/286 Gio trnh Visual Basic

8.

S Dng Shortcuts (Phm Tt) Khi Son Tho Code Chc nng Xa dng lnh ti v tr hin thi Xa t pha bn tri Xa t pha bn phi T ng tm v in vo t kha, hay bin Nhy n u Module lnh hin hnh Nhy n cui Module lnh hin hnh Tht dng (Indent) 1 on m lnh Canh dng ra tri (Outdent) 1 on m lnh Nhy n 1 Procedure t tn Procedure (ch s dng cho hm t vit) s dng kim tra li hm t vit Ctrl + Y Ctrl + Back Space Ctrl + Delete Ctrl + Spacebar Ctrl + Home Ctrl + End Chn khi ln, v phm TAB (hay Edit Toolbar) Chn khi ln, v phm Shift+TAB (hay Edit Toolbar) Shift+F2 Phm tt

Trang 219/286

Gio trnh Visual Basic

Quay v v tr ang gi hm (khi ang trong on m nh ngha hm) sau khi s dng Shift+F2

Ctrl+Shift+F2

V mt s phm tt khc khi san tho code Chc nng Xem ca s Code Xem ca s Object Browser Tm kim Thay th Tm tip Tm ngc Chuyn n th tc k tip Chuyn n th tc trc Xem nh ngha Cun xung 1 mn hnh Cun ln mt mn hnh Nhy v v tr trc Chc nng Di con tr sang phi 1 t Di con tr sang tri 1 t F7 F2 CTRL + F CTRL + H SHIFT + F4 SHIFT + F3 CTRL + DOWN ARROW CTRL + UP ARROW SHIFT + F2 CTRL + PAGE DOWN CTRL + PAGE UP CTRL + SHIFT + F2 Phm tt CTRL + RIGHT ARROW CTRL + LEFT ARROW Phm tt

Trang 220/286

Gio trnh Visual Basic

Di con tr v cui dng Di con tr v u dng Ly li hnh ng trc Canh tri Xo tt c cc im dng (break-points) Xem menu cm ng cnh

END HOME CTRL + Z TAB SHIFT + SHIFT + F9

SHIFT + F10

9.

Tch Ca S Son Tho Dng xem mt lc 2 Code Procedure cng 1 lc trong 2 ca s c lp nhau. Thch hp cho chng trnh c nhiu Procedure. S dng Menu: [Winow][Split] hay Split Bar. hy b ch Split Window, Chn Menu: [Winow][Split] hay ko Split Bar ln trn. Trang 221/286 Gio trnh Visual Basic

10.

Ko V Th

S dng kh nng Ko v Th ca chut di chuyn khi lnh: Chn khi lnh v ko n v tr mi. S dng kh nng Ko v Th ca chut Copy khi lnh: Chn khi lnh v ko n v tr cn dn, trong khi ko th gi phm Ctrl. 11. To Nhiu i Tng Nhanh

Khi cn to nhiu i tng cng loi, bnh thng mi ln v phi chn li (bng cch Click chut) i tng t Toolbox. to nhiu i tng cng loi mt cch nhanh chng cc i tng ny ly tn khc nhau, khng phi to mng i tng, khi chn ToolBox cho i tng u tin, gi phm Ctrl khi Click. Vic to cc i tng th 2,3 c tin hnh trong khi vn gi phm Ctrl. to nhiu i tng cng loi mt cch nhanh chng cc i tng trong 1 mng i tng, s dng kh nng Copy v Dn cc i tng. 12. S Dng Form Editor Toolbar

S dng toolbar ny nhanh chng thi k v canh chnh i tng m Form Edittor Toolbar: T Menu: [View] [ToolBar]-[Form Editor] Vi cc nt lnh t tri qua phi: Trang 222/286 Gio trnh Visual Basic

Send to Front: a i tng ln trn i tng khc Send to Back: a i tng xung di i tng khc Align Objects: ging hng. Center Object In Form: cho i tng nm gia Form Make Same Size: iu chnh kch thc nhm i tng Lock Controls On Form: Kha i tng trn Form. Khng cho php di chuyn.

13.

iu Chnh i Tng Bng phm

iu chnh v tr i tng bng bn phm (keyboard) Chn i tng cn iu chnh Gi phm Ctrl, v chn phm di chuyn (ln, xung, qua tri, sang phi) di chuyn i tng theo hng c chn. (Mi ln di chuyn s qua ng li k tip) iu chnh kch thc i tng bng bn phm (keyboard) Trang 223/286 Gio trnh Visual Basic

Chn i tng cn iu chnh Gi phm Shift, v chn phm di chuyn (ln, xung, qua tri, sang phi) thay i kch thc i tng theo hng c chn. 14. S Dng Colot Palette chn mu

Visual Basic cung cp cho chng ta cng c Color Palette chn mu (nn, font ch) khi thit k giao din. hin th Color Palette, chn menu View-> Color Palette. Chng ta chn lai (font ch hay mu nn) sau ch vic chn mu sc cho cc i tng trn Form.

15.

S Dng Shortcuts Thc Thi Mt Project

Trang 224/286

Gio trnh Visual Basic

Chc nng Thc thi Project hin hnh (From Design Time) Tip tc Thc thi Project (From Break Time) Khi ng li Project (From Break Time) Dng Project ang thi hnh Bt u thc thi Project thng qua tng dng lnh n (Dng cho vic kim tra v Debug) F5 F5

Phm tt

Shift + F5 Ctrl + Break F8

16.

S Dng Object Brower Object Brower ca VB c xem nh mt ngun thng tin quan trng xem xt Objects, properties, methods, Events hay cc hng s c nh ngha sn ca cc i tng VB, cc i tng ca cc ng dng khc VB, cc Active X controls. Object Brower cn l mt bng tng hp v Project hin hnh, vi cc Objects, Properties, Procedures c nh ngha trong Project hin hnh. Thng qua Object Brower nhy n cc phn on Code trong chng trnh m Object Browser: bm phm F2 hoc chn menu View-Object Browser

Trang 225/286

Gio trnh Visual Basic

V d: Tm bng cc hng s mu c nh ngha bi VB M Object Browser Nhp vo t kha mu bt k m chng ta nh (v d: Vbred) Nhn nt tm (hay bm Enter) Object Browser hin th ton b cc hng s v mu trong danh sch

Trang 226/286

Gio trnh Visual Basic

T tm hiu v cc i tng:
Chon oi tng can tm hieu trong bang Classes. Object Brower cua VB hien th cac properties, methods, Events cua oi tng tng ng. Method Icon

Event Icon Property Icon

Chng ta c th s dng Object Brower nhy n mt v tr Code no trong chng trnh


Chon Project trong danh sach Project / Library. Trong ca so Classes se hien th cac Module va Form cua Project. Chon Form can tm. List Box phai se hien th cac ham s kien, ham t nh ngha cua Form tng ng. e nhay en Procedure hay Sub nao, Double Click tren ham tng ng.

Project / Library Box

Trang 227/286

Gio trnh Visual Basic

17.

S dng t kha With

S dng With khi c an lnh lp li nhiu ln vi mt i tng no . With End With gip chng trnh d c, vit nhanh hn, r ngha hn. Khng s dng With Text1.Text = "Test" Text1.FontBold = True Text1.ForeColor = QBColor(13) S dng WithEnd With With Text1 . Text = "Test" . FontBold = True . ForeColor = QBColor(4) End With 18. S Dng gip (Help)

Visual Basic i km vi b gip rt mnh v phong ph, y , v chi tit. ci b Help, chng ta phi ci b a MSDN Library sau khi ci xong Visual Studio. hin gip , chng ta bm phm F1 ngay ti cc control, an lnh hin tr gip ng cnh tng ng

Trang 228/286

Gio trnh Visual Basic

PH LC 1: CI T VISUAL BASIC
ci t Visual Baisc 6.0, chng ta s dng a Visual Studio 6.0 hay cn gi l Visual Studio 98 (1 CDROM). Nu mun ci t thm bn gip (help) cho Visual Basic 6.0, chng ta cn c b a MSDN 98 (2 CDROM).

Sau y s trnh by cc bc c bn ci t Visual Basic 6.0 Bc 1: B a CDROM Visual Studio 98 vo. T menu Start -> Run -> Browse->Chn a CDROM cha chng trnh-> Chn file Setup.exe

Bc 2 Giao din phn ci t xut hin. Chn Next

Trang 229/286

Gio trnh Visual Basic

Bc 3 Mn hnh End User License Agreement xut hin. Chn mc I accept v bm nt Next

Bc 4 Mn hnh nhp Product Number. Nhp vo Serial ca phn mm. (v d: 415-0287065)

Bc 5 Trang 230/286 Gio trnh Visual Basic

Mn hnh cho bn chn Visual Studio 6.0 Professional Edition hay Server Applications. Chn mc u tin, Visual Studio 6.0 Professional Edition. Click Next.

Bc 6 Chng trnh bt u thc hin qu trnh ci t vi li nhn Starting Visual Studio 6.0 Setup, please wait. Chng ta phi i mt vi pht. Bc 7 Chng trnh Setup s nhc bn ng bt c bn Visual Studio no bn ang thc thi. Nu my bn cha ci, th chn Continue.

Trang 231/286

Gio trnh Visual Basic

Bc 8 n bc ny, chng trnh s xc nhn li Product ID ca bn. Click OK.

Trang 232/286

Gio trnh Visual Basic

Bc 9 Nu bn mun ci Visual Studio trong mt folder khc, chn nt Change Folder. Click Custom

Bc 10 y l bc quan trng nu bn mun tit kim a v khng ci cc thn phn khng s dng do Visual Studio c rt nhiu chng trnh v th vin dng cho cc mc ch khc nhau.

Trang 233/286

Gio trnh Visual Basic

Bn c th b chn Visual C++, Visual FoxPro, Visual InterDev, ActiveX, Professional Tools. Bn nn chn :Visual Basic, Data Access, Graphics v Tools (vi du chn) Click nt Continue Bc 11 B ci t s kim tra a v tin hnh ci t phn mm.

Trang 234/286

Gio trnh Visual Basic

Bc 12 Bc k tip bn s c hi v vic ci t b gip MSDN Library. Nu mun ci t th chn Install MSDN v click Next.

Bc 13 Sau khi ci t (hoc khng) b MSDN, chng trnh s hi v vic ci t Install Shield. B qua tin ch ny bng cch khng chn n v bm Next

Bc 14 Chng trnh a ra chn la v vic ci t component cho sever. B qua phn ny bng cch bm nt Next

Trang 235/286

Gio trnh Visual Basic

Bc 15 Bc cui cng bn s c nhc nh v vic ng k vi Microsoft v bn Visual Studio ang s dng. Nu khng mun ng k, b chn Register Now v bm nt Finish.

Qu trnh ci t kt thc.

Trang 236/286

Gio trnh Visual Basic

PH LC 2: BI TP LUYN TP
1. Bi tp tng hp

1. Hy to mt Form trong cha mt menu c cu trc nh sau:

Menu Exit dng kt thc chng trnh. Menu Form LIGHT show Form LIGHT ln mn hnh Menu Form STRING show Form STRING ln mn hnh Menu Form ARRAY show Form ARRAY ln mn hnh Form LIGHT: Form ny dng 3 shape control m phng 3 n tn hiu giao thng. Hy vit code thay i mu ca shape cho ph hp mi khi ngi s dng chn mt color trong Option group bn phi. C th nh sau: Khi ngi s dng chn Option Red th shape th nht c mu , cc shape cn li c mu trng. Tng t nh th cho hai trng hp cn li. Nt Unload Me Unload Form LIGHT

Trang 237/286

Gio trnh Visual Basic

Form ARRAY: Nt To mng (10 100) cho php to gi tr ngu nhin t 10 n 100 cho mt mng 20 phn t, sau xut gi tr ca cc phn t trong mng ln ListBox. Option S chia ht cho 4 hin th gi tr ca cc phn t trong mng m tha iu kin chia ht cho 4 Option S >0 hin th gi tr ca cc phn t trong mng m tha iu kin >0 Option S <0 hin th gi tr ca cc phn t trong mng m tha iu kin <0 Option S chia ht cho N hin th gi tr ca cc phn t trong mng m tha iu kin chia ht cho N vi N c nhp bi hm InputBox Option Tt c cho hin th gi tr ca tt c cc phn t trong mng Nt Unload cho php unload Form ny

Trang 238/286

Gio trnh Visual Basic

Form STRING:

Nt Nhp H v Tn cho php nhp thm mt H tn mi bi InputBox sau a vo ListBox h tn. Nt S ngi c h = ? cho bit c bao nhiu ngi c h ging vi h c nhp vo bi hm InputBox. Nt S ngi c tn = ? cho bit c bao nhiu ngi c tn ging vi tn c nhp vo bi hm InputBox. Trang 239/286 Gio trnh Visual Basic

Cc nt Tch H, Tch Ch Lt, Tch Tn: tch cc thnh phn ca tn va nhp v a vo cc Listbox tng ng. 2. To mt Project tnh cc khon ph tn ca 1 sinh vin trong 1 nm hc. Trnh by ging nh hnh bn. Yu cu: TextBox nhp Tin ch cho php nhp ch s.

Khi nhn nt Thm th thm vo danh sch ng thi xo trng cc gi tr trong Textbox. Nt Xo dng xo mt mc ra khi danh sch. Trang 240/286 Gio trnh Visual Basic

Nt Bo co s in ra 1 bo co 1 Form khc bao gm cc khon ph v tnh tng chi phi. 3. Hy to mt Project dng qun l gi thu my ca mt phng my c giao din v yu cu nh sau: Khi Form va load ln mn hnh th cc image trong khung my 1 v my 2 c picture l My tnh Cc Label c Caption= rng Khi ngi s dng click vo image ca my 1 hoc my 2 th: Nu l my cha thu: i picture ca image thnh hnh ng h Hin gi hin ti vo label bt u ca my (label pha trn) Nu l my ang thu: Hin gi hin ti vo label Kt thc ca my (label pha di) Da vo Gi v Pht (khng cn quan tm n s giy) trn cc label Bt u v Kt thc tnh ra s tin phi tr, ri dng Msgbox thng bo (tin thu my l 3000/gi) i picture ca image thnh hnh My tnh Xa ni dung trong cc Label Bt u v Kt thc Khi ngi s dng Click vo nt Kt thc th kim tra nu c my ang thu phi nhc nh ngi s dng bng MsgBox, khi no ngi s dng tht s mun kt thc th mi dng chng trnh.

Trang 241/286

Gio trnh Visual Basic

4. Hy to mt project c giao din nh sau:

Khi ngi s dng click vo mc kt thc th xut hin mt Msgbox hi ngi s dng sau mi quyt nh c dng chng trnh hay khng. Trong menu Cp Nht c cc mc:

Xa hc vin: Xa tt c cc hc vin ang chn trong Lp A v Lp B. (cc listbox Lp A v Lp B cho php chn cng lc nhiu mc) Chuyn sang lp A: Chuyn cc mc ang chn trong Lp B sang Lp A Chuyn sang lp B: Chuyn cc mc ang chn trong Lp A sang Lp B

Trang 242/286

Gio trnh Visual Basic

Lu : Khi hai mc trn c chn nu khng c hc vin no c chn trong Listbox th xut hin mt msgbox yu cu ngi s dng phi chn hc vin trc Nhp hc vin mi: Khi mc ny c chn th xut hin Form cho php nhp thng tinh nh sau: ComboBox: cha hai mc Lp A v Lp B Khi ngi s dng Click vo nt cp nht : Nu h v tn khc rng th: Ph thuc vo lp chn trong ComboBox cp nht h tn vo ListBox tng ng trn Form chnh Xa ni dung ca Textbox a con tr v Textbox Nu h v tn bng rng: Xut hin mt Msgbox yu cu phi nhp h tn a con tr v Textbox Khi ngi s dng Click vo nt Tr v th ng Form nhp thng tin li v quay tr v Form chnh.

5. Hy to mt project gip cho rp chiu phim qun l vic bn v ca mnh. Rp c 6 hng gh, mi hng c 3 gh, cc gh c nh s t 1 n 15 v c phn thnh 3 l nh (hnh bn):

Trang 243/286

Gio trnh Visual Basic

Gi v l A 1000/v Gi v l B 1500/v Gi v l C 2000/v

Trn Form trnh by mt s cc ch ngi ngi s dng chn v tr mun mua. Trn s ny cng th hin nhng v tr bn v v nhng v tr cha bn v bng cch th hin mu khc nhau (gh cha bn v mu trng, gh bn v mu vng). Khi ngi s dng click chut ti mt v tr trn s th: Nu y l v tr cha bn v th i mu ca v tr ny sang mu xanh cho bit y l v tr ang chn. Nu y l v tr ang chn (c mu xanh) th i mu ca v tr ny tr v mu trng Nu y l mt v tr bn v th xut hin mt Message box thng bo cho ngi s dng bit. Sau khi chn cc v tr ngi sh sng c th click chut vo nt CHN hoc HY B Nu click vo nt CHN th: Trang 244/286 Gio trnh Visual Basic

i mu cc v tr chn (mu xanh)trn s sang mu vng (cho bit v tr bn v) Xut ln mt Label tng s tin phi tr cho s v mua (ph thuc vo cc v tr chn) Nu click vo nt HY B th: i mu cc v tr chn (mu xanh) trn s sang mu trng tr li Xut ln label gi tr 0

2.

thi trc nghim

Q.1/ 60

Nu Item1 c gi tr l 15 v Item2 =30, th kt qu ca chng trnh Visual Basic sau y xut hin Label1 l g? If Item1 > Item2 Or (Item1 + 10) > Item2 Then Label1.Caption = "XXXX" ElseIf Item2 < Item1 Then Label1.Caption = "YYYY" Else Label1.Caption = "ZZZZ" End If Cc chn la? A) B) Empty Khng th bit c Trang 245/286 Gio trnh Visual Basic

C) D) E) F) Q.2/ 60

Chng trnh li XXXX ZZZZ YYYY

Nu bin Temp =95, th kt qu xut ra Label ca an chng trnh sau l bao nhiu? If (Temp >= 70) Then Label1.Caption = "DurianGroup" ElseIf (Temp >= 80) Then Label1.Caption = "DurianGroup.Wordpress.com" ElseIf (Temp >= 90) Then Label1.Caption = "http://DurianGroup.Wordpress.com" End If Cc chn la? A) B) C) D) E) F) DurianGroup Khng c g xut ra label Chng trnh b li http://DurianGroup.Wordpress.com DurianGroup.Wordpress.com Khng th xc nh c

Trang 246/286

Gio trnh Visual Basic

Q.3/ 60 Trong chng trnh sau, bao nhiu ln Visual Basic thc hin thng ip MSgbox? For i = 2 to 8 Step 2 If i = 6 Then Exit For Msgbix i Next i Cc chn la? A) B) C) D) E) F) Q.4/ 60 Cho hm nh ngha nh sau Sub SquareIt(byval num as integer) num = num * num * num * num End Sub Bn c chng trnh gi hm ni trn Sum = 4 Trang 247/286 Gio trnh Visual Basic 8 2 4 3 6 1

SquareIt Sum Sau khi chy, gi tr ca bin Sum l? Cc chn la? A) B) C) D) E) Q.5/ 60 64 16 4 256 0

Hm g chuyn cc k t trong mt TextBox v dng ch thng (lower case)? Cc chn la? A) B) C) D) E) F) Q.6/ 60 text1=Lcase(Text1) ModCase(text1,"L") Text1=Format(text1,"LowerCase") ToLower(Text1.text) LCase(Text1.text) LowerCase(Text1.text)

Trang 248/286

Gio trnh Visual Basic

Cho bit kt qu ca chng trnh sau y l gi? Sub ProcA() On Error GoTo eh: Dim x as integer x = 1000 ProcB(x) msgbox "x = " + x Exit Sub eh: msgbox "C li" End Sub Sub ProcB(n as integer) dim y as integer, z as integer z = n/y msgbox "z = " + z msgbox "y - " + y End Sub Cc chn la? A) B) C) D) x = 1000 Thng bo: "C li" z = 1000, y = 1, x = 1000 chng trnh b li v khng thc hin c

Q.7/ 60 Trang 249/286 Gio trnh Visual Basic

Form lai g sau y l cc Form khc trong cng chng trnh khng th c kch hat cho n khi n ng li? Cc chn la? A) B) C) D) Q.8/ 60 Modeless Form Startup Form Modal Form MDI Form

Bn c mt Form tn l frmApplication, trn Form ny cha mt textbox txtCreditLimit v mt command button tn l cmdOK. Nu ngi s dng nhp vo mt gi tr ln hn 25000 th cmdOK s b cm (disabled.) Chn s kin thch hp? Cc chn la? A) B) C) D) Q.9/ 60 txtCreditLimit_Click txtCreditLimit_Change txtCreditLimit_KeyPress cmdOK_Click

Trang 250/286

Gio trnh Visual Basic

Chng trnh ca bn nh sau Sub ProcA() Dim strProcNameA As String On Error GoTo ErrorHandler strProcNameA = "ProcA" Call Proc2(strProcNameA) ExitHere: MsgBox "It Worked!" Exit Sub ErrorHandler: MsgBox "Error in " + strProcNameA End Sub Sub ProcB(strName As String) strName = "ProcB" Call ProcC(strName) End Sub Sub ProcC(strName As String) strName = "ProcC" x=1/0 End Sub Chng trnh s hin th ra g trong thng im (message) u tin sau khi bn gi call ProcA? Cc chn la? A) B) It Worked! Error in ProcA

Trang 251/286

Gio trnh Visual Basic

C) D) Q.10/ 60

Error in ProcB Error in ProcC

Chng trnh ca bn cha mt frame tn l fraMyFrame. Trn frame ny cha 2 option buttons, dng mng i tng tn l optShowText. an code code sau y lm cho mt text box control trn Form hin th khi ngi s dng chn option button u tin: Const SHOWTEXTBOX = 0 Const HIDETEXTBOX = 1 If Index = SHOWTEXTBOX Then txtMyText.Visible = True Else txtMyText.Visible = False End If Chn s kin thch hp a an code ni trn vo? Cc chn la? A) B) C) D) Q.11/ 60 Trang 252/286 Gio trnh Visual Basic optShowText(0)_Click optShowText(1)_Click optShowText_Click fraMyFrame_Click

Chng trnh ca bn c mt text box tn l txtMyTextbox v mt list box control tn l LstMyUstbox. Khi ngi s dng click chn mt phn t trong LstMyListbox, bn mun gi tr ang chn trong Listbox hin th vo textbox. Chn chng trnh thch hp? Cc chn la? A) txtMyTextbox.Text = 1stMyListbox.Selected(lstMyListbox.Listlndex) txtMyTextbox.Text = 1stMyListbox. ItemData(lstMyListbox.Listlndex) txtMyTextbox = 1stMyListbox txtMyTextbox.Text = 1stMyListbox(1stMyListbox. Listlndex).List

B) C) D) Q.12/ 60

Bn c textbox tn l txtUserName dng nhp tn ngi s dng. Bn c mt command button Command1. Bn mun command button ny ch hat ng khi tn nhp vo txtUserName bng tn ca bn. Chn s kin (event) thch hp vit chng trnh? Cc chn la? A) B) C) txtUserName _Change txtUserName _LostFocus txtUserName _Click Trang 253/286 Gio trnh Visual Basic

D) Q.13/ 60

txtUserName _Validate

Bn c mt mng gm 3 menu (item 1, Item 2, Item 3) c tn l mnuFileItem vi cu trc sau File Item 1 Item 2 Item 3 Bn mun vit an code bt tt c menu v tr chn? Cc chn la?

A)

For Each menu In mnuFileItem . Checked = True Next mnuFileItem

B)

For i = 0 to 2 nuFileItem(i).Checked = True Next i

C)

With mnuFileItem . Checked = True End With

D)

With mnuFileItem Trang 254/286 Gio trnh Visual Basic

. Checked(0) = True . Checked(1) = True . Checked(2) = True End With Q.14/ 60

an code no sau y s chn tt c cc phn t ca listbix lstForms? Cc chn la?

A)

For i = 0 To lstForms.ListCount - 1 lstForms.List(i).Selected = TRUE Next For i = 0 To lstForms.ListCount - 1 lstForms.Selected(i) = TRUE Next For i = 0 To lstForms.ListCount - 1 lstForms.List(i).SetFocus Next For i = 1 To lstForms.ListCount lstForms.Selected(i) = TRUE Next

B)

C)

D)

Q.15/ 60

Trang 255/286

Gio trnh Visual Basic

Bn mun thay i mu ch ca mt TextBox. Chn thuc tnh thch hp? Cc chn la? A) B) C) D) E) F) Q.16/ 60 ColorText ForeColor TextColor FontColor FillColor Color

ng dng ca bn c mt menu mnuCsutomer, vi 2 menu con l Find v Edit. Menu Edit hin nay ang b cm. (disabled). Bn mun ngi s dng tm mt khch hng (customer) trc khi c th chn menu Edit. Bn mun vit code cho php cp nht khch hng. Chn an code thc hn?

Cc chn la? Private Sub MnuCustomer_Click() MnuFind.Visible=True EndSub Private Sub MnuEdit_Click() MnuEdit.Visible=True Trang 256/286 Gio trnh Visual Basic

A)

B)

EndSub Private Sub MnuFind_Click() MnuEdit.Visible=True EndSub Private Sub MnuFind_Click() MnuEdit.Enabled=True EndSub

C)

D)

Q.17/ 60

Xem xt chng trnh di y Private Sub cmdProcess_Click() Call MyProcA EndSub Sub MyProcA() On Error goto ErrorHanler Text1="MyProcA" Call MyProcB Text3="MyProcC" Exit Sub ErrorHanler: Msgbox " c li." -------------CODE Cn thit y ------------End Sub Sub MyProcB() Trang 257/286 Gio trnh Visual Basic

Text2="MyProcB" Call MyProcC End Sub Sub MyProcC() Dim vDiv Vnum=3/vDiv Text2=vNum End Sub Chn an code tng ng bn di chn vo chng trnh cho php chng trnh vn tip tc thc hin khi c li xy ra? Cc chn la? A) B) C) D) E) F) Q.18/ 60 Resume Exit Sub Resume Next Exit Exit For Exit Function

Bn c 2 menu trong chng trnh l mnuView v mnuReports. Menu MnuView dng iu khin vic hat ng ca mnuReports. Bn mun khi ngi s dng chn mnuView, nu mnuView cha c chn, n s cho php menu mnuReports hat ng , ng thi chnh n s c du check nh du l c chn. Khi ngi s dng chn mnuView, nu mnuView c chn, n Trang 258/286 Gio trnh Visual Basic

s khng cho php menu mnuReports hat ng , ng thi chnh n s tt du check cho bit l khng c chn. Chn lnh thc hin iu ny? Cc chn la?

A)

Private Sub mnuView_Click() MnuToolbar.Enable=True MnuReports.Checked=True End sub

B)

Private Sub mnuReport_Click() MnuReports.Checked=Not MnuReports.Checked tblReports.Visible=Not tblReports.Visible End sub

C)

Private Sub mnuView_Click() MnuReports.Checked=True MnuView.Visible=True End sub

D)

Private Sub mnuReport_Click() MnuReports.Checked=True tblReports.Visible=True End sub

Q.19/ 60 Trang 259/286 Gio trnh Visual Basic

Chn an lnh gy ra li c php (syntax)? Cc chn la? Dim vPercent as Integer Dim vNum as Integer vPercent =vNum * -100 if vlwWatch="DurianGroup" then vType="123" else vType="789" Private vrtype as Integer Private vrPick as string Vrtype=1vrPick ="4" if vrtype = vrPick then Text1.Text="DurianGroup" Else Text1.Text="DurianGroup.WOordpress.com" End if Dim vHold as Boolean vHold ="DurianGroup"

A)

B)

C)

D) Q.20/ 60

Gi tr ca thuc tnh Name ___________ Cc chn la?

Trang 260/286

Gio trnh Visual Basic

A) B) C) D) Q.21/ 60

Khng th sa lc Runtime Khng tn ti lc Runtime Khng th sa lc Designtime Khng th c lc runtime

ngn ngi s dng a focus vo mt button vi bt c cch no, bn nn ___________ Cc chn la? A) B) C) D) Q.22/ 60 Thit lp thuc tnh TabStop =0. Thit lp thuc tnh TabIndex =0. Thit lp thuc tnh Focus = False Thit lp thuc tnh Enabled =False

Lnh no sau y s gn gi tr Caption ca CommandButton vo ni dung ca textbox Text1 Cc chn la? A) B) C) D) Text1 = Command1.Caption Text1.Text = CStr(Command1) Text1 = Command1.Text Text1.Text = Command1.Text Trang 261/286 Gio trnh Visual Basic

Q.23/ 60

Nu thuc tnh Enabled ca TextBox l False, th Cc chn la? text trong textbox s m, ngi s dng c th a con tr vo c nhng khng th thay i ni dung textbox text trong textbox s m, ngi s dng khng th a focus vo textbox c, v khng sa ni dung c text trong textbox vn sng, ngi s dng c th a con tr vo c nhng khng th thay i ni dung textbox text trong textbox vn sng, ngi s dng khng th a con tr vo c nhng khng th thay i ni dung textbox

A)

B)

C)

D)

Q.24/ 60

hy b mt Form ra khi b nh, chng ta dng lnh _____________ Cc chn la?

A) B) C)

End Show Exit Trang 262/286 Gio trnh Visual Basic

D) Q.25/ 60

Unload

Bn mun vit mt hm tn l Calculate. Hm ny c 2 tham tr kiu Integer v tr v gi tr kiu s LONG. Chn khai bo thch hp nht? Cc chn la? A) Public Sub Calculate (intX as Integer, intY as Integer) as Long Sub Calculate (intX as Integer, intY as Integer) as Long Function Calculate (ByVal intX as Integer, ByRef intY as Integer) as Long Function Calculate (ByVal intX as Integer, ByVal intY as Integer) as Long Private Sub Calculate (intX as Integer, intY as Integer) as Long Function Calculate (intX as Integer, intY as Integer) as Long

B)

C)

D)

E)

F) Q.26/ 60

MDI Form l Form g? Cc chn la? A) Mt Form hin th dng Model Trang 263/286 Gio trnh Visual Basic

B) C) D) Q.27/ 60

Mt Form nh to hn v kch thc so vi cc Form khc Mt Form cha cc Form khc Mt Form lin kt n Form khc

Nu t kha Option Explicit c khai bo trong mt module, th tc dng ca n l Cc chn la? A) B) C) D) Q.28/ 60 Mi bin s l Variant Mi bin s c gi tr NULL Mi bin s c gi tr 0 yu cu tt c cc bin phi c khai bo tng minh

Chng trnh s hin th gi tr g khi nt lnh btnDiaplay c click? Private Sub btnDisplay_Click() Dim num As Double = 9 txtBoxResult.Text = CalculateSquareRoot(num) End Sub Function CalculateSquareRoot(ByVal NumberArg As Double) As Trang 264/286 Gio trnh Visual Basic

Double If NumberArg < 0 Then MsgBox "Error - cannot find square root - result set to zero" CalculateSquareRoot=0 Else CalculateSquareRoot=Sqrt(NumberArg) End If End Function Cc chn la? A) B) C) D) E) Q.29/ 60 0 12 9 6 3

Chng trnh s hin th gi tr g khi nt lnh btnDiaplay c click? Private Sub btnDisplay_Click() Dim word1, word2, word3 As String word1 = "First" word2 = "Second" word3 = "Third" Myproc(word1, word2, word3) End Sub

Trang 265/286

Gio trnh Visual Basic

Sub Myproc(ByVal var3 As String, ByVal var2 As String, ByVal var1 As String) txtBox.Text = var1 & var2 & var3 End Sub Cc chn la? A) B) C) D) E) Q.30/ 60 SecondThirdFirst ThirdSecondFirst Program will be error FirstSecondThird No output

Chng trnh s hin th gi tr g khi nt lnh btnDiaplay c click? Private Sub btnDisplay_Click() Dim x as String, y As String x = "tin" y = "can" Swap(x, y) txtOutput.Text = x & " " & y End Sub Sub Swap(ByRef x As String, ByVal y As String) Dim temp As String temp = x x=y Trang 266/286 Gio trnh Visual Basic

y = temp End Sub Cc chn la? A) B) C) D) E) Q.31/ 60 can tin can can Li khi thc hin chng trnh tin can tin tin

Bn mun tnh gi tr ca biu thc: S= 1/2 + 2/3 + 3/4 + 4/5 + + 99/100? Chn an lnh thch hp? Cc chn la? For x = 1 To 100 s = s+(1 / (x + 1)) Next For n = 1 To 99 s = s+(n / (1 + n)) Next For d = 2 To 99 s = 1 / d + d / (d + 1) Next Trang 267/286 Gio trnh Visual Basic

A)

B)

C)

D)

For q = 100 To 1 s =s+ ((q + 1) /q) Next

Q.32/ 60

Cho mng khai bo nh sau: Dim myArray(100) As Double Bn mun thit lp tt c gi tr ca mng thnh 100. Chn chng trnh thch hp? Cc chn la? For j = 0 to 100 myArray(j) = 100 Next All options For j = 1 to 100 myArray(j) = 100 Next myArray() is already initialized to 100 by the Dim statement. For i = 0 To 100 (i) = 100 Next myArray = 100 Trang 268/286 Gio trnh Visual Basic

A)

B)

C)

D)

E)

F)

Q.33/ 60

Nu bn mun to thm mt command button cho Form, bn s ly command button ny t thnh phn no trong mi trng thit k ca VB (Visual Basic environment)? Cc chn la? A) B) C) D) Q.34/ 60 The Form window The Properties window The Toolbox The Project window

kt thc chng trnh, ngi ta dng lnh Cc chn la? A) B) C) D) E) Q.35/ 60 Exit Halt End Exit (0) End Sub

Trang 269/286

Gio trnh Visual Basic

Chng trnh ca bn cho php ngi khc chn mu ca Form. Bn c 5 mu chn. Bn nn s dng i tng no l thch hp nht? Cc chn la? A) B) C) D) Q.36/ 60 option buttons text boxes check boxes name boxes

a hnh nh vo mt image control, ngoi ta dng thuc tnh _______ Cc chn la? A) B) C) D) Q.37/ 60 icon graphic image picture

xc nh xem mt option button c c chn hay khng, ngui ta dng thuc tnh __________ Cc chn la? A) Visible Trang 270/286 Gio trnh Visual Basic

B) C) D) E) F) Q.38/ 60

Enabled Value Caption GotFocus Selected

Ba k t u c ngh (prefix) ca label l __________. Cc chn la? A) B) C) D) E) F) Q.39/ 60 lab lb l la lbl lbe

Nu mt bin c khai bo m khng ch nh kiu d liu, th bin s c kiu l ________ Cc chn la? A) boolean

Trang 271/286

Gio trnh Visual Basic

B) C) D) E) Q.40/ 60

variable string object variant

Nu mt hm (Sub/Function) cn c gi trong nhiu Form, th hm ny nn c t trong __________ Cc chn la? A) B) C) D) Q.41/ 60 Mt hm khc Form module Bin cc b

i vi mt Listbox trong Visual Basic 6, thm 1 phn t vo, ngi ta s dng phng thc _________ Cc chn la? A) B) C) D) LisstIndex AddValue AddItem AddList Trang 272/286 Gio trnh Visual Basic

E) F) Q.42/ 60

ItemAdd List

Cho mng 2 chiu A c khai bo nh sau: Dim A(0 To 3, 0 To 5) As Integer Mng A cha bao nhiu phn t? Cc chn la? A) B) C) D) E) F) Q.43/ 60 24 3 8 10 5 15

Khi button btnDisplay c click, th kt qu ca chng trnh s hin th? Private Sub btnDisplay_Click() Dim a As String, b As String txtBox.text= a = "A" b = "B" Trang 273/286 Gio trnh Visual Basic

PrintWords(a, b) PrintWords(b, a) End Sub Sub PrintWords(ByVal a As String, ByVal b As String) txtBox.Text = a & b End Sub Cc chn la? A) B) C) D) E) Q.44/ 60 ABBA ab ba AB BA ABAB abba

Khi button btnDisplay c click, th kt qu ca chng trnh s hin th? Private Sub btnDisplay_Click() Dim var1, var2, var3, num As Integer var1 = 2 var2 = 4 var3 = 6 Add(num) txtBox.Text = CStr(num) End Sub Sub Add(ByRef num As Integer) Trang 274/286 Gio trnh Visual Basic

Dim var1, var2, var3 As Integer num = var1 + var2 + var3 End Sub Cc chn la? A) B) C) D) Q.45/ 60 6 12 0 8

Khi button btnDisplay c click, th kt qu ca chng trnh s hin th? Private Sub btnDisplay_Click() Dim num As Integer num= 20 DoubleIt(num) txtBox.Text = CStr(num) End Sub Sub DoubleIt(ByRef var As Integer) var = var * 2 End Sub Cc chn la? A) B) 20 0 Trang 275/286 Gio trnh Visual Basic

C) D) Q.46/ 60

40 80

Chn pht biu sai trong cc pht biu sau y khi thc hin vi Visual Basic? Cc chn la? A) B) C) D) E) F) Q.47/ 60 x = 10 If run then lblOut.Text = "True" z=x+y lblOut.Text = str(z) x+y=y y = 20

Chn an lnh no sau y thay i gi tr hin th trn Button Button1 thn "Push Me" ? Cc chn la? A) B) C) D) Button1.Name = "Push Me" Button1.Value = "Push Me" Button1.SetText( "Push Me") Button1.Caption = "Push Me" Trang 276/286 Gio trnh Visual Basic

E) F) Q.48/ 60

Button1.Print "Push Me" Button1.Text = "Push Me"

Trong VB6, Vi Khai bo DIM a, b as integer th a s c kiu bin g? Cc chn la? A) B) C) D) E) Q.49/ 60 boolean string long integer Kiu d liu khc

Bn mun xa tt c cc ohn t trong listbox LstA. Chn an code tng ng? Cc chn la? A) B) C) D) LstA.Clear LstA.Remove LstA.removeItem LstA.ListCount-1 LstA.Unload

Trang 277/286

Gio trnh Visual Basic

E) F) Q.50/ 60

LstA.RemoveAll LstA.removeItem 0

Cho chng trnh sau y, vi Index l bin kiu Integer v thisarray l mt mng (0 to 5) ca cc s Integer For index = 0 To 5 thisarray(index) = index Next For index = 0 To 5 Msgbox thisarray(index): 'Print Next Kt qu hin th khi thc thi chng trnh ln lut l? Cc chn la? A) B) C) D) Q.51/ 60 123456 555555 01 2 3 4 5 000000

Consider the following statements: Dim i As Integer, j As Integer, k As Integer i=3 Trang 278/286 Gio trnh Visual Basic

j=5 k=2 If i < j Then If i < k Then i = 4 Else i = 5 Pht biu no sau y l ng? Cc chn la? A) B) C) D) E) Q.52/ 60 Sau khi thc hin th i = 4 Sau khi thc hin th i c gi tr khc 3 hoc 4 hoc 5 Chng trnh b li Sau khi thc hin th i = 5 Sau khi thc hin th i = 3

Cho chng trnh sau y Sub Form_Load () Dim MyString As String * 5 Dim Index As Integer Dim s1 As String, s2 As String, s3 As String MyString = "ABCDE" For Index = 5 To 2 Step -1 s1 = Left(MyString, Index - 2) s2 = Mid(MyString, Index, 1) s3 = Right(MyString, 5 - Index + 1) MyString = s1 + s2 + s3 Next Trang 279/286 Gio trnh Visual Basic

Msgbox MyString End Sub Kt qu hin th khi thc hin chng trnh l? Cc chn la? A) B) C) D) Q.53/ 60 EEEEE ABBBB Chng trnh b li AAAAA

Cho chng trnh sau y Sub Form_Load () Dim i, a As Integer, b As Integer i=0 a=4 b = 10 swap a, b Msgbox str(a) & " " & str( b) & " " Str( i) End Sub Sub swap (a As Integer, b As Integer) Dim Temp, i Temp = a a=b Trang 280/286 Gio trnh Visual Basic

b = Temp i=1 End Sub Cho bit kt qu sau khi chng trnh thc hin? Cc chn la? A) B) C) D) Q.54/ 60 10 4 0 4 10 0 10 4 1 4 10 1

Control no phi to nhm khi hat ng? Cc chn la? A) B) C) D) Q.55/ 60 OptionButtons TextBoxes CheckBoxes Listbox

vit ch thch (comment) trong Visual Basic Cc chn la? A) dng du // Trang 281/286 Gio trnh Visual Basic

B) C) D) Q.56/ 60

/* comment */ du ' REM

Hm g dng chuyn s qua chui? Cc chn la? A) B) C) D) Q.57/ 60 ToString Str Convert ParseString

Bao nhiu ln MsgBox s hin th khi thc thi chng trnh sau? For Index = 0 To 2 MsgBox Index Next Cc chn la? A) B) C) 2 0 1

Trang 282/286

Gio trnh Visual Basic

D) Q.58/ 60

Gi tr hin th cui cng khi thc hin chng trnh sau l bao nhiu?

Twos = 2 Do While Twos < 100 MsgBox Twos Twos = Twos * 2 Loop Cc chn la? A) B) C) D) Q.59/ 60 2 100 64 128

Chng trnh no sau y s tnh tng cc gi tr t 1 n 100? Cc chn la? Sum = 0 For I = 1 To 100 Sum = Sum + I Trang 283/286 Gio trnh Visual Basic

A)

Next I Sum = 0 For I = 100 To 1 Step -1 Sum = Sum + I Next I Sum = 0 If I <= 100 Then Sum = Sum + I End If Sum = 0 do while I <= 100 Then Sum = Sum + I loop Sum = 0 do Sum = Sum + I loop while I <= 100

B)

C)

D)

E)

Q.60/ 60

Chng trnh no sau y s tnh tng cc gi tr cc s chn t 1 n 100? Cc chn la? Sum = 0 For I = 1 To 100 if i mod 2 =0 then Sum = Sum + I Trang 284/286 Gio trnh Visual Basic

A)

Next I Sum = 0 For I = 2 To 100 step 2 Sum = Sum + I Next I Sum = 0 For I = 1 To 100 Sum = Sum + 2 Next I Sum = 0 For I = 1 To 100 step 2 Sum = Sum + I Next I

B)

C)

D)

Trang 285/286

Gio trnh Visual Basic

TI LIU THAM KHO


1. Programming in Visual Basic 6.0, Julia Case Bradley and Anita C. Millspaugh, McGraw-Hill 2. Programming Microsoft Visual Basic 6.0, Francesco Balena, Microsoft Press 3. Microsoft Visual Basic 6.0 Programer's guide, Microsoft Press 4. Microsoft Visual Basic 6.0 Introduction to Programming, Michael Sprague, Phillips 5. Programming With Visual Basic 6, Boek Edward Coburn & Edward J. Coburn 6. Lp trnh Windows 1, Phm Quang Tri

Trang 286/286

Gio trnh Visual Basic