Thuc hanh vb

  • Published on
    30-Jul-2015

  • View
    16

  • Download
    0

Embed Size (px)

Transcript

<p> 1. Thc hnh vi Visual Basic Bi: Khoa CNTT HSP KT Hng Yn 2. Thc hnh vi Visual Basic Bi: Khoa CNTT HSP KT Hng Yn Phin bn trc tuyn: &lt; http://voer.edu.vn/content/col10383/1.1/ &gt; Hoc lieu Mo Vietnam - Vietnam Open Educational Resources 3. Ti liu ny v s bin tp ni dung c bn quyn thuc v Khoa CNTT HSP KT Hng Yn. Ti liu ny tun th giy php Creative Commons Attribution 3.0 (http://creativecommons.org/licenses/by/3.0/). Ti liu c hiu nh bi: August 22, 2010 Ngy to PDF: August 24, 2010 bit thng tin v ng gp cho cc module c trong ti liu ny, xem tr. 122. 4. Ni dung 1 Bi thc hnh s 1: Ci t Visual Basic v mi trng lm vic ca VB 1.1 Ci t phn mm Visual Basic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.2 Chy chng trnh Visual Basic 6.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . 8 1.3 Thit lp mi trng lm vic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 1.4 Lu d n (Project) ra a . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 1.5 Chy v ng chng trnh Visual Basic (VB) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 1.6 Vit lnh (Code) cho Form hin th li cho Hello World . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 1.7 S dng cu lnh Debug.Print . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . 16 1.8 S dng Cu lnh InputBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 2 Bi thc hnh s 2: Bin, mng, hm, th tc v cc cu trc iu khin 2.1 Khai bo, gn v hin th gi tr ca cc loi bin c bn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 2.2 Khai bo v s dng bin mng . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 2.3 nh ngha v s dng kiu d liu mi - Kiu bn ghi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 2.4 nh ngha Hm (function) trong Visual Basic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . 23 2.5 nh ngha th tc trong Visual Basic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . 23 2.6 Truyn tham tr cho chng trnh con . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 2.7 Truyn tham chiu cho chng trnh con . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 2.8 Cu trc r nhnh If...Then v If ... ElseIf...Then . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 2.9 Cu trc a r nhnh Select Case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 2.10 Cu trc lp For . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 2.11 Cu trc lp Do ... Loop While | Do ... Loop Until . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 3 Bi thc hnh s 3: S dng cc iu khin c bn trong Visual Basic 3.1 S dng TextBox, Label kt hp vi Command Button . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 3.2 S dng iu khin Option . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 3.3 S dng iu khin CheckBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 3.4 S dng iu khin ListBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 3.5 S dng iu khin PictureBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 3.6 S dng iu khin Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 3.7 S dng HscrollBar (Thanh cun ngang) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 3.8 S dng iu khin Timer, Drive, Dir v File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 4 Bi thc hnh s 4: S dng cc hp thoi 4.1 S dng cc hp thoi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . 55 5 Bi thc hnh s 5: S dng Menu v cc thanh cng c 5.1 To menu c nhiu cp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 5.2 To mt Menu ngang c nhiu mc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . 67 5.3 To mt Menu ngang (Menu bar) n gin. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . 70 5.4 Vit lnh cho cc mc ca menu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . 72 5.5 To thanh cng c Toolbar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 5.6 Vit lnh cho cc nt trn thanh cng c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 5.7 Xy dng chng trnh son tho vn bn n gin . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . 83 5.8 Xy dng chng trnh nghe nhc n gin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . 87 6 Bi thc hnh s 6: To, thao tc vi c s d liu v s dng cc i tng 6.1 To mt bng CSDL trong Microsoft Access 2000 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . 93 6.2 Kt ni n CSDL s dng i tng ADO Data Control . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . 101 5. iv 6.3 Hin th bng CSDL trong Data Grid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 6.4 Thm mt bn ghi vo bng CSDL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . 108 6.5 Sa i ni dung ca bn ghi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . 110 6.6 Tm kim mt bn ghi trong bng . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . 112 6.7 Loi b (Xo) mt bn ghi khi bng CSDL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . 114 6.8 S dng cc phng thc ca i tng RecordSet duyt cc bn ghi . . . . . . .. . . . . . . . . . . . 117 Attributions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .122 6. Chng 1 Bi thc hnh s 1: Ci t Visual Basic v mi trng lm vic ca VB 1.1 Ci t phn mm Visual Basic1 ci t Visual Basic 6.0 chng ta cn c b Visual Studio 6.0 (hoc a ci VB ring) lu trong a cng hoc a CD-ROM. Vi b Visual Studio ci t trn a cng, Cc bc thc hin nh sau : B1: Tm v chy file Setup.exe Figure 1.1: Chy file Setup 1This content is available online at . 1 7. 2 CHNG 1. BI THC HNH S 1: CI T VISUAL BASIC V MI TRNG LM VIC CA VB B2: Chn Next Figure 1.2: Chn Next B3: Chn I Accept the Agreement, sau chn (click) Next 12 8. 3 Figure 1.3: Cc iu khon v bn quyn B4: Chn Next 9. 4 CHNG 1. BI THC HNH S 1: CI T VISUAL BASIC V MI TRNG LM VIC CA VB Figure 1.4: Nhp thng tin ng k B5: Chn Custom v chn Next (Hoc c th chn Products ci t cc sn phm ring bit y l cch n gin nht) 12 10. 5 Figure 1.5: La chn sn phm cn ci t B6: Nu mun ci Visual Studio vo th mc khc, click chn Browse. Tip theo chn Next 11. 6 CHNG 1. BI THC HNH S 1: CI T VISUAL BASIC V MI TRNG LM VIC CA VB Figure 1.6: Chn ng dn ci t B7: Chn Continue, Bc tip theo chn OK 12. 7 Figure 1.7: Tin hnh ci t B8: khi tn dung lng a cng, nn b cc thnh phn khng cn thit (b du kim tra i vi mc khng mun ci t) nh hnh 8 di y: Sau chn Continue v ch cho qu trnh ci t kt thc (Finish). 13. 8 CHNG 1. BI THC HNH S 1: CI T VISUAL BASIC V MI TRNG LM VIC CA VB Figure 1.8: Chn cc thnh phn cn ci t 1.2 Chy chng trnh Visual Basic 6.02 Click chn Start Programs MS Visual Studio 6.0 MS Visual Basic 6.0. Sau chn kiu d n l Standard EXE khi c hp thoi hin ra: 21 2This content is available online at . 14. 9 Figure 1.9: Chn kiu d n cn pht trin Sau khi nhn nt Open, th VB s to sn cho chng ta mt Project, c giao din nh hnh di y : 15. 10 CHNG 1. BI THC HNH S 1: CI T VISUAL BASIC V MI TRNG LM VIC CA VB Figure 1.10: Ca s giao din chnh ca Visual Basic 1.3 Thit lp mi trng lm vic3 T ca s chnh, click chn thc n (Menu) Tools, v chn mc options 3This content is available online at . 16. 11 Figure 1.11: t cc tu chn cho mi trng lm vic Hu la chn t ng kim tra c php v t rng phm TAB = 8 (hoc 6 v.v...) 1 2 3 Figure 1.12: t ch kim tra c php v yu cu khai bo bin 17. 12 CHNG 1. BI THC HNH S 1: CI T VISUAL BASIC V MI TRNG LM VIC CA VB Chn Font ch hin th cho vn bn chng trnh ngun. Hy chn font ch l font vntime hoc VK Sans serif. Figure 1.13: Chn font v mu ch cho vn bn chng trnh ngun t rng ca li trn Form : 18. 13 Figure 1.14: t rng cho li Ngoi ra cn nhiu thit lp khc na, yu cu sinh vin t thc hnh ! 1.4 Lu d n (Project) ra a4 lu d n ra a, chn menu File Save Project. Hoc nhn biu tng a mm trn thanh cng c. Lu khi lu Project: Mt project c th cha cc Form, report, module, image, clAss v.v. . . Th mi i tng ny nn lu vo mt th mc ring tng ng nh th mc Forms, reports, modules, images, clAss v.v. . . Cn ring file *.vbp (Visual Basic Project) th c lu th mc cha. Di y l mt hnh nh ca vic lu tr cc thnh phn ca mt Project: 4This content is available online at . 19. 14 CHNG 1. BI THC HNH S 1: CI T VISUAL BASIC V MI TRNG LM VIC CA VB Figure 1.15: Vic t chc lu tr cc thnh phn ca mt d n 1.5 Chy v ng chng trnh Visual Basic (VB)5 chy chng trnh, chng ta c th vo menu Run Start (F5) hoc RunStart with Full compile (Ctrl + F5) Run Start: Chy chng trnh nhng khng dch ton b chng trnh (tc l chng trnh chy n u th my dch ti ) Run Start with full compile : Dch ton b chng trnh trc khi chy, nh vy nu c xut hin li bt c u trong chng trnh th my s dng li v thng bo li. 1.6 Vit lnh (Code) cho Form hin th li cho Hello World6 Hin th li chy trong ca s trung gian (Intermediate Window) Bc 1: To mt Project : Vo menu Project New Project, sau chn loi Project l Standard EXE nh Hnh 9. Bc 2 : M ca s son tho lnh: Vo menu View Code Bc 3: Vit lnh nh sau: 5This content is available online at . 6This content is available online at . 20. 15 Figure 1.16: Vit lnh trong ca s Code Bc 4: Chy chng trnh : Nhn phm F5 hoc t hp phm Ctrl-F5 v quan st kt qu, ta s thy xut hin dng ch Hello world trong mt ca s c tn l Immediate. C th hin ca s ny bng t hp phm Ctrl-G. Nh vy, lnh Debug.print c chc nng hin th kt qu ra mn hnh, n tng t nh lnh Writeln trong PAscal, printf trong C hay ? trong Foxpro. . . Hin th li cho trong hp thoi - MsgBox. Cc bc thc hin ging nh phn a) nhng vit lnh sau thay v lnh print: 21. 16 CHNG 1. BI THC HNH S 1: CI T VISUAL BASIC V MI TRNG LM VIC CA VB Figure 1.17: Hin th li cho bng lnh MsgBox Nhn F5 chy chng trnh, ta c kt qu: Figure 1.18: Kt qu chy chng trnh Nh vy, lnh MsgBox cng c chc nng hin th kt qu ra mn hnh ging nh Debug.Print nhng trn mt ca s (hp thoi) ring. 1.7 S dng cu lnh Debug.Print7 Lnh Print dng in mt biu thc ra ca s tm thi (Immediate). Chng trnh sau y s hin th mt xu, mt s, mt bin s, mt xu vi mt bin, mt biu thc bt k bng lnh Debug.Print. 7This content is available online at . 22. 17 Figure 1.19: Cc cch s dng lnh Print Nhn F5 chy chng trnh. Ca s Immediate cho ta kt qu nh sau: Figure 1.20: Kt qu chy chng trnh Lu : Du &amp; dng ghp cc gi tr cn in. 23. 18 CHNG 1. BI THC HNH S 1: CI T VISUAL BASIC V MI TRNG LM VIC CA VB 1.8 S dng Cu lnh InputBox8 Lnh (hm) InputBox c chc nng nhp d liu t ngi dng, tng t nh Readln trong PAscal, scanf trong C, Accept trong Foxpro ... Hm InputBox c th nhn mt trong 3 tham s quan trng l: Dng nhc Prompt, Tiu ca hp thoi Title v gi tr mc nh Default. Hm ny tr v gi tr m ngi dng va nhp. Thc hnh 1 : nhp h tn ca ngi dng bng hm InputBox G on lnh sau vo trong th tc Form_Load : SubForm_Load Dim HoTen As String HoTen = InputBox(Nhp h tn) Msgbox Bn va nhp xu l : &amp;HoTen End Sub Thc hnh 2: Nhp H tn v tui, sau thng bo ra mn hnh bng MsgBox SubForm_Load Dim HoTen As String Dim Tuoi As Integer HoTen = InputBox(Nhp h tn,Tiu : Nhp thng tin) Tuoi = InputBox(Tui ca bn : ,Nhp thng tin, 20) Msgbox Bn va nhp xu l : &amp;HoTen MsgBox Tui l : &amp;Tuoi End Sub Mt s nhn xt: Th tc SubForm_Load tng t nh hm main trong C hay Begin . . . End. trong PAscal s c gi u tin khi chng trnh chy. Hm MsgBox, Debug.Print v InputBox c s dng nh nhng lnh nhp xut d liu n gin nht trong Visual Basic. 8This content is available online at . 24. Chng 2 Bi thc hnh s 2: Bin, mng, hm, th tc v cc cu trc iu khin 2.1 Khai bo, gn v hin th gi tr ca cc loi bin c bn1 2.1.1 Khai bo bin Thc hnh: Khai bo cc bin tng ng vi cc kiu d liu c bn trong VB, sau gn gi tr v hin th gi tr ca cc bin ra mn hnh bng hm MsgBox. Hng dn: Cc kiu d liu c bn trong VB bao gm Byte, Integer, Long, Single, Double, String, Variant. . . Vit lnh: To mt Project mi v g on lnh sau trong Form Option Explicit Dim ToanCucTrongFormAs Integer Public ToanCucTrongUngDung As Integer Private SubForm_Load() Dim b As Byte Dim Bool As Boolean Dim i AsInteger Dim L As Long Dim F As Single Dim D As Double Dim S As String Dim S1 As String * 30 Dim V As Variant App.Title = "Khai bo bin trong Visual Basic" MsgBox "Bin b, chim 1 byte, phm vi biu din 0-255" MsgBox "Bin Bool, 2 byte, biu din gi tr True v False" MsgBox "Bin i, 2 byte, phm vi: -32768 ... +32767" MsgBox "Bin L, chim 4 byte, phm vi: -2,147,483,648 n 2,147,483,647 " MsgBox "Bin F, chim 4 byte, biu din s thc m v dng" MsgBox "Bin D, chim 8 byte, biu din s thc m v dng rt ln" MsgBox "Bin S, chim 10+ di ca xu. c th lu ti 2 t k t" MsgBox "Bin S1 l bin xu c di c nh (trng hp ny l 30)." &amp; _ "c th lu ti a khong 65400 k t" 1This content is available online at . 19 25. 20 CHNG 2. BI THC HNH S 2: BIN, MNG, HM, TH TC V CC CU TRC IU KHIN MsgBox "Bin V l bin Variant, chim 16 byte. N c th lu bt k loi gi tr no" End sub Gii thch thm: Bin ToanCucTrongForm (Ton cc trong Form) c khai bo vi t kho Dim l bin c th s dng bt k u trong chnh Form n c khai bo. Bin ToanCuc...</p>