Bài 1. viết chương trình lời chào hello!!!
Private Sub cmdCHAO_Click()
txtDisplay.Text = "chao ban!!!!!"
End Sub
Private Sub cmdTHOAT_Click()
End
End Sub
Private Sub cmdXOA_Click()
txtDisplay.Text = ""
End Sub
Bài 2
Option Explicit
Private Sub cmdCAPNHAT_Click()
Dim GT As String * 5
Dim HOTEN As String * 30
Dim NAMSINH As Integer
Dim TUOI As Single
Dim NAM As Date
'gán giá tri vào bien
HOTEN = txtHOTEN
NAMSINH = Val(txtNAMSINH)
NAM = Year(Date)
TUOI = NAM - txtNAMSINH
If chkGT = 1 Then
If TUOI <= 25 Then
GT = " ANH "
Else
If TUOI > 50 Then
GT = " ONG "
Else
GT = " CHU "
End If
End If
Else
If TUOI <= 25 Then
GT = " CHI "
Else
If TUOI > 50 Then
GT = " BA "
Else
GT = " CO "
End If
End If
End If
lblCHAO = "Chào" & GT & HOTEN & Chr(13) & "Rất vui được đón" & GT &
"đến với cửa hàng của chúng tôi"
End Sub
Private Sub cmdTHOAT_Click()
End
End Sub
Bài 3
Private Sub CmdASCII_Click()
Dim i As Integer
For i = 0 To 255
Text2 = Text2 & vbCrLf & i & " >" & Chr(i)
Next i
End Sub
Private Sub CmdTHOAT_Click()
End
End Sub
Private Sub CmdTINH_Click()
LblKQ = Text1 & " >" & Chr(Text1)
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
Text1.SetFocus
Text1.SelStart = 0
Text1.SelLength = Len(Text1)
LblKQ = "ki tu bieu dien cua ma ASCII" & Text1 & " la: " & Chr(Val(Text1))
End If
End Sub
Bài 4
Private Sub CMDTHOAT_Click()
If MsgBox("Ban That Su Muon Thoat??", 4 + 32, "tb") = 6 Then
End
End If
End Sub
Private Sub CMDXEM_Click()
Dim I As Byte
Dim A As String
TXTNHAP.SetFocus
If I < 1 And I > 9 Then
TXT2.Text = "KHONG CO KET QUA!!!"
TXTNHAP.SetFocus
TXTNHAP.SelStart = 0
TXTNHAP.SelLength = Len(TXTNHAP)
Else
For I = 1 To 10
A = TXTNHAP.Text * I
TXT2.Text = TXT2 & vbCrLf & TXTNHAP & " X " & I & " = " & A & vbCrLf
TXTNHAP.SetFocus
TXTNHAP.SelStart = 0
TXTNHAP.SelLength = Len(TXTNHAP)
TXT2.SelStart = 0
TXT2.SelLength = Len(TXT2)
Next I
End If
End Sub
Bài 5
Private Sub cmdthoat_Click(Index As Integer)
End
End Sub
Private Sub cmdtinh_Click(Index As Integer)
Dim N, I, GT, X, f As Integer
Dim S, T, S2, S3, S4 As Single
N = Val(txt1)
S = 0
For I = 1 To N
S = S + 1 / I
S = Format(S, "##,##.00")
txt3 = S
Next I
GT = 1
T = 0
For I = 1 To N
GT = GT * I
T = T + 1 / GT
T = Format(T, "##,##.00")
txt4 = T
Next I
GT = 1
X = Val(txt2)
S2 = 1
For I = 1 To N
G = G * I
S2 = S2 + X * I / GT
txt5 = S2
Next I
S4 = 0
For I = 1 To N
S4 = S4 + Sin(X) + Sin(X) * I
txt7 = S4
Next I
End Sub
Bài 6
Option Explicit
Dim i As Byte
Private Sub Form_Load()
i = 0
End Sub
Private Sub Timer1_Timer()
If i < 5 Then
Me.BackColor = QBColor(Int(Rnd * 16))
Image1.Picture = Image2(i).Picture
i = i + 1
Else
i = 0
End If
End Sub
Bài 7
Private Sub cmd_thoat_Click()
End
End Sub
Private Sub cmd1_Click()
Dim X, Y, CANH, CANH2 As Single
X = Val(txtX)
Y = Val(txtY)
CANH = Val(txtCANH)
CANH2 = Val(txtCANH2)
If (txtCANH2.Visible = True) Then
Shape1.Left = X
Shape1.Top = Y
Shape1.Width = CANH2
Shape1.Height = CANH
Else
Shape1.Left = X
Shape1.Top = Y
Shape1.Width = CANH
Shape1.Height = CANH
End If
End Sub
Private Sub cmd2_Click()
Dim X, Y, CANH As Single
X = Val(txtX)
Y = Val(txtY)
CANH = Val(txtCANH)
CANH2 = Val(txtCANH2)
If (txtCANH2.Visible = True) Then
Shape1.Left = X - CANH / 2
Shape1.Top = Y - CANH2 / 2
Shape1.Width = CANH2
Shape1.Height = CANH
Else
Shape1.Left = X - CANH / 2
Shape1.Top = Y - CANH / 2
Shape1.Width = CANH
Shape1.Height = CANH
End If
End Sub
Private Sub form_load()
ScaleMode = 7
Picture1.ScaleMode = 7
End Sub
Private Sub Option2_Click(Index As Integer)
Shape1.Shape = Index
Select Case Index
Case 0, 2
Label4.Visible = True
txtCANH2.Visible = True
Case 1, 3
Label4.Visible = False
txtCANH2.Visible = False
End Select
End Sub
Bài 8
timer : intetval: 500
Private Sub Timer1_Timer()
Label1 = Time()
End Sub
Bài 9
Option Explicit
Private Sub cmdTINH_Click()
Dim CANH1, CANH2, DT, CV As Single
CANH1 = Val(txtCanh1)
CANH2 = Val(txtCanh2)
CV = (CANH1 + CANH2) * 2
DT = CANH1 * CANH2
lblKQ = "dien tich= " & DT & Chr(13) & "chu vi= " & CV
'chuyen focus ve txtCanh1
txtCanh1.SetFocus
'choïn nguyen chuoi so trong txtCanh1
txtCanh1.SelStart = 0
txtCanh1.SelLength = Len(txtCanh1)
End Sub
Private Sub txtCanh1_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
txtCanh2.SetFocus
'choïn nguyen chuoi so trong txtCanh2
txtCanh2.SelStart = 0
txtCanh2.SelLength = Len(txtCanh2)
End If
End Sub
Private Sub txtCanh2_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
' goi thuc hien thu tuc cmdTINH-click
cmdTINH_Click
End If
End Sub
Bài 10
Private Sub cmdthoat_Click()
End
End Sub
Private Sub txtsodau_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
txtsosau.SetFocus
txtsosau.SelStart = 0
txtsosau.SelLength = Len(txtsosau)
End If
End Sub
Private Sub txtsosau_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
cmdtinh_Click
End If
End Sub
Private Sub cmdtinh_Click()
Dim sothu1, sothu2, tong, hieu, tich, thuong As Single
sothu1 = Val(txtsodau)
sothu2 = Val(txtsosau)
tong = sothu1 + sothu2
hieu = sothu1 - sothu2
tich = sothu1 * sothu2
thuong = sothu1 / sothu2
lblkq = "tong= " & tong & Chr(13) & "hieu= " & hieu & Chr(13) & "tich= " & tich &
Chr(13) & "thuong= " & thuong
txtsodau.SetFocus
txtsodau.SelStart = 0
txtsodau.SelLength = Len(txtsodau)
End Sub
Bài 11
Private Sub Command1_Click()
lblxuat = txtnhap
End Sub
Bài 12 USCLN
FORM 1
Option Explicit
Dim x As Byte
Dim y As Byte
Public Function USCLN(ByVal A As Integer, ByVal B As Integer)
Dim r As Integer
Do While B <> 0
r = A Mod B
A = B
B = r
Loop
USCLN = A
End Function
Private Sub cmdTINH_Click()
Dim soA As Integer
Dim soB As Integer
soA = Val(txtA)
soB = Val(txtB)
'goùi haứm USCLN
lblKQ = "uoc so chung lon nhat = " & USCLN(soA, soB)
End Sub
Private Sub Command1_Click()
'hieọn form
Form2.Show
'daỏu form
Me.Hide
End Sub
FORM 2
Option Explicit
Private Sub Command1_Click()
Dim soA As Integer
Dim soB As Integer
soA = Val(txtA)
soB = Val(txtB)
lblKQ = "uoc so chung lon nhat = " & Form1.USCLN(soA, soB)
End Sub
Private Sub Command2_Click()
'giải phóng for khỏi bộ nhớ
Unload Me
'hiện form
Form1.Show
End Sub
Bài 13.List
Option Explicit
Private Sub cmdEnd_Click()
End
End Sub
Private Sub cmdSUA_Click()
If txtNAME = "" Then
MsgBox "chua nhap"
txtNAME.SetFocus
Else
lstDS.List(lstDS.ListIndex) = txtNAME
End If
Label3 = lstDS.ListCount
End Sub
Private Sub cmdThem_Click()
If txtNAME = "" Then
MsgBox "chua nhap"
Else
lstDS.AddItem txtNAME
End If
txtNAME.SelStart = 0
txtNAME.SelLength = Len(txtNAME)
txtNAME.SetFocus
Label3 = lstDS.ListCount
End Sub
Private Sub cmdXOA_Click()
Dim i As Byte
i = lstDS.ListIndex
lstDS.RemoveItem lstDS.ListIndex
Label3 = lstDS.ListCount
If i = 0 And lstDS.ListCount = 0 Then
cmdXOA.Enabled = False
txtNAME.Text = ""
Exit Sub
End If
If i = lstDS.ListCount Then i = i - 1
lstDS.Selected(i) = True
End Sub
Private Sub cmdXoaHet_Click()
lstDS.Clear
Label3 = lstDS.ListCount
End Sub
Private Sub Form_Load()
Label3 = lstDS.ListCount
cmdXOA.Enabled = False
End Sub
Private Sub lstDS_Click()
txtNAME.Text = lstDS.Text
cmdXOA.Enabled = True
txtNAME.SetFocus
End Sub
Bài 14. Chạy chữ
Option Explicit
Private Sub Timer1_Timer()
lblTRAI = Right(lblTRAI, Len(lblTRAI) - 1) & Left(lblTRAI, 1)
lblTRAI.ForeColor = QBColor(Int(Rnd * 16))
lblPHAI = Right(lblPHAI, 1) & Left(lblPHAI, Len(lblPHAI) - 1)
lblPHAI.ForeColor = QBColor(Int(Rnd * 16))
End Sub
Private Sub HsbColor_Change(Index As Integer)
'Đặt màu tô của shpSample bằng RGB của ba giá trò trong ba thanh cuộn
shpSample.FillColor = RGB(HsbColor(0).Value, HsbColor(1).Value,
HsbColor(2).Value)
'Đặt nội dung của TextBox tương ứng bằng giá trò của thanh cuộn
txtColor(Index).Text = Trim(Str(HsbColor(Index)))
End Sub
Private Sub txtColor_Change(Index As Integer)
'Đặt giá trò của thanh cuộn tương ứng bằng giá trò của TextBox tương ứng
HsbColor(Index).Value = Val(txtColor(Index).Text)
'Đặt màu tô của shpSample bằng RGB của ba giá trò trong ba thanh cuộn
shpSample.FillColor = RGB(HsbColor(0).Value, HsbColor(1).Value,
HsbColor(2).Value)
End Sub
Bài 15. Điều kiện hình vng và dãy hình vng
Private Sub cmd_cheo_Click()
Dim X, Y, CANH As Single
X = Val(txtX)
Y = Val(txtY)
CANH = Val(txtCANH)
Shape1.Left = X
Shape1.Top = Y
Shape1.Width = CANH
Shape1.Height = CANH
Picture1.Line (X, Y)-(X + CANH, Y + CANH)
Picture1.Line (X + CANH, Y)-(X, Y + CANH)
End Sub
Private Sub cmd_hinhvuong_Click()
Dim X, Y, CANH As Single
X = Val(txtX)
Y = Val(txtY)
CANH = Val(txtCANH)
Shape1.Left = X
Shape1.Top = Y
Shape1.Width = CANH
Shape1.Height = CANH
End Sub
Private Sub cmd_hvuong_n_Click()
Dim N, i, a
CANH = Val(txtCANH)
N = txtN
If N > 1 Then
've day le
For i = 1 To N Step 2
Picture1.Line (i * CANH, 0)-((i + 1) * CANH, CANH), RGB(Int(Rnd * 255),
Int(Rnd * 255), Int(Rnd * 255)), BF
Next i
've day chan
For i = 2 To N Step 2
Picture1.Line (i * CANH, CANH)-((i + 1) * CANH, 2 * CANH), RGB(Int(Rnd *
255), Int(Rnd * 255), Int(Rnd * 255)), BF
Next i
End If
End Sub
Private Sub cmd_ngoai_Click()
Dim X, Y, CANH As Single
X = Val(txtX)
Y = Val(txtY)
CANH = Val(txtCANH)
Shape1.Left = X
Shape1.Top = Y
Shape1.Width = CANH
Shape1.Height = CANH
Shape3.Left = X
Shape3.Top = Y
Shape3.Shape = 3
Shape3.Left = X + CANH / 2 - Sqr(CANH * CANH + CANH * CANH) / 2
Shape3.Top = Y + CANH / 2 - Sqr(CANH * CANH + CANH * CANH) / 2
Shape3.Width = Sqr(CANH * CANH + CANH * CANH)
Shape3.Height = Sqr(CANH * CANH + CANH * CANH)
End Sub
Private Sub cmd_noi_Click()
Dim X, Y, CANH As Single
X = Val(txtX)
Y = Val(txtY)
CANH = Val(txtCANH)
Shape1.Left = X
Shape1.Top = Y
Shape1.Width = CANH
Shape1.Height = CANH
Shape2.Left = X
Shape2.Top = Y
Shape2.Shape = 3
Shape2.Width = CANH
Shape2.Height = CANH
End Sub
Private Sub cmd_thoat_Click()
End
End Sub
Private Sub cmd_vechung_Click()
Dim X, Y, CANH As Single
X = Val(txtX)
Y = Val(txtY)
CANH = Val(txtCANH)
Shape1.Left = X
Shape1.Top = Y
Shape1.Width = CANH
Shape1.Height = CANH
Shape2.Visible = True
Shape2.Left = X
Shape2.Top = Y
Shape2.Shape = 3
Shape2.Width = CANH
Shape2.Height = CANH
Shape3.Visible = True
Shape3.Shape = 3
Shape3.Left = X + CANH / 2 - Sqr(CANH * CANH + CANH * CANH) / 2
Shape3.Top = Y + CANH / 2 - Sqr(CANH * CANH + CANH * CANH) / 2
Shape3.Width = Sqr(CANH * CANH + CANH * CANH)
Shape3.Height = Sqr(CANH * CANH + CANH * CANH)
Picture1.Line (X, Y)-(X + CANH, Y + CANH)
Picture1.Line (X + CANH, Y)-(X, Y + CANH)
End Sub
Private Sub Timer1_Timer()
Shape1.BackColor = QBColor(Int(Rnd * 16))
End Sub
Bài 16. Diện tích, chu vi hcn
Option Explicit
Private Sub cmdTINH_Click()
Dim CANH1, CANH2, DT, CV As Single
CANH1 = Val(txtCanh1)
CANH2 = Val(txtCanh2)
CV = (CANH1 + CANH2) * 2
DT = CANH1 * CANH2
lblKQ = "dien tich= " & DT & Chr(13) & "chu vi= " & CV
'chuyen focus ve txtCanh1
txtCanh1.SetFocus
'choïn nguyen chuoi so trong txtCanh1
txtCanh1.SelStart = 0
txtCanh1.SelLength = Len(txtCanh1)
End Sub
Private Sub txtCanh1_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
txtCanh2.SetFocus
'choïn nguyen chuoi so trong txtCanh2
txtCanh2.SelStart = 0
txtCanh2.SelLength = Len(txtCanh2)
End If
End Sub
Private Sub txtCanh2_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
' goi thuc hien thu tuc cmdTINH-click
cmdTINH_Click
End If
End Sub
Bài 17
Private Sub cmdco_GotFocus()
cmdco.SetFocus
End Sub
Private Sub cmdco_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As
Single)
cmdco.Move Rnd * (Me.ScaleWidth - cmdco.Width), Rnd * (Me.ScaleHeight -
cmdco.Height)
End Sub
Private Sub cmdko_Click()
'If MsgBox("maøy dieân a? maøy cheát laø chaéc lio6n khakha!!!", 32 + 4, "ngu ne!!") =
6 Then
'End If
lblkq = "may dien a? may chet la cai chac!!! may chet tao cung 500d khkhkhaaaaaaaa!!!"
End Sub