Tải bản đầy đủ (.pdf) (26 trang)

thực hành visual basic 6 0

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (793.88 KB, 26 trang )

Bài tập thực hành môn: ngôn ngữ visual basic Bộ môn : công nghệ phần mềm
This exercise was written by Vu Bao Tao ( the member of caulacbovb club) 1 / 26
Bài 1: Điều khiển Animation ( Hình động)
Điều khiển Animation cho phép hiển thị các đoạn phim .AVI
Để sử dụng trớc hết ta thêm điều khiển Animation vào Toolbox thông qua hộp
thoại Components \ Microsoft Windows Common Control 2.60
Bổ sung điều khiển Anim ation vào form, tiếp đó bổ sung lên form hai nút bấm lên
form.
Điều khiển
Thuộc tính
Điều khiển
Thuộc tính
Animation
Name: Animation1
Name: frmMain
Name: CmdPlay
Form
Caption: Animation
Command
BorderStyle: Fixed Dialog
Name: CmdStop
Command
Trong sự kiện cmdPlay _Click() và cmdStop_Click() nhập code nh sau:
Dim str As String
Private Sub cmdPlay_Click()
str = "C:\Program Files\Microsoft Visual Studio \Common\Graphics\Videos\filecopy.avi"
Animation1.Open str
Animation1.Play
End Sub
Private Sub cmdStop_Click()
Animation1.Stop


End Sub
Tiến hành chạy chơng trình bằng cách nhấn phím F5
Bài 2: Điều khiển Progress bar (Thanh tiến trình)
Để sử dụng trớc hết ta thêm điều khiển Progress Bar vào Toolbox thông qua hộp
thoại Components \ Microsoft Windows Common Controls 6.0
Bổ sung điều khiển Progress Bar vào form, tiếp đó bổ sung lên form hai điều khiển
là Label và Timer
Điều khiển
Thuộc tính
ProgressBar
Name: ProgressBar1
Max: 100
Min: 0
Bài tập thực hành môn: ngôn ngữ visual basic Bộ môn : công nghệ phần mềm
This exercise was written by Vu Bao Tao ( the member of caulacbovb club) 2 / 26
Timer
Name: Timer1
Interval: 300
Label
Name: Label1
Caption: Chơng trình đang khởi động
Trong sự kiện Form_Load() và Timer1_Timer() nhập code nh sau:
Private Sub Form_Load()
ProgressBar1.Value = ProgressBar1.Min
End Sub
Private Sub Timer1_Timer()
ProgressBar1.Value = ProgressBar1. Value + 5
If ProgressBar1.Value >= ProgressBar1.Max Then
Timer1.Enabled = False
Unload Form1

End If
End Sub
Tiến hành chạy chơng trình
Bài 3: Điều khiển Progress bar (Thanh tiến trình) dùng PictureBox
Để sử dụng trớc hết ta thêm điều khiển Progress Bar vào Toolbox thông qua hộp
thoại Components \ Microsoft Windows Common Controls 6.0
Bổ sung điều khiển Progress Bar vào form, tiếp đó bổ sung lên form hai điều khiển
là Label và Timer
Điều khiển
Thuộc tính
Điều khiển
Thuộc tính
Picture1
Name: Picture1
Backcolor: chọn màu trắng
Label
Name: Label1
Caption: Program is loading
Picture2
Name: Picture2
Backcolor: chọn màu đỏ
Form
Name: form1
Caption: Progressbar
Timer
Name: Timer1
Interval: 30
Bài tập thực hành môn: ngôn ngữ visual basic Bộ môn : công nghệ phần mềm
This exercise was written by Vu Bao Tao ( the member of caulacbovb club) 3 / 26
Trong sự kiện Timer1_Timer() nhập code nh sau:

Private Sub Timer1_Timer()
Picture2.Width = Picture2.Width + 40
If Picture2.Width = Picture1.Width Then
Picture1.Visible = False
Timer1.Interval = 0
Unload frmlogin
End If
End Sub
Tiến hành chạy chơng trình
Bài 4: Sử dụng điề u khiển textbox và nút bấm Command để tạo hộp thoại Login
Trớc hết tạo một form.
Sau đó bổ sung thêm 2 điều khiển textbox, 2 nhãn label và 2 nút bấm Command
lên form.
Điều khiển
Thuộc tính
Điều khiển
Thuộc tính
Form
Name: frmlogin
Caption: Login
textbox
Name: txtName
text:
Label
Name: Label1
Caption: Ngời sử dụng
textbox
Name: txtName
text:
Label

Name: Label2
Caption: Mật khẩu
Command
Name: cmdOk
Caption: &Chấp nhận
Command
Name: cmdexit
Caption: &Hủy bỏ
Ta mở trang soạn thảo lệnh chơng trình và viết mã lệnh cho các sự kiện
Private Sub cmdexit_Click()
Unload frmlogin
End Sub
'doan code de kiem tra tai khoan nguoi dung
Private Sub cmdok_Click()
If txtname.Text = "" And txtpassword.Text = "" Then
MsgBox "Ban chua nhap thong tin nguoi dung!"
txtname.SetFocus
Else
Bµi tËp thùc hµnh m«n: ng«n ng÷ visual basic Bé m«n : c«ng nghÖ phÇn mÒm
This exercise was written by Vu Bao Tao ( the member of caulacbovb club) 4 / 26
If txtname.Text = "" Or txtpassword.Text = "" Then
MsgBox "Ban phai nhap du thong tin !"
If txtname.Text = "" Then txtname.SetFocus
If txtpassword.Text = "" Then txtpassword.SetFocus
Else
If UCase(Trim(txtname.Text)) = "ADMIN" And UCase(Trim(txtpassword.Text)) = "123456" Then
MsgBox "Chuc mung ban da dang nhap thanh cong !"
Unload frmlogin
Else
MsgBox "Ban nhap sai thong tin, hay nhap lai !"

txtname.Text = ""
txtpassword.Text = ""
txtname.SetFocus
End If
End If
End If
End Sub
'doan code sau lam doi mau doi tuong kho con tro hoi tu va mat hoi tu
Private Sub txtname_GotFocus()
txtname.BackColor = QBColor(11)
End Sub
Private Sub txtname_LostFocus()
txtname.BackColor = QBColor(15)
End Sub
Private Sub txtpassword_GotFocus()
txtpassword.BackColor = QBColor(11)
End Sub
Private Sub txtpassword_LostFocus()
txtpassword.BackColor = QBColor(15)
End Sub
'doan code sau xu ly su kien nguoi su dung nhan phim ENTER
Private Sub txtname_KeyPress(KeyAscii As Integer)
Select Case Key Ascii
Case 13
txtpassword.SetFocus
End Select
End Sub
Private Sub txtpassword_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 13

cmdok.SetFocus
End Select
End Sub
 Thùc hiÖn ch¬ng tr×nh.
Bài tập thực hành môn: ngôn ngữ visual basic Bộ môn : công nghệ phần mềm
This exercise was written by Vu Bao Tao ( the member of caulacbovb club) 5 / 26
Bài 5: Thiết kế chơng trình tính diện tích tam giác.
Form nh sau:
Bảng thuộc tính nh sau:
Điều khiển
Thuộc tính
Điều khiển
Thuộc tính
Điều khiển
Thuộc tính
Name: lblA
Name: cmdHuy
Name: txtA
Label
Caption: Cạnh A
Command
Caption: &Hủy bỏ
Textbox
Text:
Name: lblB
Name: cmdThoat
Name: txtB
Label
Caption: Cạnh B
Command

Caption: &Thoát
Textbox
Text:
Name: lblC
Name: Frame1
Name: txtC
Label
Caption: Cạnh C
Frame
Caption: &Chọnthao tác
Textbox
Text:
Name: lblDT
Name: cmdTinh
Name: txtDT
Label
Caption:Diện tích
Command
Caption: &Tính
Textbox
Text:
Mở cửa sổ soạn thảo mã lệnh và nhập code cho các sự kiện nh sau:
Option Explicit
Dim a, b, c, p, dt As Double
Private Sub cmdHuy_Click()
txtA.Text = ""
txtB.Text = ""
txtC.Text = ""
txtdt.Text = ""
txtA.SetFocus

End Sub
Private Sub cmdThoat_Click()
Unload Me
End Sub
Private Sub cmdTinh_Click()
If txtA.Text = "" Or txtB.Text = "" Or txtC.Text = "" Then
MsgBox "Ban nhap thieu du lieu", vbOKOnly + vbQuestion
txtA.Text = 4
txtB.Text = 5
txtC.Text = 6
Bài tập thực hành môn: ngôn ngữ visual basic Bộ môn : công nghệ phần mềm
This exercise was written by Vu Bao Tao ( the member of caulacbovb club) 6 / 26
End If
a = CDbl(txtA.Text) 'ham CDbl chuyen doi gia tri trong txtA.text thanh kieu so Double
b = CDbl(txtB.Text)
c = CDbl(txtC.Text)
p = (a + b + c) / 2
If (a + b > c And b + c > a And a + c > b) Then
dt = Sqr(p * (p - a) * (p - b) * (p - c))
txtdt.Text = FormatNumber(dt, 2)
'ham formatnumber cho phep lay gia tri cua dt voi hai so le
Else
MsgBox "Khong the tinh!"
cmdHuy_Click
End If
End Sub
Private Sub txtA_GotFocus()
txtA.BackColor = QBColor (14)
End Sub
Private Sub txtb_GotFocus()

txtB.BackColor = QBColor(14)
End Sub
Private Sub txtc_GotFocus()
txtC.BackColor = QBColor(14)
End Sub
Private Sub txtA_LostFocus()
txtA.BackColor = QBColor(15)
End Sub
Private Sub txtb_LostFocus()
txtB.BackColor = QBColor(15)
End Sub
Private Sub txtc_LostFocus()
txtC.BackColor = QBColor(15)
End Sub
Private Sub txta_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 13
txtB.SetFocus
End Select
End Sub
Private Sub txtB_K eyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 13
txtC.SetFocus
End Select
End Sub
Private Sub txtc_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 13
cmdTinh.SetFocus

End Select
End Sub
Thực hiện chơng trình.
Trong trờng hợp ta không muốn cho ngời sử dụng nhập ký tự vào các textbox thì cần
viết thêm đoạn code sau vào trong chơng trình.
Bài tập thực hành môn: ngôn ngữ visual basic Bộ môn : công nghệ phần mềm
This exercise was written by Vu Bao Tao ( the member of caulacbovb club) 7 / 26
Private Sub txtA_KeyPress(KeyAscii As Integer)
If (KeyAscii < Asc("0") Or KeyAscii > Asc("9")) Then
KeyAscii = 0
End If
End Sub
Private Sub txtB_KeyPress(KeyAscii As Integer)
If (KeyAscii < Asc("0") Or KeyAscii > Asc("9")) Then
KeyAscii = 0
End If
End Sub
Private Sub txtC_KeyPress(KeyAscii As Integer)
If (KeyAscii < Asc(" 0") Or KeyAscii > Asc("9")) Then
KeyAscii = 0
End If
End Sub
Bài 6: Thiết kế chơng trình tính khoảng cách hai điểm có giao diện nh sau:
Bảng thuộc tính nh sau:
Điều
khiển
Thuộc tính
Điều
khiển
Thuộc tính

Điều khiển
Thuộc tính
Name: Label1
Name: Frame1
Name: txthesogoc
Label
Caption: X1
Frame
Caption: Điểm thứ 1
Textbox
Text:
Name: Label2
Name: Frame2
Name: txtKC
Label
Caption: Y1
Frame
Caption: Điểm thứ 2
Textbox
Text:
Name: Label3
Name: txtX1
Name: cmdTinhtoan
Label
Caption: X2
Textbox
Text:
Command
Caption: Tính toán
Name: Label4

Name: txtY1
Name: cmdthoat
Label
Caption: Y2
Textbox
Text:
Command
Caption: Thoát
Name: Label5
Name: txtX2
Label
Caption: Hệ số góc
Textbox
Text:
Bài tập thực hành môn: ngôn ngữ visual basic Bộ môn : công nghệ phần mềm
This exercise was written by Vu Bao Tao ( the member of caulacbovb club) 8 / 26
Name: Label6
Name: txtY2
Label
Caption: Khoảng cách
Textbox
Text:
Viết mã lệnh cho các sự kiện chọn nút Tính toán và Thoát nh sau:
Private Sub cmdTinhtoan_Click()
Dim x1, y1, x2, y2, d, hsg As Double
If txtX1.Text = "" Or txtY1.Text = "" Or txtX2.Tex t = "" Or txtY2.Text = "" Then
MsgBox "Ban can nhap du du lieu !"
If txtX1.Text = "" Then txtX1.SetFocus
If txtY1.Text = "" Then txtY1.SetFocus
If txtX2.Text = "" Then txtX2.SetFocus

If txtY2.Text = "" Then txtY2. SetFocus
Else
x1 = Val(txtX1.Text)
y1 = Val(txtY1.Text)
x2 = Val(txtX2.Text)
y2 = Val(txtY2.Text)
d = Sqr((x2 - 1) * (x2 - x1) + (y2 - y1) * (y2 - y1))
hsg = (y2 - y1) / (x2 - x1)
txthesogoc.Text = FormatNumber(hsg, 2)
txtKC.Text = FormatNumber(d, 2)
End If
End Sub
Private Sub cmdthoat_Click()
Unload Me
End Sub
Bài 7: Thiết kế chơng trình giải phơng trình bậc nhất ax +b=0 có giao diện nh
sau:
Bảng thuộc tính nh sau:
Điều
khiển
Thuộc tính
Điều
khiển
Thuộc tính
Điều
khiển
Thuộc tính
Name: Label1
Name: Frame1
Name: txtKQ

Label
Caption: Hệ số A
Frame
Caption: Chọn nút bấm
Textbox
Text:
Bài tập thực hành môn: ngôn ngữ visual basic Bộ môn : công nghệ phần mềm
This exercise was written by Vu Bao Tao ( the member of caulacbovb club) 9 / 26
Name: Label2
Name: txtA
Name: cmdTinh
Label
Caption: Hệ số B
Textbox
Text:
Command
Caption: &Tính
Name: Label3
Name: txtB
Name: cmdThoat
Label
Caption: Kết quả
Textbox
Text:
Command
Caption: &Thoát
Viết mã lệnh cho các sự kiện chọn nút Tính và Thoát nh sau:
Private Sub cmdTinh_Click()
Dim a, b, x As Double
If txtA.Text = "" Or txtB.Text = "" Then

MsgBox "Ban chua nhap du du lieu !"
If txtA.Text = "" Then txtA.SetFocus
If txtB.Text = "" Then txtB.SetFocus
Else
a = Val(txtA.Text)
b = Val(txtB.Text)
If a = 0 Then
If b = 0 Then
txtKQ.Text = "Phuong trinh vo so nghiem !"
Else
txtKQ.Text = "Phuong trinh vo nghiem!"
End If
Else
x = -b / a
txtKQ.Text = "Phuong trinh co nghiem x="
txtKQ.Text = txtKQ.Text & FormatNumber(x, 2)
End If
End If
End Sub
Private Sub cmdthoat_Click()
Unload Me
End Sub
Bài 8: Thiết kế chơng trình giải phơng trình bậc hai ax
2
+bx+c=0 có giao diện nh
sau:
Bài tập thực hành môn: ngôn ngữ visual basic Bộ môn : công nghệ phần mềm
This exercise was written by Vu Bao Tao ( the member of caulacbovb club) 10 / 26
Bảng thuộc tính các đi ều khiển nh sau:
Điều

khiển
Thuộc tính
Điều
khiển
Thuộc tính
Điều
khiển
Thuộc tính
Name: Label1
Name: Frame1
Name: Command1
Label
Caption: Hệ số A
Frame
Caption: Chọn
chức năng
Command
Caption: &Giải phơng
trình
Name: Label2
Name: textA
Name: Command2
Label
Caption: Hệ số B
Textbox
Text:
Command
Caption: &Xóa
Name: Label3
Name: textB

Name: Command3
Label
Caption: Hệ số C
Textbox
Text:
Command
Caption: &Thoát
Name: Label4
Name: textC
Name: Timer1
Label
Caption: Kết quả
Textbox
Text:
Timer
Interval: 150
Name: Label5
Name: Textketqua
Name: frmPTB2
Lbl1
Caption: Chuong
trinh duoc viet boi
ABC
Textbox
Text:
Form
Caption: CT giai phuong
trinh bac hai
Viết mã lệnh cho các sự kiện chọn nút Giải phơng trình, Xóa và Thoát nh sau:
Private Sub Command1_Click()

Dim a, b, c As Double
Dim delta, x, x1, x2 As Double
If TextA.Text = "" Or TextB.Text = "" Or TextC.Text = "" Then
MsgBox "Ban nhap thieu du lieu", vbOKOnly + vbQuestion
TextA.Text = 4
TextB.Text = -6
TextC.Text = 2
End If
a = Val(TextA.Text)
b = Val(TextB.Text)
c = Val(TextC.Text)
delta = b * b - 4 * a * c
If (delta < 0) Then
Textketqua.Text = " Phơng trình vô nghiệm"
End If
If (delta = 0) Then
x = -b / (2 * a)
Textketqua.Text = " Phơng trình có nghiệm kép x= " & FormatNumber(x, 2)
End If
If (delta > 0) Then
x1 = (-b + Sqr(delta)) / (2 * a)
x2 = (-b - Sqr(delta)) / (2 * a)
Textketqua.Text = " Phơng trình có hai nghiệm x1= " & FormatNumber(x1, 2) & " và x2= " &
FormatNumber(x2, 2)
End If
End Sub
Private Sub Command2_Click()
TextA.Text = ""
TextB.Text = ""
TextC.Text = ""

Textketqua.Text = ""
Bài tập thực hành môn: ngôn ngữ visual basic Bộ môn : công nghệ phần mềm
This exercise was written by Vu Bao Tao ( the member of caulacbovb club) 11 / 26
End Sub
Private Sub Timer1_Timer()
Dim x, y As String
x = Left(lbl1.Caption, 1)
y = Right(lbl1.Caption, Len(lbl1.Caption) - 1)
lbl1.Caption = y + x
End Sub
Private Sub texta_GotFocus()
TextA.BackColor = QBColor(14)
End Sub
Private Sub textb_GotFocus()
TextB.BackColor = QBColor(14)
End Sub
Private Sub textc_GotFocus()
TextC.BackColor = QBColor(14)
End Sub
Private Sub textA_LostFocus()
TextA.BackColor = QBColor(15)
End Sub
Private Sub textb_LostFocus()
TextB.BackColor = QBColor(15)
End Sub
Private Sub textc_LostFocus()
TextC.BackColor = QBColor(15)
End Sub
Private Sub texta_KeyPress(KeyAscii As Integer)
Select Case KeyAscii

Case 13
TextB.SetFocus
End Select
End Sub
Private Sub textB_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 13
TextC.SetFocus
End Select
End Sub
Private Sub textc_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 13
Command1.SetFocus
End Select
End Sub
Bài 9: Thiết kế chơng trì nh minh họa việc sử dụng điều khiển timer có giao dện
nh sau;
Bài tập thực hành môn: ngôn ngữ visual basic Bộ môn : công nghệ phần mềm
This exercise was written by Vu Bao Tao ( the member of caulacbovb club) 12 / 26
Bảng thuộc tính nh sau:
Điều
khiển
Thuộc tính
Điều
khiển
Thuộc tính
Điều
khiển
Thuộc tính

Name: Label1
Name: Label3
Name: Command1
Label
Caption:
Label
Caption: Timer1
Command
Caption: &Close
Name: Label2
Name: Label4
Name: Timer1
Label
Caption:
Label
Caption: Timer2
Timer
Interval: 1000
Viết mã lệnh cho chơng trình nh sau:
Dim n As Integer
Private Sub Form_Load()
Label1.Caption = Format(Time, "hh:mm:ss:nn")
n = 60
Label2.Caption = n
End Sub
Private Sub Timer1_Timer()
n = n - 1
Label2.Caption = n
Label1.Caption = Format(Time, "hh:mm:ss:nn")
If n = 0 Then

MsgBox "Da het 60 giay"
Label1.Caption = ""
Label2.Caption = ""
Timer1.Enabled = False
End If
End Sub
'
Private Sub Command1_Click()
Unload Me
End Sub
Bài 10: Thiết kế chơng trình LoadPicture minh họa việc sử dụng điều khiển
RadioOption và điều khiển Image
Giao diện của chơng trình:
Bài tập thực hành môn: ngôn ngữ visual basic Bộ môn : công nghệ phần mềm
This exercise was written by Vu Bao Tao ( the member of caulacbovb club) 13 / 26
Giả sử trong th mục của chơng trình đã tồn tại 4 file ảnh lần lợt có tên là:
Flower.jpg , Noel.jpg , Fishes.jpg , WestLake.jpg
Bảng thuộc tính của các điều khiển nh sau:
Điều
khiển
Thuộc tính
Điều
khiển
Thuộc tính
Điều
khiển
Thuộc tính
Name: optFlower
Name: Frame1
Name: imgWestLake

Option
Caption: Flower
Frame
Caption: Selection
Image
Stretch: True
Name: optNoel
Name: imgFlower
Name: cmdLoad
Option
Caption: Noel
Image
Stretch: True
Command
Caption: &Load
Name: optFishes
Name: imgNoel
Name: cmdExit
Option
Caption: Fishes
Image
Stretch: True
Command
Caption: &Exit
Name: optWestLake
Name: imgFishes
Name: frmLoadPicture
Option
Caption: WestLake
Image

Stretch: True
Form
Caption: LoadPicutre
Viết mã lệnh để xử lý sự kiện khi bấm nút Load và Exit
Private Sub CmdLoad_Click()
If optflower.Value = True Then
imgflower.Picture = LoadPicture(App.Path & " \flower.jpg")
'ham app.path tra ve duong dan toi thu muc cua chuong trinh
imgwestlake.Picture = LoadPicture("")
imgNoel.Picture = Lo adPicture("")
imgfishes.Picture = LoadPicture("")
'lam rong cacimgture khac
End If
If optNoel.Value = True Then
imgNoel.Picture = LoadPicture(App.Path & " \Noel.jpg")
imgwestlake.Picture = LoadPicture("")
imgflower.Picture = LoadPicture("")
imgfishes.Picture = LoadPicture("")
End If
If optfishes.Value = True Then
imgfishes.Picture = LoadPicture(App.Path & " \fishes.jpg")
imgwestlake.Picture = LoadPicture("")
imgflower.Picture = Loa dPicture("")
imgNoel.Picture = LoadPicture("")
End If
If optWestLake.Value = True Then
imgwestlake.Picture = LoadPicture(App.Path & " \westlake.jpg")
imgfishes.Picture = LoadPicture("")
imgflower.Picture = LoadPicture("")
imgNoel.Picture = LoadPicture("")

End If
End Sub
Private Sub CmsExit_Click()
Unload Me
End Sub
Bài tập thực hành môn: ngôn ngữ visual basic Bộ môn : công nghệ phần mềm
This exercise was written by Vu Bao Tao ( the member of caulacbovb club) 14 / 26
Kết quả khi thực hiện chơng trình:
Yêu cầu HS-SV sửa code chơng trình để kết quả khi thực hiện chơng trình nh
sau:
Bài tập thực hành môn: ngôn ngữ visual basic Bộ môn : công nghệ phần mềm
This exercise was written by Vu Bao Tao ( the member of caulacbovb club) 15 / 26
Bài 11: Thiết kế chơng trình sử dụng điều khiển Option Button và Checkbox
Chơng trình gồm có hai form:
Giao diện của form thứ nhất: Giao diện của form thứ hai:
Bảng thuộc tính của hai form nh sau:
Form thứ nhất
Điều khiển
Thuộc tính
Điều khiển
Thuộc tính
Điều khiển
Thuộc tính
Name: Label1
Name: Frame1
Name: Check5
Label
Caption: Họ và tên
Frame
Caption:Thu nhập

Checkbox
Caption: T nhân
Name: Label2
Name: Frame2
Name: Check6
Label
Caption: Năm sinh
Frame
Caption:Cơ quan
Checkbox
Caption: Liên doanh
Name: Label3
Name: Frame3
Name: Option1
Label
Caption: Quê quán
Frame
Caption:Chức vụ
Option
Caption: Đảng viên
Name:Text1
Name: Check1
Name: Option2
Textbox
Text:
Checkbox
Caption: Lơng
chính
Option
Caption: Đoàn viên

Name:Text2
Name: Check2
Name: Command1
Textbox
Text:
Checkbox
Caption: Phụ cấp
Command
Caption: &Nhập
Name:Text3
Name: Check3
Name: Command2
Textbox
Text:
Checkbox
Caption: Làm thêm
Command
Caption: &Đóng
Name: form1
Name: Check4
Form
Caption: Checkbox
and OptionButton
Checkbox
Caption: Nhà nớc
Form thứ hai
Điều khiển
Thuộc tính
Điều khiển
Thuộc tính

Điều khiển
Thuộc tính
Name: form2
Name: Text1
Name: Command1
Caption:
Text:
Command
Caption: &Close
BorderStyle: None
Multiline: True
Name: Label1
Form
Textbox
Label
Captiop:Thông tin của
nhân viên vừa nhập
Viết mã lệnh xử lý sự kiện nhấn nút Nhập v à nút Đóng của form thứ nhất.
Bài tập thực hành môn: ngôn ngữ visual basic Bộ môn : công nghệ phần mềm
This exercise was written by Vu Bao Tao ( the member of caulacbovb club) 16 / 26
Private Sub Command1 _Click()
Dim str As String
str = Form1.Text1.Text & " " & Form1.Text2.Text & " " & Form1.Text3.Text & "có thu nhâp từ "
'
If Check1.Value = True Then
str = str & Check1.Caption & " và "
End If
If Check1.Value = 1 Then
str = str & Check1.Caption & " và "
End If

If Check2.Value = 1 Then
str = str & Check2.Caption & "và "
End If
If Check3.Value = 1 Then
str = str & Check3.Caption & "và "
End If
'
str = str & " và làm trong cơ quan "
If Check4.Value = 1 Th en
str = str & Check4.Caption & "và "
End If
If Check5.Value = 1 Then
str = str & Check5.Caption & " và "
End If
If Check6.Value = 1 Then
str = str & Check6.Caption & " và "
End If
'
str = str & " có chúc vụ la "
If Option1.Value = True Then
str = str & Option1.Caption
End If
If Option2.Value = True Then
str = str & Option2.Caption
End If
Form1.Hide
Form2.Show
Form2.Text1.Text = str
End Sub
Private Sub Command2_Click()

Unload Form1
End Sub
Viết mã lệnh xử lý sự ki ện nhấn nút Close của form thứ hai.
Private Sub Command1_Click()
Unload Form2
Form1.Show
End Sub
Bài tập thực hành môn: ngôn ngữ visual basic Bộ môn : công nghệ phần mềm
This exercise was written by Vu Bao Tao ( the member of caulacbovb club) 17 / 26
Bài 11: Thiết kế chơng trình PictureViewer .
Giao diện chơng trình.
Bảng thuộc tính của các điều khiển nh sau:
Điều khiển
Thuộc tính
Điều
khiển
Thuộc tính
Điều
khiển
Thuộc tính
DriverListBox
Name: Driver1
Name: Timer1
Name: Image1
DirListBox
Name: Dir1
Timer
Interval: 100
Image
Stretch: True

Name: File1
Name: Label2
Name: frmMain
FileListBox
Patern: *.jpg; *.bmp; *.gif
Name: Label1
Label
Caption: PictureViewer
Label
Caption: Chơng
trình đợc viết bởi
Vũ Bảo Tạo
Form
Caption:
PictureViewer
Viết mã lệnh cho chơng trình nh sau:
Private Sub Dir1_Change()
File1.Path = Dir1.Path
End Sub
Private Sub Drive1_Change()
Dir1.Path = Drive1.Drive
End Sub
Private Sub File1_Click()
Image1.Picture = LoadPicture(File1.Path & " \" & File1.FileName)
End Sub
Private Sub Timer1_Timer()
Dim x, y As String
x = Left(Label2.Caption, 1)
y = Right(Label2.Caption, Len(Label2. Caption) - 1)
Label2.Caption = y + x

End Sub
Bài tập thực hành môn: ngôn ngữ visual basic Bộ môn : công nghệ phần mềm
This exercise was written by Vu Bao Tao ( the member of caulacbovb club) 18 / 26
Bài 12: Thiết kế chơng trình Tính hai số
Giao diện chơng trình.
Bảng thuộc tính của các điều khiển nh sau:
Điều
khiển
Thuộc tính
Điều
khiển
Thuộc tính
Điều khiển
Thuộc tính
Name: Label1
Name: txtso1
Name: optnhan
Label
Caption: Số 1
Textbox
Text:
Option
Caption: Nhân
Name: Label2
Name: txtso2
Name: optchia
Label
Caption: Số 2
Textbox
Text:

Option
Caption: Chia
Name: Label3
Name: txtkq
Name: cmdTinh
Label
Caption: Kết quả
Textbox
Text:
Command
Caption: Tính
Name: Frame1
Name: optcong
Name: cmdXoa
Frame
Caption: Chọn nút bấm
Option
Caption: Cộng
Command
Caption: Xóa
Name: Frame2
Name: opttru
Name: cmdThoat
Frame
Caption: Chọn phép tính
Option
Caption: Trừ
Command
Caption: Thoát
Viết mã lệnh cho chơng trình nh sau:

Private Sub cmdTinh_Click()
Dim so1, so2, kq As Double 'khai bao 3 bien
If txtso1.Text = "" And txtso2.Text = "" Then
MsgBox "Ban chua nhap du lieu !"
txtso1.SetFocus
Else
If txtso1.Text = "" Or txtso2.Text = "" Then
MsgBox "Ban can nhap du du lieu !"
If txtso1.Text = "" Then txtso1.SetFocus
If txtso2.Text = "" Then txtso2.SetFocus
Else
so1 = Val(txtso1.Text)
so2 = Val(txtso2.Text)
If optCong.Value = True Then kq = so1 + so2
If opttru.Value = True Then kq = so1 - so2
If optnhan.Value = True Then kq = so1 * so2
Bµi tËp thùc hµnh m«n: ng«n ng÷ visual basic Bé m«n : c«ng nghÖ phÇn mÒm
This exercise was written by Vu Bao Tao ( the member of caulacbovb club) 19 / 26
If optchia.Value = True Then
If so2 = 0 Then
MsgBox "Khong the chia, hay nhap lai gia tri so 2!"
txtso2.Text = ""
txtso2.SetFocus
Else
kq = so1 / so2
End If
End If
End If
End If
'dua ket qua ra o ket qua

txtkq.Text = FormatNumber(kq, 2)
End Sub
Private Sub cmdXoa_Click()
txtso1.Text = ""
txtso2.Text = ""
txtkq.Text = ""
txtso1.SetFocus
End Sub
Private Sub txtso2_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 13
optCong.SetFocus
cmdTinh.SetFocus
End Select
End Sub
Private Sub txtso1_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 13
txtso2.SetFocus
End Select
End Sub
Private Sub txtso1_LostFocus()
txtso1.BackColor = QBColor(15)
End Sub
Private Sub txtso2_LostFocus()
txtso2.BackColor = QBColor(15)
End Sub
Private Sub txtso1_GotFocus()
txtso1.BackColor = QBColor(14)
End Sub

Private Sub txtso2_GotFocus()
txtso2.BackColor = QBColor(14)
End Sub
Private Sub txtkq_GotFocus()
MsgBox "Khong duoc nhap du lieu vao day !"
optCong.SetFocus
End Sub
Private Sub cmdThoat_Click()
Unload Me
Bài tập thực hành môn: ngôn ngữ visual basic Bộ môn : công nghệ phần mềm
This exercise was written by Vu Bao Tao ( the member of caulacbovb club) 20 / 26
End Sub
Bài 13: Thiết kế chơng trình ChangeMone y
Giao diện chơng trình.
Bảng thuộc tính của các điều khiển nh sau:
Điều
khiển
Thuộc tính
Điều
khiển
Thuộc tính
Điều
khiển
Thuộc tính
Name: Frame1
Name: optMac
Name: cmdNhap
Frame
Caption: Ngoại tệ
Option

Caption: &Mác Đức
Command
Caption: &Nhập dữ liệu
Name: Frame2
Name: optRup
Name: cmdTinhvaIn
Frame
Caption: Công việc
Option
Caption: &Rúp Nga
Command
Caption:&Tính và In
Name: optBang
Name:frmDoitien
Name: cmdXoa
Option
Caption: &Bảng Anh
Command
Caption: &Xóa
Name: optDola
Name: cmdKetthuc
Option
Caption: &Dola Mỹ
Form
Caption:
ChangeMoney
Command
Caption: &Kết thúc
Yêu cầu trớc khi viết code cho chơng trình sinh viên cần phải tìm hiểu và
nghiên cứu về Hộp thông báo (Msgbox) và hộp nhập (InputBox)

Viết mã lệnh cho chơng trình nh sau:
Dim ten, tienviet, tienngoaite, tygia, tenngoaite
Private Sub CmdNhap_Click()
ten = InputBox("Ten khach hang = ", "Nhap ten khach hang", " Le Van Hai")
tienviet = 1000 * Val(InputBox("So tien can doi= (000)"))
End Sub
Private Sub CmdXoa_Click()
Cls
End Sub
Private Sub CmdKetthuc_Click()
Beep
End
End Sub
Bài tập thực hành môn: ngôn ngữ visual basic Bộ môn : công nghệ phần mềm
This exercise was written by Vu Bao Tao ( the member of caulacbovb club) 21 / 26
Private Sub CmdTinhvaIn_Click()
Cls
If optBang.Value Then
tienngoaite = "Bang Anh"
tygia = 21000
End If
'
If OptMac.Value Then
tienngoaite = "Mac Duc"
tygia = 12000
End If
'
If OptRup.Value Then
tienngoaite = "Rup Nga"
tygia = 800

End If
'
If optDola.Value Then
tienngoaite = "Dola My"
tygia = 17900
End If
tienngoaite = tienviet / tygia
Print
Print "Khach hang: "; ten
Print "So tien Viet la: "; tienviet
Print "Ty gia so voi " + tenngoaite + "la: "; tygia
Print "So tien bang " + tenngoaite + "la: "; FormatNumber(tienngoaite, 1)
End Sub
Bài 14: Thiết kế chơng trình Thống kê
Để thiết kế đợc chơng trình sinh viên cần phải có kiến thức về mảng (array), các
điều khiển nh hộp đánh dấu (Checbox), khung (frame). Ngoài ra sinh viên phải
nắm vững cấu trúc lặp for.
Giao diện chơng trình
Form thứ nhất
Bài tập thực hành môn: ngôn ngữ visual basic Bộ môn : công nghệ phần mềm
This exercise was written by Vu Bao Tao ( the member of caulacbovb club) 22 / 26
Bảng thuộc tính của form thứ nhất.
Điều
khiển
Thuộc tính
Điều
khiển
Thuộc tính
Name: FrmThongKe
Name: Frame1

Form
Caption: Chơng trình thống kê
Frame
Caption: Giải trí
Name: CmdSave
Name: Frame2
Command
Caption:&Lu
Frame
Caption: Xe gắn máy
Name: CmdResult
Name: Frame3
Command
Caption: &Kết quả
Frame
Caption: Công việc
Name: CmdExit
Name: lbl1
Command
Caption: &Thoát
Lable
Caption: Xin moi cac ban lua .
Name: Timer1
Timer
Interval: 150
Tạo 3 mảng điều khiển CheckBox lần lợt là Chksothich, Chkxe, ChkCongviec, mỗi
mảng gồm 7 phần tử.
Ví dụ:
Chksothich (0).Caption = "Âm nhạc"
Gán cho thuộc tính Caption của hộp đánh dấu

ChKsothich có chỉ số index là 0 một chuỗi ký tự là
"Âm nhạc"
Form thứ hai
Bảng thuộc tính của form thứ hai
Điều
khiển
Thuộc tính
Điều
khiển
Thuộc tính
Name: FrmBC
Name: Text1
Caption:
Text:
BorderStyle: 0- None
Backcolor: Màu vàng nhạt
Form
Backcolor: Màu xanh nhạt
Textbox
Name: Command1
Name: Label1
Command
Caption: &Close
Lable
Caption:Kết quả thống kê
Viết code cho form thứ nhất nh sau:
Dim a(7), b(7), c(7) As Integer 'khai bao 3 mang
Dim maxa, maxb, maxc As Integer ' khai bao 3 bien de luu gia tri max ung voi 3 mang
Dim solanchon As Integer
Dim maxsothich, maxxe, maxcongviec As String

Dim xau As String
Bài tập thực hành môn: ngôn ngữ visual basic Bộ môn : công nghệ phần mềm
This exercise was written by Vu Bao Tao ( the member of caulacbovb club) 23 / 26
Private Sub Form_Load()
solanchon = 0 'khoi tao so lan nguoi su dung chon la 0
Dim i As Integer
For i = 0 To 6
Chksothich(i).Value = 0
chkxe(i).Value = 0
chkcongviec(i).Value = 0 'huy danh dau cac hop kiem
a(i) = 0: b(i) = 0: c(i) = 0 'ba lenh duoc viet tren mot dong
'khoi tao gia tri cho cac phan tu cua 3 mang bang 0
Next i
End Sub
Private Sub cmdsave_Click()
solanchon = solanchon + 1
For i = 0 To 6
If Chksothich(i).Value = 1 Then a(i) = a(i) + 1
If chkxe(i).Value = 1 Then b(i) = b(i) + 1
If chkcongviec(i).Value = 1 Then c(i) = c(i) + 1
'neu nguoi su dun g chon hop kiem nao thi cong them mot gia tri
Next i
For i = 0 To 6
Chksothich(i).Value = 0
chkxe(i).Value = 0
chkcongviec(i).Value = 0 'huy danh dau cac hop kiem de khoi tao lai
Next i
End Sub
Private Sub cmdresult_Click()
'timhop checkbox duoc chon nhieu nhat o moi mang

'tim max cua mang a ung voi so thich duoc chon nhieu nhat
maxa = a(0)
For i = 0 To 6
If maxa < a(i) Then maxa = a(i)
Next i
'tim max cua mang b ung voi loai xe duoc chon nhieu nhat
maxb = b(0)
For i = 0 To 6
If maxb < b(i) Then maxb = b(i)
Next i
'tim max cua mang c ung voi cong viec duoc chon nhieu nhat
maxc = c(0)
For i = 0 To 6
If maxc < c(i) Then maxc = c(i)
Next i
'Tim ten sothich, xe m ay, cong viec duoc chon nhieu nhat va luu vao cac bien
For i = 0 To 6
If maxa = a(i) Then maxsothich = Chksothich(i).Caption
If maxb = b(i) Then maxxe = chkxe(i).Caption
If maxc = c(i) Then maxcongviec = chkcongviec(i).Caption
Next i
'goi hien thi ket qua
xau = "Số lần lựa chọn là: " & solanchon & _
" - Sở thích chọn nhiều nhất là: " & maxsothich & _
" - Loại xe đợc a chuộng nhất là: " & maxxe & _
" - Nghề đợc a chuộng nhi ều nhất là: " & maxcongviec
frmBC.Show
frmBC.Text1.Text = xau
frmThongke.Hide
End Sub

Bài tập thực hành môn: ngôn ngữ visual basic Bộ môn : công nghệ phần mềm
This exercise was written by Vu Bao Tao ( the member of caulacbovb club) 24 / 26
Private Sub cmdexit_Click()
Unload frmThongke
End Sub
Private Sub Timer1_Timer()
Dim x, y As String
x = Left(lbl1.Caption, 1)
y = Right(lbl1.C aption, Len(lbl1.Caption) - 1)
lbl1.Caption = y + x
End Sub
Viết code cho form thứ hai nh sau:
Private Sub Command1_Click()
Unload frmBC
frmThongke.Show
End Sub
Bài 15: Thiết kế chơng trình máy tính
Để thiết kế đợc chơng trình sinh viên cầ n phải có kiến thức về mảng (array), các
điều khiển nh hộp đánh dấu (Checbox), nút bấm (CommandButton). Ngoài ra
sinh viên phải nắm vững cấu trúc lặp for.
Giao diện chơng trình
Viết mã lệnh cho chơng trình nh sau:
Dim status, vdecimal, ope r, ce As Byte
Dim so As Double
Private Sub Form_Load()
status = 0
vdecimal = 0
ce = 0
txtresult.Text = "0."
End Sub

'
Private Sub cmdCancel_Click()
txtresult.Text = "0."
status = 0
vdecimal = 0
oper = ""
ce = 0
so = 0
Bµi tËp thùc hµnh m«n: ng«n ng÷ visual basic Bé m«n : c«ng nghÖ phÇn mÒm
This exercise was written by Vu Bao Tao ( the member of caulacbovb club) 25 / 26
End Sub
Private Sub cmdCE_Click()
ce = 0
vdecimal = 0
txtresult.Text = "0."
so = 0
End Sub
'
Private Sub cmdnumber_Click(Index As Integer)
If ce = 0 Then
txtresult.Text = Index
ce = 1
Else
txtresult.Text = txtresult.Text & Index
End If
End Sub
'
Private Sub Opera tor_Click(Index As Integer)
oper = Operator(Index).Caption
If txtresult.Text = "0." Then

so = 0
Else
so = Val(txtresult.Text)
End If
'
status = 1
txtresult.Text = "0."
vdecimal = 0
ce = 0
End Sub
'
Private Sub cmdpoint_Click()
If vdecimal = 0 Then
If ce = 0 Then
txtresult.Text = "0."
ce = 1
Else
txtresult.Text = txtresult.Text & "."
End If
vdecimal = 1
End If
End Sub
Private Sub CmdEqual_Click()
If status = 1 Then
Select Case oper
Case "+": txtresult = FormatNumber(so + Val(txtresult.Text), 1)
Case "-": txtresult = FormatNumber(so - Val(txtresult.Text), 1)
Case "*": txtresult = FormatNumber(so * Val(txtresult.Text), 1)
Case "/":
If (Val(txtresult.Text) <> 0) Then

txtresult.Text = FormatNumber(so / Val(txtresult.Text), 1)
Else
txtresult.Text = "Cannot divide by zero."
End If
End Select
End If
End Sub
 Thùc hiÖn ch¬ng tr×nh

×