Laihan 1.
Form Gaji Pegawai
Code Program :
Private Sub Command1_Click()
If Combo1 = "I" Then
Text1 = 750000
If Combo2 = "Nikah" Then
Text2 = 150000
ElseIf Combo2 = "Belum" Then
Text2 = 75000
End If
Text3 = Val(0.1 * Text1)
Text4 = Val(Text1) + Val(Text2)
Text5 = Val(Text4) - Val(Text3)
ElseIf Combo1 = "II" Then
Text1 = 1000000
If Combo2 = "Nikah" Then
Text2 = 250000
ElseIf Combo2 = "Belum" Then
Text2 = 125000
End If
Text3 = Val(0.12 * Text1)
Text4 = Val(Text1) + Val(Text2)
Text5 = Val(Text4) - Val(Text3)
ElseIf Combo1 = "III" Then
Text1 = 1750000
If Combo2 = "Nikah" Then
Text2 = 450000
ElseIf Combo2 = "Belum" Then
Text2 = 200000
End If
Text3 = Val(0.14 * Text1)
Text4 = Val(Text1) + Val(Text2)
Text5 = Val(Text4) - Val(Text3)
ElseIf Combo1 = "IV" Then
Text1 = 2250000
If Combo2 = "Nikah" Then
Text2 = 650000
ElseIf Combo2 = "Belum" Then
Text2 = 350000
End If
Text3 = Val(0.15 * Text1)
Text4 = Val(Text1) + Val(Text2)
Text5 = Val(Text4) - Val(Text3)
Else
Text1 = "Salah Kode"
Text2 = 0
Text3 = 0
Text4 = 0
Text5 = 0
End If
Text1 = Format(Text1, "Rp ###,###,###")
Text2 = Format(Text2, "Rp ###,###,###")
Text3 = Format(Text3, "Rp ###,###,###")
Text4 = Format(Text4, "Rp ###,###,###")
Text5 = Format(Text5, "Rp ###,###,###")
End Sub
Private Sub Command2_Click()
Text1 = ""
Text2 = ""
Text3 = ""
Text4 = ""
Text5 = ""
Text6 = ""
Text7 = ""
Text1.SetFocus
End Sub
Private Sub Command3_Click()
End
End Sub
Private Sub Form_Load()
Combo1.AddItem "I"
Combo1.AddItem "II"
Combo1.AddItem "III"
Combo1.AddItem "IV"
Combo1.ListIndex = 0
Combo2.AddItem "Nikah"
Combo2.AddItem "Belum"
Combo2.ListIndex = 0
End Sub
Latihan 2. Konversi Suhu
Code Program :
Private Sub chkCel_Click()
If chkCel.Value = vbChecked Then
lblCel.Visible = True
Else
lblCel.Visible = False
End If
End Sub
Private Sub chkFah_Click()
If chkFah.Value = vbChecked Then
lblFah.Visible = True
Else
lblFah.Visible = False
End If
End Sub
Private Sub chkKel_Click()
If chkKel.Value = vbChecked Then
lblKel.Visible = True
Else
lblKel.Visible = False
End If
End Sub
Private Sub chkRan_Click()
If chkRan.Value = vbChecked Then
lblRan.Visible = True
Else
lblRan.Visible = False
End If
End Sub
Private Sub cmdInput_Click()
txtSuhu.Text = ""
lblCel.Visible = False
lblFah.Visible = False
lblKel.Visible = False
lblRan.Visible = False
optCel.Value = False
optFah.Value = False
optKel.Value = False
optRan.Value = False
chkCel.Value = Unchecked
chkFah.Value = Unchecked
chkKel.Value = Unchecked
chkRan.Value = Unchecked
chkCel.Enabled = False
chkFah.Enabled = False
chkKel.Enabled = False
chkRan.Enabled = False
txtSuhu.SetFocus
End Sub
Private Sub cmdSelesai_Click()
If MsgBox("Anda Keluar ?", vbOKCancel + 32, "Pesan") = vbOK Then
Unload Me
End If
End Sub
Private Sub optCel_Click()
chkCel.Value = Unchecked
chkFah.Value = Unchecked
chkKel.Value = Unchecked
chkRan.Value = Unchecked
chkCel.Enabled = False
chkFah.Enabled = True
chkKel.Enabled = True
chkRan.Enabled = True
lblCel.Visible = False
lblFah.Visible = False
lblKel.Visible = False
lblRan.Visible = False
lblFah.Caption = 9 / 5 * Val(txtSuhu.Text) + 32
lblKel.Caption = Val(txtSuhu.Text) + 273
lblRan.Caption = (9 / 5 * Val(txtSuhu.Text) + 32) + 460
End Sub
Private Sub optFah_Click()
chkCel.Value = Unchecked
chkFah.Value = Unchecked
chkKel.Value = Unchecked
chkRan.Value = Unchecked
chkCel.Enabled = True
chkFah.Enabled = False
chkKel.Enabled = True
chkRan.Enabled = True
lblCel.Visible = False
lblFah.Visible = False
lblKel.Visible = False
lblRan.Visible = False
lblFah.Caption = ""
lblCel.Caption = 5 / 9 * (Val(txtSuhu.Text) - 32)
lblKel.Caption = 5 / 9 * (Val(txtSuhu.Text) - 32) + 273
lblRan.Caption = Val(txtSuhu.Text) + 460
End Sub
Private Sub optKel_Click()
chkCel.Value = Unchecked
chkFah.Value = Unchecked
chkKel.Value = Unchecked
chkRan.Value = Unchecked
chkCel.Enabled = True
chkFah.Enabled = True
chkKel.Enabled = False
chkRan.Enabled = True
lblCel.Visible = False
lblFah.Visible = False
lblKel.Visible = False
lblRan.Visible = False
lblKel.Caption = ""
lblCel.Caption = Val(txtSuhu.Text) - 273
lblFah.Caption = 9 / 5 * (Val(txtSuhu.Text) - 273) + 32
lblRan.Caption = 9 / 5 * (Val(txtSuhu.Text) - 273) + 460
End Sub
Private Sub optRan_Click()
chkCel.Value = Unchecked
chkFah.Value = Unchecked
chkKel.Value = Unchecked
chkRan.Value = Unchecked
chkCel.Enabled = True
chkFah.Enabled = True
chkKel.Enabled = True
chkRan.Enabled = False
lblCel.Visible = False
lblFah.Visible = False
lblKel.Visible = False
lblRan.Visible = False
lblRan.Caption = ""
lblCel.Caption = 5 / 9 * (Val(txtSuhu.Text) - 492)
lblFah.Caption = Val(txtSuhu.Text) - 460
lblKel.Caption = 5 / 9 * (Val(txtSuhu.Text) - 492) + 273
End Sub
Latihan 3 PDAM
Code Program :
Dim kanan As Boolean
Private Sub Command1_Click()
If Combo1 = "1" Then
Text4 = 250
Text5 = Val(Text3) - Val(Text2)
Text6 = Val(Text5 * Text4)
Text7 = Val(0.1 * Text6)
Text8 = Val(Text6) + Val(Text7)
ElseIf Combo1 = "2" Then
Text4 = 950
Text5 = Val(Text3) - Val(Text2)
Text6 = Val(Text5 * Text4)
Text7 = Val(0.1 * Text6)
Text8 = Val(Text6) + Val(Text7)
ElseIf Combo1 = "3" Then
Text4 = 1500
Text5 = Val(Text3) - Val(Text2)
Text6 = Val(Text5 * Text4)
Text7 = Val(0.1 * Text6)
Text8 = Val(Text6) + Val(Text7)
ElseIf Combo1 = "4" Then
Text4 = 1750
Text5 = Val(Text3) - Val(Text2)
Text6 = Val(Text5 * Text4)
Text7 = Val(0.1 * Text6)
Text8 = Val(Text6) + Val(Text7)
Else
Text1 = "Salah Kode"
Text2 = 0
Text3 = 0
Text4 = 0
Text5 = 0
Text6 = 0
Text7 = 0
Text8 = 0
End If
Text6 = Format(Text6, "Rp ###,###,###")
Text7 = Format(Text7, "Rp ###,###,###")
Text8 = Format(Text8, "Rp ###,###,###")
End Sub
Private Sub Command2_Click()
Text1 = ""
Text2 = ""
Text3 = ""
Text4 = ""
Text5 = ""
Text6 = ""
Text7 = ""
Text8 = ""
Text1.SetFocus
End Sub
Private Sub Command3_Click()
If MsgBox("Anda Keluar ?", vbOKCancel + 32, "Pesan") = vbOK Then
Unload Me
End If
End Sub
Private Sub Form_Load()
Combo1.AddItem "1"
Combo1.AddItem "2"
Combo1.AddItem "3"
Combo1.AddItem "4"
Label1.FontBold = True
Label1.FontSize = 20
Label1.Left = 240
Timer1.Interval = 100
kanan = True
End Sub
Private Sub Timer1_Timer()
'Jika arah text kekanan
If kanan = True Then
'Menggeser posisi text kekanan
Label1.Left = Label1.Left + 100
'Menggeser posisi akhir text
'Pada batas kanan
If Label1.Left > 2000 Then
'ubah arah gerak kir
kanan = False
End If
Else
'jika arah gerak text kekiri
'Menggeser posisi text kekiri
Label1.Left = Label1.Left - 100
'Menggeser posisi akhir text
'pada batas kanan
If Label1.Left < 240 Then
'ubah arah gerak kekanan
kanan = True
End If
End If
End Sub
Latihan 4 Program Kalkulator
Code Program :
'deklarasi variable penampung nilai
Dim nilai1, nilai2 As Integer
Dim hasil As Double
'deklarasi variable utk flag
'(penanda nilai pertama atau kedua)
Dim pertama As Boolean
'jenis operasi
Dim operasi As Integer
Private Sub cmdAngka_Click(Index As Integer)
'mengubah teks rata kanan
TxtLayar.Alignment = 1
'mengecek objek angka
Select Case Index
Case 0
'menampilkan nilai yg dimasukan pada layar
TxtLayar.Text = Trim(TxtLayar.Text) & "1"
Case 1
'menampilkan nilai yg dimasukan pada layar
TxtLayar.Text = Trim(TxtLayar.Text) & "2"
Case 2
'menampilkan nilai yg dimasukan pada layar
TxtLayar.Text = Trim(TxtLayar.Text) & "3"
Case 3
'menampilkan nilai yg dimasukan pada layar
TxtLayar.Text = Trim(TxtLayar.Text) & "4"
Case 4
'menampilkan nilai yg dimasukan pada layar
TxtLayar.Text = Trim(TxtLayar.Text) & "5"
Case 5
'menampilkan nilai yg dimasukan pada layar
TxtLayar.Text = Trim(TxtLayar.Text) & "6"
Case 6
'menampilkan nilai yg dimasukan pada layar
TxtLayar.Text = Trim(TxtLayar.Text) & "7"
Case 7
'menampilkan nilai yg dimasukan pada layar
TxtLayar.Text = Trim(TxtLayar.Text) & "8"
Case 8
'menampilkan nilai yg dimasukan pada layar
TxtLayar.Text = Trim(TxtLayar.Text) & "9"
Case 9
'menampilkan nilai yg dimasukan pada layar
TxtLayar.Text = Trim(TxtLayar.Text) & "0"
End Select
End Sub
Private Sub cmdHasil_Click()
'jika bukan nilai pertama
'atau yg dimasukan adalah nilai kedua
If pertama = False Then
'mengambil nilai kedua dari layar
nilai2 = Val(TxtLayar.Text)
'memilih operasi yg dilakukan
'berdasarkan pilihan operasi dari user
Select Case operasi
'operasi penjumlahan
Case 1
hasil = nilai1 + nilai2
'operasi pengurangan
Case 2
hasil = nilai1 - nilai2
'operasi perkalian
Case 3
hasil = nilai1 * nilai2
'operasi pembagian
Case 4
hasil = nilai1 / nilai2
End Select
'menampilkan hasil operasi ke layar
TxtLayar.Text = hasil
End If
End Sub
Private Sub cmdKosong_Click()
'operasi perhitungan direset
operasi = 0
'mengembalikan ke posisi nilai pertama
pertama = True
'mereset nilai penampung hasil operasi
hasil = 0
'mengosongi nilai text box
TxtLayar.Text = ""
End Sub
Private Sub cmdOperasi_Click(Index As Integer)
'jika niali pertama
If pertama = True Then
'mengubah ke posisi nilai kedua
pertama = False
'mengambil nilai pertama
'pada layar text box
nilai1 = Val(TxtLayar.Text)
'mengosongi layar text box
TxtLayar.Text = ""
'mengubah text rata kiri
TxtLayar.Alignment = 0
'pengecekan operasi perhitungan
Select Case Index
Case 0
'operasi penjumlahan
operasi = 1
Case 1
'operasi pengurangan
operasi = 2
Case 2
'operasi perkalian
operasi = 3
Case 3
'operasi pembagian
operasi = 4
End Select
End If
End Sub
Private Sub Form_Load()
'memberi nilai awal var penampung
nilai1 = 0
nilai2 = 0
hasil = 0
'mengosongi layar textbox
TxtLayar.Text = ""
'tidak ada operasi yg dipilih
operasi = 0
'mengubah ke posisi nilai pertama
pertama = True
End Sub