Code thực hành tin học đại cương. ctgttp
-
Upload
ttx-love -
Category
Engineering
-
view
107 -
download
1
description
Transcript of Code thực hành tin học đại cương. ctgttp
2014
Bài 1: Tính diện tích và chu vi hình trònPrivate Sub cmdnhapmoi_Click()
txtbankinh.Text = ""
txtchuvi.Text = ""
txtdientich.Text = ""
txtbankinh.SetFocus
End Sub
Private Sub cmdthoat_Click()
If MsgBox("Bạn có muốn thoát chương trình?", vbYesNo + vbQuestion, "Thông báo") = vbYes Then
End
End If
End Sub
Private Sub cmdtinh_Click()
Dim r As Single
Dim s, p As Single
r = Val(txtbankinh.Text)
If Not IsNumeric(txtbankinh.Text) Then
MsgBox "Bạn nhập không phải số , hãy nhập lại", vbOKOnly, " Thông báo "
txtbankinh.SetFocus
Exit Sub
End If
p = 2 * 3.14 * r
s = 3.14 * r * r
txtchuvi.Text = CStr(p)
Công trình giao thông thành ph _khóa 54ố Page 1
2014
txtdientich.Text = CStr(s)
End Sub
Bài 2: Tính diện tích và chu vi tam giácPrivate Sub cmdThoat_click()
If MsgBox("Bạn có muốn thoát chương trình?", vbYesNo + vbQuestion, " Thông báo ") = vbYes Then
End
End If
End Sub
Private Sub cmdLamMoi_click()
txtxa.Text = " "
Txtya.Text = " "
txtxb.Text = " "
txtyb.Text = " "
txtxc.Text = " "
txtyc.Text = " "
txtdientich.Text = " "
txtchuvi.Text = " "
txtxa.SetFocus
End Sub
Private Sub cmdTinh_Click()
Dim Xa, Ya, Xb, Yb, Xc, Yc As Single
Dim s, p, cv As Single
Dim a, b, c As Single
Xa = Val(txtxa.Text)
Công trình giao thông thành ph _khóa 54ố Page 2
2014
Ya = Val(Txtya.Text)
Xb = Val(txtxb.Text)
Ya = Val(txtyb.Text)
Xc = Val(txtxc.Text)
Yc = Val(Txtya.Text)
If Not IsNumeric(txtxa.Text) Then
MsgBox " Bạn nhập Xa không phải số , hãy nhập lại ", vbExclamation, " Thông báo "
txtxa.SetFocus
Exit Sub
End If
If Not IsNumeric(Txtya.Text) Then
MsgBox " Bạn nhập Ya không phải số , hãy nhập lại ", vbExclamation, " Thông báo "
Txtya.SetFocus
Exit Sub
End If
If Not IsNumeric(txtxb.Text) Then
MsgBox " Bạn nhập Xb không phải số , hãy nhập lại ", vbExclamation, " Thông báo "
txtxb.SetFocus
Exit Sub
End If
If Not IsNumeric(txtyb.Text) Then
MsgBox " Bạn nhập Yb không phải số , hãy nhập lại ", vbExclamation, " Thông báo "
txtyb.SetFocus
Exit Sub
End If
Công trình giao thông thành ph _khóa 54ố Page 3
2014
If Not IsNumeric(txtxc.Text) Then
MsgBox " Bạn nhập Xc không phải số , hãy nhập lại ", vbExclamation, " Thông báo "
txtxc.SetFocus
Exit Sub
End If
If Not IsNumeric(txtyc.Text) Then
MsgBox " Bạn nhập Yc không phải số , hãy nhập lại ", vbExclamation, " Thông báo "
txtyc.SetFocus
Exit Sub
End If
a = Sqr((Xb - Xc) ^ 2 + (Yb - Yc) ^ 2)
b = Sqr((Xa - Xc) ^ 2 + (Ya - Yc) ^ 2)
c = Sqr((Xa - Xb) ^ 2 + (Ya - Yb) ^ 2)
If ((a + b > c) And (b + c > a) And (c + a > b)) Then
p = (a + b + c) / 2
s = Sqr(p * (p - a) * (p - b) * (p - c))
cv = 2 * p
Else
MsgBox "Tọa độ bạn nhập không phải là tọa độ 3 đỉnh của 1 tam giác , hãy nhập lại", vbOKOnly, " Thông báo "
txtxa.SetFocus
Exit Sub
End If
txtdientich.Text = CStr(s)
txtchuvi.Text = CStr(cv)
End Sub
Công trình giao thông thành ph _khóa 54ố Page 4
2014
Bài 3: Tính diện tích hình vành khănPrivate Sub cmdlamlai_Click()
txtbankinh1.Text = " "
txtbankinh2.Text = " "
txtchuvi1.Text = " "
txtchuvi2.Text = " "
txtdientich.Text = " "
txtbankinh1.SetFocus
End Sub
Private Sub cmdthoat_Click()
If MsgBox("Bạn có muốn thoát chương trình?", vbYesNo + vbQuestion, " Thông báo ") = vbYes Then
End
End If
End Sub
Private Sub cmdtinh_Click()
Dim a, b As Single
Dim p1, p2 As Single
Dim s, s1, s2 As Single
a = Val(txtbankinh1.Text)
b = Val(txtbankinh2.Text)
If Trim(txtbankinh1.Text) = "" Then
MsgBox " Bạn phải nhập bán kính đường tròn D1 vào",vbOkOnly, "Thông báo"
txtbankinh1.SetFocus
Exit Sub
Công trình giao thông thành ph _khóa 54ố Page 5
2014
End If
If Trim(txtbankinh2.Text) = "" Then
MsgBox " Bạn phải nhập bán kính đường tròn D2 vào",vbOkOnly, "Thông báo"
txtbankinh2.SetFocus
Exit Sub
End If
If ((a > 0) And (b > 0) And (a > b)) Then
p1 = 2 * 3.14 * a
p2 = 2 * 3.14 * b
s1 = 3.14 * a * a
s2 = 3.14 * b * b
s = s1 - s2
Else
MsgBox " Bạn phải nhập giá trị D1 lớn hơn giá trị D2", vbOKOnly, " Thông báo "
txtbankinh1.SetFocus
Exit Sub
End If
txtchuvi1.Text = CStr(p1)
txtchuvi2.Text = CStr(p2)
txtdientich.Text = CStr(s)
End Sub
Private Sub txtbankinh1_KeyPress(KeyAscii As Integer)
If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 46 And KeyAscii <> 8 Then
KeyAscii = 0
End If
Công trình giao thông thành ph _khóa 54ố Page 6
2014
End Sub
Private Sub txtbankinh2_KeyPress(KeyAscii As Integer)
If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 46 And KeyAscii <> 8 Then
KeyAscii = 0
End If
End Sub
Bài 4: Bốn phép tính Form 1Private Sub cmdchia_Click()
Dim a, b As Single
Dim t As Single
a = Val(txtso1.Text)
b = Val(txtso2.Text)
If Trim(txtso1.Text) = "" Then
MsgBox "Bạn phải nhập số liệu vào ô số 1",vbOkOnly, "Thông báo"
txtso1.SetFocus
Exit Sub
End If
If Trim(txtso2.Text) = "" Then
MsgBox "Bạn phải nhập đủ số liệu vào ô số 2",vbOkOnly, "Thông báo"
txtso2.SetFocus
Exit Sub
End If
If b = 0 Then
MsgBox " Giá trị b phải khác 0", vbOKOnly + vbExclamation, " Thông báo "
Công trình giao thông thành ph _khóa 54ố Page 7
2014
txtso2.SetFocus
Exit Sub
Else
t = a / b
End If
txtketqua.Text = CStr(t)
End Sub
Private Sub cmdcong_Click()
Dim a, b As Single
Dim s As Single
a = Val(txtso1.Text)
b = Val(txtso2.Text)
If Trim(txtso1.Text) = "" Then
MsgBox "Bạn phải nhập số liệu vào ô số 1",vbOkOnly, "Thông báo"
txtso1.SetFocus
Exit Sub
End If
If Trim(txtso2.Text) = "" Then
MsgBox "Bạn phải nhập số liệu vào ô số 2",vbOkOnly, "Thông báo"
txtso2.SetFocus
Exit Sub
End If
s = a + b
txtketqua.Text = CStr(s)
End Sub
Công trình giao thông thành ph _khóa 54ố Page 8
2014
Private Sub cmdnhan_Click()
Dim a, b As Single
Dim q As Single
a = Val(txtso1.Text)
b = Val(txtso2.Text)
If Trim(txtso1.Text) = "" Then
MsgBox " Bạn phải nhập số liệu vào ô số 1",vbOkOnly, "Thông báo"
txtso1.SetFocus
Exit Sub
End If
If Trim(txtso2.Text) = "" Then
MsgBox " Bạn phải nhập số liệu vào ô số 2",vbOkOnly, "Thông báo"
txtso2.SetFocus
Exit Sub
End If
q = a * b
txtketqua.Text = CStr(q)
End Sub
Private Sub cmdnhapmoi_Click()
txtso1.Text = " "
txtso2.Text = " "
txtketqua.Text = " "
txtso1.SetFocus
End Sub
Private Sub cmdthoat_Click()
Công trình giao thông thành ph _khóa 54ố Page 9
2014
If MsgBox("Bạn có muốn thoát chương trình?", vbYesNo + vbQuestion, " Thông báo ") = vbYes Then
End
End If
End Sub
Private Sub cmdtru_Click()
Dim a, b As Single
Dim p As Single
a = Val(txtso1.Text)
b = Val(txtso2.Text)
If Trim(txtso1.Text) = "" Then
MsgBox " Bạn phải nhập số liệu vào ô số 1",vbOkOnly, "Thông báo"
txtso1.SetFocus
Exit Sub
End If
If Trim(txtso2.Text) = "" Then
MsgBox " Bạn phải nhập số liệu vào ô số 2",vbOkOnly, "Thông báo"
txtso2.SetFocus
Exit Sub
End If
p = a - b
txtketqua.Text = CStr(p)
End Sub
Private Sub txtso1_KeyPress(KeyAscii As Integer)
If ((KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 46) And KeyAscii <> 8 Then
KeyAscii = 0
Công trình giao thông thành ph _khóa 54ố Page 10
2014
End If
End Sub
Private Sub txtso2_KeyPress(KeyAscii As Integer)
If ((KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 46) And KeyAscii <> 8 Then
KeyAscii = 0
End If
End Sub
Bài 5 : Bốn phép tính Form 2Private Sub cmdlammoi_Click()
txta.Text = ""
txtb.Text = ""
txtketqua = ""
cbopheptinh.Text = ""
txta.SetFocus
End SubPrivate Sub cmdthoat_Click()
If MsgBox("Bạn có muốn thoát chương trình?", vbYesNo + vbQuestion, " Thông báo ") = vbYes Then
End
End If
End Sub
Private Sub cmdthuchien_Click()
Dim a, b As Integer
Dim s As Single
Dim pheptinh As String
pheptinh = CStr(cbopheptinh.Text)
Công trình giao thông thành ph _khóa 54ố Page 11
2014
a = Val(txta.Text)
b = Val(txtb.Text)
If Not IsNumeric(txta.Text) Then
MsgBox "Bạn phải nhập số vào ô A", vbOKOnly, " Thông báo "
txta.SetFocus
Exit Sub
End If
If Not IsNumeric(txtb.Text) Then
MsgBox "Bạn phải nhập số vào ô B", vbOKOnly, " Thông báo "
txtb.SetFocus
Exit Sub
End If
Select Case pheptinh
Case "+":
s = (a + b)
Case "-":
s = (a - b)
Case "*":
s = (a * b)
Case "/":
If b <> 0 Then
s = (a / b)
Else
MsgBox "Giá trị của B phải khác 0 nhập lại",vbOkOnly, "Thông báo"
txtb.SetFocus
Công trình giao thông thành ph _khóa 54ố Page 12
2014
End If
End Select
txtketqua.Text = CStr(s)
End Sub
Private Sub Form_Load()
cbopheptinh.AddItem "+"
cbopheptinh.AddItem "-"
cbopheptinh.AddItem "*"
cbopheptinh.AddItem "/"
End Sub
Bài 6: Giải phương trình bậc 2Private Sub cmdgiai_Click()
Dim a, b, c As Single
Dim delta As Single
a = Val(txta.Text)
b = Val(txtb.Text)
c = Val(txtc.Text)
If Trim(txta.Text) = "" Then
MsgBox "Bạn phải nhập số liệu cho a", vbQuestion, " Thông báo "
txta.SetFocus
Exit Sub
End If
If Trim(txtb.Text) = "" Then
MsgBox " Bạn phải nhập số liệu cho b", vbQuestion, " Thông báo "
Công trình giao thông thành ph _khóa 54ố Page 13
2014
txtb.SetFocus
Exit Sub
End If
If Trim(txtc.Text) = "" Then
MsgBox " Bạn phải nhập số liệu cho c", vbQuestion, " Thông báo "
txtc.SetFocus
Exit Sub
End If
delta = b * b - 4 * a * c
If delta < 0 Then
txtkq.Text = " Phương trình vô nghiệm"
End If
If delta = 0 Then
x = (-b) / (2 * a)
txtkq.Text = "Phương trình có nghiệm kép x=" & CStr(x)
End If
If delta > 0 Then
X1 = (-b + (delta) ^ 0.5) / (2 * a)
X2 = (-b - (delta) ^ 0.5) / (2 * a)
txtkq.Text = "Phương trình có 2 nghiệm x1=" & CStr(X1) + " ,x2=" & CStr(X2)
End If
End Sub
Private Sub cmdlamlai_Click()
txta.Text = " "
txtc.Text = " "
Công trình giao thông thành ph _khóa 54ố Page 14
2014
txtb.Text = " "
txtkq.Text = " "
txta.SetFocus
End SubPrivate Sub cmdthoat_Click()
If MsgBox(" Bạn có muốn thoát chương trình?", vbYesNo + vbQuestion, " Thông báo ") = vbYes Then
End
End If
End Sub
Private Sub txta_KeyPress(KeyAscii As Integer)
If ((KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 46) And KeyAscii <> 8 Then
KeyAscii = 0
End If
End Sub
Private Sub txtb_KeyPress(KeyAscii As Integer)
If ((KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 46) And KeyAscii <> 8 Then
KeyAscii = 0
End If
End Sub
Private Sub txtc_KeyPress(KeyAscii As Integer)
If ((KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 46) And KeyAscii <> 8 Then
KeyAscii = 0
End If
End Sub
Bài 7: Hệ phương trình bậc nhất 2 ẩn
Công trình giao thông thành ph _khóa 54ố Page 15
2014
Private Sub cmdgiai_Click()
Dim a1, a2, b1, b2, c1, c2 As Single
Dim d, dx, dy As Single
a1 = Val(txta1.Text)
a2 = Val(txta2.Text)
b1 = Val(txtb1.Text)
b2 = Val(txtb2.Text)
c1 = Val(txtc1.Text)
c2 = Val(txtc2.Text)
If Trim(txta1.Text) = "" Then
MsgBox " Bạn phải nhập số liệu cho a1", vbOKOnly, " Thông báo "
txta1.SetFocus
Exit Sub
End If
If Trim(txta2.Text) = "" Then
MsgBox " Bạn phải nhập số liệu cho a2", vbOKOnly, " Thông báo "
txta2.SetFocus
Exit Sub
End If
If Trim(txtb1.Text) = "" Then
MsgBox " Bạn phải nhập số liệu cho b1", vbOKOnly, " Thông báo "
txtb1.SetFocus
Exit Sub
End If
If Trim(txtb2.Text) = "" Then
Công trình giao thông thành ph _khóa 54ố Page 16
2014
MsgBox " Bạn phải nhập số liệu cho b2", vbOKOnly, " Thông báo "
txtb2.SetFocus
Exit Sub
End If
If Trim(txtc1.Text) = "" Then
MsgBox " Bạn phải nhập số liệu cho c1", vbOKOnly, " Thông báo "
txtc1.SetFocus
Exit Sub
End If
If Trim(txtc2.Text) = "" Then
MsgBox " Bạn phải nhập số liệu cho c2", vbOKOnly, " Thông báo "
txtc2.SetFocus
Exit Sub
End If
d = a1 * b2 - a2 * 1
dx = c1 * b2 - c2 * b1
dy = a1 * c2 - a2 * c1
If d = 0 Then
If (dx <> 0) Or (dy <> 0) Then
lblx.Caption = "Hệ vô nghiệm"
Else
lblx.Caption = "Hệ vô số nghiệm"
End If
End If
If (d <> 0) Then
Công trình giao thông thành ph _khóa 54ố Page 17
2014
x = dx / d
y = dy / d
lblx.Caption = "Hệ có nghiệm x=" & CStr(x)
lbly.Caption = "y=" & CStr(y)
End If
End Sub
Private Sub cmdlamlai_Click()
txta1.Text = " "
txta2.Text = " "
txtb1.Text = " "
txtb2.Text = " "
txtc1.Text = " "
txtc2.Text = " "
lblx.Caption = " "
lbly.Caption = " "
txta1.SetFocus
End Sub
Private Sub cmdthoat_Click()
If MsgBox("Bạn có muốn thoát chương trình?", vbYesNo + vbQuestion, " Thông báo ") = vbYes Then
End
End If
End Sub
Private Sub txta1_KeyPress(KeyAscii As Integer)
If ((KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 46) And KeyAscii <> 8 Then
KeyAscii = 0
Công trình giao thông thành ph _khóa 54ố Page 18
2014
End If
End Sub
Private Sub txta2_KeyPress(KeyAscii As Integer)
If ((KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 46) And KeyAscii <> 8 Then
KeyAscii = 0
End If
End Sub
Private Sub txtb1_KeyPress(KeyAscii As Integer)
If ((KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 46) And KeyAscii <> 8 Then
KeyAscii = 0
End If
End Sub
Private Sub txtb2_KeyPress(KeyAscii As Integer)
If ((KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 46) And KeyAscii <> 8 Then
KeyAscii = 0
End If
End Sub
Private Sub txtc1_KeyPress(KeyAscii As Integer)
If ((KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 46) And KeyAscii <> 8 Then
KeyAscii = 0
End If
End Sub
Private Sub txtc2_KeyPress(KeyAscii As Integer)
If ((KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 46) And KeyAscii <> 8 Then
KeyAscii = 0
Công trình giao thông thành ph _khóa 54ố Page 19
2014
End If
End Sub
Bài 8: Chương trình tính số ngày trong tháng
Private Sub cmdketqua_Click()
Dim thang, nam, songay As Integer
thang = Val(txtthang.Text)
nam = Val(txtnam.Text)
If Trim(txtthang.Text) = "" Then
MsgBox " Bạn chưa nhập tháng hãy nhập vào", vbOKOnly, " Thông báo "
txtthang.SetFocus
Exit Sub
End If
If (thang < 1 Or thang > 12) Then
MsgBox " Bạn phải nhập tháng trong khoảng [1->12]", vbOKOnly, " Thông báo "
txtthang.SetFocus
Exit Sub
End If
If Trim(txtnam.Text) = "" Then
MsgBox " Bạn chưa nhập năm hãy nhập vào", vbOKOnly, " Thông báo "
txtnam.SetFocus
Exit Sub
End If
If Len(Trim(txtnam.Text)) <> 4 Then
MsgBox "Bạn phải nhập năm có 4 số", vbOKOnly, " Thông báo "
Công trình giao thông thành ph _khóa 54ố Page 20
2014
txtnam.SetFocus
Exit Sub
End If
Select Case thang
Case 1, 3, 5, 7, 8, 10, 12:
songay = 31
Case 2:
If (nam Mod 4 = 0 And nam Mod 100 <> 0) Then
songay = 29
Else
songay = 28
End If
Case Else
songay = 30
End Select
lblketqua.Caption = "Tháng " & txtthang.Text & " năm " & txtnam.Text & " có " & CStr(songay) & " ngày "
End Sub
Private Sub cmdlamlai_Click()
txtthang.Text = " "
txtnam.Text = " "
lblketqua.Caption = " "
txtthang.SetFocus
End Sub
Private Sub cmdthoat_Click()
If MsgBox("Bạn có muốn thoát chương trình?", vbYesNo + vbQuestion, " Thông báo ") = vbYes Then
Công trình giao thông thành ph _khóa 54ố Page 21
2014
End
End If
End Sub
Private Sub txtnam_KeyPress(KeyAscii As Integer)
If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 8 Then
KeyAscii = 0
End If
End Sub
Private Sub txtthang_KeyPress(KeyAscii As Integer)
If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 8 Then
KeyAscii = 0
End If
End Sub
Bài 9: Program of FontPrivate Sub chkbold_Click()
If chkbold.Value = 1 Then
txta.FontBold = True
Else
txta.FontBold = False
End If
End Sub
Private Sub chkitalic_Click()
If chkitalic.Value = 1 Then
txta.FontItalic = True
Công trình giao thông thành ph _khóa 54ố Page 22
2014
Else
txta.FontItalic = False
End If
End Sub
Private Sub chkstrikeout_Click()
If chkstrikeout.Value = 1 Then
txta.FontStrikethru = True
Else
txta.FontStrikethru = False
End If
End Sub
Private Sub chkunderline_Click()
If chkunderline.Value = 1 Then
txta.FontUnderline = True
Else
txta.FontUnderline = False
End If
End Sub
Private Sub cmdlamlai_Click()
txta.FontName = ".VnHelvetIns"
txta.ForeColor = vbBlack
txta.FontBold = False
txta.FontItalic = False
txta.FontStrikethru = False
txta.FontUnderline = False
Công trình giao thông thành ph _khóa 54ố Page 23
2014
chkbold.Value = 0
chkitalic.Value = 0
chkstrikeout.Value = 0
chkunderline.Value = 0
End Sub
Private Sub cmdthoat_Click()
If MsgBox("Bạn có muốn thoát chương trình?", vbYesNo + vbQuestion, " Thông báo ") = vbYes Then
End
End If
End Sub
Private Sub optblue_Click()
If optblue.Value = True Then
txta.ForeColor = vbBlue
End If
End Sub
Private Sub optgreen_Click()
If optgreen.Value = True Then
txta.ForeColor = vbGreen
End If
End Sub
Private Sub optmagenta_Click()
If optmagenta.Value = True Then
txta.ForeColor = vbMagenta
End If
End Sub
Công trình giao thông thành ph _khóa 54ố Page 24
2014
Private Sub optred_Click()
If optred.Value = True Then
txta.ForeColor = vbRed
End If
End Sub
Private Sub opttahoma_Click()
If opttahoma.Value = True Then
txta.FontName = "tahoma"
End If
End Sub
Private Sub optvntime_Click()
If optvntime.Value = True Then
txta.FontName = ".vntime"
End If
End Sub
Private Sub optvnuniverse_Click()
If optvnuniverse.Value = True Then
txta.FontName = ".vnuniverse"
End If
End Sub
Private Sub optvnvogue_Click()
If optvnvogue.Value = True Then
txta.FontName = ".vnvogue"
End If
End Sub
Công trình giao thông thành ph _khóa 54ố Page 25
2014
Bài 10: Chương trình minh họa List BoxPrivate Sub cmdthem_Click()
If Trim(txttenngoaingu.Text) = "" Then
MsgBox "Bạn phải nhập tên ngoại ngữ", vbOKOnly, " Thông báo "
txttenngoaingu.SetFocus
Exit Sub
End If
Dim kt As Boolean
kt = False
Dim i As Integer
i = 0
Do While Not kt And i < lstdanhsachngoaingu.ListCount
If Trim(txttenngoaingu.Text) = lstdanhsachngoaingu.List(i) Then kt = True
i = i + 1
Loop
If kt = True Then
MsgBox "Tên ngoại ngữ + txttenngoaingu.Text + " đã có , hãy nhập lại", vbOKOnly, " Thông báo "
txttenngoaingu.SetFocus
Else
lstdanhsachngoaingu.AddItem (txttenngoaingu.Text)
txttenngoaingu.Text = " "
txttenngoaingu.SetFocus
End If
End Sub
Công trình giao thông thành ph _khóa 54ố Page 26
2014
Private Sub cmdthoat_Click()
If MsgBox("Bạn có muốn thoát chương trình?", vbYesNo + vbQuestion, " Thông báo ") = vbYes Then
End
End If
End Sub
Private Sub cmdtim_Click()
If Trim(txttenngoaingu.Text) = "" Then
MsgBox " Bạn phải nhập tên ngoại ngữ", vbOKOnly, " Thông báo "
txttenngoaingu.SetFocus
Exit Sub
End If
Dim kt As Boolean
kt = False
Dim i As Integer
i = 0
Do While Not kt And i < lstdanhsachngoaingu.ListCount
If Trim(txttenngoaingu.Text) = lstdanhsachngoaingu.List(i) Then kt = True
i = i + 1
Loop
If kt = True Then
MsgBox "Ngoại ngữ + txttenngoaingu.Text + " đã có", vbOKOnly, " Thông báo "
Else
MsgBox "Ngoại ngữ" + txttenngoaingu.Text + " chưa có", vbOKOnly, " Thông báo "
End If
End Sub
Công trình giao thông thành ph _khóa 54ố Page 27
2014
Private Sub cmdxoa_Click()
If lstdanhsachngoaingu.ListIndex >= 0 Then
If MsgBox("Bạn có muốn xóa ngoại ngữ" + lstdanhsachngoaingu.Text + " ", vbYesNo + vbQuestion, " Thông báo ") = vbYes Then
lstdanhsachngoaingu.RemoveItem (lstdanhsachngoaingu.ListIndex)
End If
cmdxoa.Enabled = False
End If
End Sub
Private Sub lstdanhsachngoaingu_Click()
cmdxoa.Enabled = True
End Sub
Bài 11: Mua hàng trực tuyếnPrivate Sub cmddongy_Click()
Dim s As String
Dim i As Integer
If Trim(txthoten.Text) = "" Then
MsgBox "Bạn chưa nhập họ tên hãy nhập vào", vbOKOnly, " Thông báo "
txthoten.SetFocus
Exit Sub
End If
If Trim(txtdienthoai.Text) = "" Then
MsgBox "Bạn chưa nhập số điện thoại hãy nhập vào", vbOKOnly, " Thông báo "
txthoten.SetFocus
Exit Sub
Công trình giao thông thành ph _khóa 54ố Page 28
2014
End If
s = "Họ tên:" + txthoten.Text + vbCrLf
s = s + "Điên thoại:" + txtdienthoai.Text + vbCrLf
s = s + "Danh sách hàng đặt mua:" + vbCrLf
For i = 0 To lsthangdat.ListCount - 1
s = s + lsthangdat.List(i) + vbCrLf
Next i
s = s + "Hình thức thanh toán:"
If opttienmat.Value = True Then s = s + "Tiền mặt" + vbCrLf
If optsec.Value = True Then s = s + "sec" + vbCrLf
If optthetindung.Value = True Then s = s + " Thẻ tín dụng" + vbCrLf
s = s + "hinh thuc lien lac: "
If chkdienthoai.Value = 1 Then s = s + "Điện thoại"
If chkfax.Value = 1 Then s = s + "Fax"
If chkemail.Value = 1 Then s = s + "Email"
MsgBox s, vbExclamation, "Hóa đơn"
End Sub
Private Sub cmdthoat_Click()
If MsgBox("Bạn có muốn thoát chương trình?", vbYesNo + vbQuestion, " Thông báo ") = vbYes Then
End
End If
End Sub
Private Sub Form_Load()
lstmathang.AddItem "Kỹ thuật lập trình C#"
lstmathang.AddItem "Tự học Visual C# trong 21 ngày"
Công trình giao thông thành ph _khóa 54ố Page 29
2014
lstmathang.AddItem ".NET toàn tập-tập 1"
lstmathang.AddItem ".NET toàn tập-tập 2"
lstmathang.AddItem ".NET toàn tập-tập 3"
lstmathang.AddItem ".NET toàn tập-tập 4"
lstmathang.AddItem ".NET toàn tập-tập 5"
lstmathang.AddItem "Tin học căn bản SQL server"
lstmathang.AddItem "Cơ bản về XMl"
lstmathang.AddItem "Phân tích thiết kế hệ thống"
lstmathang.AddItem "Sử dụng Dreamwever 8.0"
lstmathang.AddItem " Đến với word 2003"
End Sub
Private Sub lsthangdat_DblClick()
If lsthangdat.ListIndex >= 0 Then
If MsgBox("Bạn có muốn xóa bỏ măt hàng này không?", vbYesNo + vbQuestion, " Thông báo ") = vbYes Then
lsthangdat.RemoveItem (lsthangdat.ListIndex)
End If
End If
End Sub
Private Sub lstmathang_DblClick()
Dim s As String
Dim kt As Boolean
Dim i As Integer
If lstmathang.ListIndex >= 0 Then
s = lstmathang.List(lstmathang.ListIndex)
Công trình giao thông thành ph _khóa 54ố Page 30
2014
kt = False
i = 0
Do While Not kt And i < lsthangdat.ListCount
If lsthangdat.List(i) = s Then kt = True
i = i + 1
Loop
If kt = True Then
MsgBox "Bạn đã chọn mua mặt hàng:" + s + "roi", vbOKOnly + vbExclamation, " Thông báo "
Else
lsthangdat.AddItem (s)
End If
End If
End Sub
Private Sub txtdienthoai_KeyPress(KeyAscii As Integer)
If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 8 Then
KeyAscii = 0
End If
End Sub
Bài 12: Chương trình tính tổngPrivate Sub cmdlamlai_Click()
txta.Text = ""
txtb.Text = ""
lbltong.Caption = ""
txta.SetFocus
Công trình giao thông thành ph _khóa 54ố Page 31
2014
End Sub
Private Sub cmdthoat_Click()
If MsgBox("Bạn có muốn thoát chương trình?", vbYesNo + vbQuestion, " Thông báo ") = vbYes Then
End
End If
End Sub
Private Sub cmdtinhtong_Click()
Dim a, b As Integer
Dim s, i As Integer
a = Val(txta.Text)
b = Val(txtb.Text)
If Trim(txta.Text) = "" Then
MsgBox "Bạn phải nhập a hãy nhập vào", vbOKOnly, " Thông báo "
txta.SetFocus
Exit Sub
End If
If Trim(txtb.Text) = "" Then
MsgBox "Bạn phải nhập b hãy nhập vào", vbOKOnly, " Thông báo "
txtb.SetFocus
Exit Sub
End If
s = 0
If a < b Then
For i = a To b
s = s + i
Công trình giao thông thành ph _khóa 54ố Page 32
2014
Next i
Else
For i = b To a
s = s + i
Next i
End If
lbltong.Caption = "Tổng từ " & txta.Text & " đến " & txtb.Text & " là : " & CStr(s)
End Sub
Private Sub txta_KeyPress(KeyAscii As Integer)
If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 8 Then
KeyAscii = 0
End If
End Sub
Private Sub txtb_KeyPress(KeyAscii As Integer)
If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 8 Then
KeyAscii = 0
End If
End Sub
Bài 13: Tính giá trị biểu thức 1Private Sub cmdlamlai_Click()
txtx.Text = ""
txtn.Text = ""
txtketqua.Text = ""
txtx.SetFocus
Công trình giao thông thành ph _khóa 54ố Page 33
2014
End Sub
Private Sub cmdthoat_Click()
If MsgBox("Bạn có muốn thoát chương trình?", vbYesNo + vbQuestion, " Thông báo ") = vbYes Then
End
End If
End Sub
Private Sub cmdtinh_Click()
Dim x, t As Single
Dim i, n As Integer
x = Val(txtx.Text)
n = Val(txtn.Text)
If Trim(txtx.Text) = "" Then
MsgBox "Bạn phải nhập giá trị cho x", vbOKOnly, " Thông báo "
txtx.SetFocus
Exit Sub
End If
If Trim(txtn.Text) = "" Then
MsgBox "Bạn phải nhập giá trị cho n", vbOKOnly, " Thông báo "
txtn.SetFocus
Exit Sub
End If
t = 2014
For i = 1 To n
t = t + (x ^ i)
Next i
Công trình giao thông thành ph _khóa 54ố Page 34
2014
txtketqua.Text = CStr(t)
End Sub
Private Sub txtn_KeyPress(KeyAscii As Integer)
If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 8 Then
KeyAscii = 0
End If
End Sub
Private Sub txtx_KeyPress(KeyAscii As Integer)
If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 46 And KeyAscii <> 8 Then
KeyAscii = 0
End If
End Sub
Bài 14: Tính giá trị biểu thức 2Private Sub cmdlamlai_Click()
txtx.Text = ""
txtn.Text = ""
txtketqua.Text = ""
txtx.SetFocus
End Sub
Private Sub cmdthoat_Click()
If MsgBox("Bạn có muốn thoát chương trình?", vbYesNo + vbQuestion, " Thông báo ") = vbYes Then
End
End If
End Sub
Công trình giao thông thành ph _khóa 54ố Page 35
2014
Private Sub cmdtinh_Click()
Dim x, s As Single
Dim n, i As Integer
x = Val(txtx.Text)
n = Val(txtn.Text)
If Trim(txtx.Text) = "" Then
MsgBox "Bạn phải nhập giá trị cho x", vbOKOnly, " Thông báo "
txtx.SetFocus
Exit Sub
End If
If Trim(txtn.Text) = "" Then
MsgBox "Bạn phải nhập giá trị cho n", vbOKOnly, " Thông báo "
txtn.SetFocus
Exit Sub
End If
s = e ^ x
For i = 1 To n
s = s + (x ^ i) / (i + 1)
Next i
txtketqua.Text = CStr(Sqr(s))
End Sub
Private Sub txtn_KeyPress(KeyAscii As Integer)
If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 8 Then KeyAscii = 0
End Sub
Private Sub txtx_KeyPress(KeyAscii As Integer)
Công trình giao thông thành ph _khóa 54ố Page 36
2014
If (KeyAscii < 48 Or KeyAscii > 57) And eyascii <> 46 And KeyAscii <> 8 Then KeyAscii = 0
End Sub
Bài 15: Kiểm tra số nguyên tố
Private Sub cmdkiemtra_Click()
Dim n, i As Integer
Dim kt As Boolean
kt = True
n = Val(txtnhapso.Text)
If Trim(txtnhapso.Text) = "" Then
MsgBox "Bạn chưa nhập số hãy nhập vào", vbOKOnly, " Thông báo "
txtnhapso.SetFocus
Exit Sub
End If
If n > 3 Then
i = 2
Do While i <= Sqr(n) And kt = True
If n Mod i = 0 Then kt = False
i = i + 1
Loop
End If
If kt = True Then
lblketqua.Caption = txtnhapso.Text & " là số nguyên tố"
Else
lblketqua.Caption = txtnhapso.Text & " không là số nguyên tố"
Công trình giao thông thành ph _khóa 54ố Page 37
2014
End If
End Sub
Private Sub cmdlamlai_Click()
txtnhapso.Text = ""
lblketqua.Caption = ""
txtnhapso.SetFocus
End Sub
Private Sub cmdthoat_Click()
If MsgBox("Bạn có muốn thoát chương trình?", vbYesNo + vbQuestion, " Thông báo ") = vbYes Then
End
End if
End Sub
Private Sub txtnhapso_KeyPress(KeyAscii As Integer)
If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 8 Then
KeyAscii = 0
End If
End Sub
Bài 16: Dãy sốPrivate Sub cmdtemvaodanhsach_Click()
If Trim(txtnhapso.Text) = "" Then
MsgBox "Bạn chưa nhập số hãy nhập vào", vbOKOnly, " Thông báo "
txtnhapso.SetFocus
Exit Sub
End If
Công trình giao thông thành ph _khóa 54ố Page 38
2014
lstdayso.AddItem (txtnhapso.Text)
txtnhapso.Text = ""
txtnhapso.SetFocus
cmdxoakhoidanhsach.Enabled = True
cmdtinhtong.Enabled = True
cmdtimmax.Enabled = True
cmdtimmin.Enabled = True
End Sub
Private Sub cmdtimmax_Click()
Dim max, x As Single
Dim i As Integer
max = CSng(lstdayso.List(0))
For i = 0 To lstdayso.ListCount - 1
x = Val(lstdayso.List(i))
If max < x Then max = x
Next i
lblmax.Caption = "Max= " & CStr(max)
End Sub
Private Sub cmdtimmin_Click()
Dim min, x As Single
Dim i As Integer
min = CSng(lstdayso.List(0))
For i = 0 To lstdayso.ListCount - 1
x = Val(lstdayso.List(i))
If min > x Then min = x
Công trình giao thông thành ph _khóa 54ố Page 39
2014
Next i
lblmin.Caption = "Min= " & CStr(min)
End Sub
Private Sub cmdtinhtong_Click()
Dim s As Single
Dim i As Integer
s = CSng(lstdayso.List(0))
s = 0
For i = 0 To lstdayso.ListCount - 1
s = s + CInt(lstdayso.List(i))
Next i
lbltong.Caption = "Tổng= " & CStr(s)
End Sub
Private Sub cmdxoakhoidanhsach_Click()
If lstdayso.ListIndex < 0 Then
MsgBox "Bạn cần chọn số để xóa", vbOKOnly, " Thông báo "
Exit Sub
Else
If MsgBox("Bạn có muốn xóa số này?", vbYesNo + vbQuestion, " Thông báo ") = vbYes Then
lstdayso.RemoveItem (lstdayso.ListIndex)
lbltong.Caption = ""
lblmax.Caption = ""
lblmin.Caption = ""
End If
End If
Công trình giao thông thành ph _khóa 54ố Page 40
2014
If lstdayso.ListCount = 0 Then
cmdxoakhoidanhsach.Enabled = False
cmdtinhtong.Enabled = False
cmdtimmax.Enabled = False
cmdtimmin.Enabled = False
End If
End Sub
Private Sub txtnhapso_KeyPress(KeyAscii As Integer)
If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 46 And KeyAscii <> 8 Then
KeyAscii = 0
End If
End Sub
Công trình giao thông thành ph _khóa 54ố Page 41