Latihan Praktik Visual Basic
1. Message Box
Buatlah sebuah form penginputan password, dimana ketika password yang diinputkan tidak sesuai,
maka akan memunculkan Message Box berupa pesan Error “Password Salah! Silakan Coba Input
Lagi” dan apabila password yang diinputkan telah sesuai, akan memunculkan Message Box berupa
Informasi “Password Sudah Benar”.
Interface
Listing
Dim password As String = "12345"
If TextBox1.Text = password Then
MessageBox.Show("Password sudah benar!", "Informasi", MessageBoxButtons.OK,
MessageBoxIcon.Information)
Else
MessageBox.Show("Password salah! Silakan coba input lagi.", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error)
TextBox1.Clear()
End If
MessageBoxButtons berfungsi untuk menampilkan Message Box, sedangkan TextBox1.Clear, fungsinya
untuk membuat TextBox1 menjadi kosong kembali setelah dilakukan penginputan.
2. Multiple Form
Buatlah 4 Form, yang terdiri dari Form Utama, Form 1, Form 2, dan Form 3.
Interface Form Utama
Interface Form 1, Form 2, Form 3
Listing Code Form Utama
Public Class FormUtama
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim form1 As New Form1()
form1.Show()
Me.Hide()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim form2 As New Form2()
form2.Show()
Me.Hide()
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim form3 As New Form3()
form3.Show()
Me.Hide()
End Sub
End Class
Listing Code Form 1, 2, 3
Public Class Form1
Private Sub Back1_Click(sender As Object, e As EventArgs) Handles Back1.Click
Dim formUtama As New FormUtama()
formUtama.Show()
Me.Close()
End Sub
Private Sub Tutup1_Click(sender As Object, e As EventArgs) Handles Tutup1.Click
Application.Exit()
End Sub
End Class
3. CheckBox dan Hitung Otomatis
Aplikasi yang dibuat, beberapa listing codenya berada pada CheckBox
Interface
Listing Code
Perhatikan, terdapat Deklarasi DIM pada bagian setelah Public Class.
Public Class Form1
Dim obat As Double = 120000
Dim gigi As Double = 80000
Dim mata As Double = 100000
Dim medical As Double = 350000
Private total As Double = 0
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Hasil.Text = "Rp." & 0
End Sub
Private Sub CkObat_CheckedChanged(sender As Object, e As EventArgs) Handles
CkObat.CheckedChanged
If CkObat.Checked Then
total = total + obat
Else
total = total - obat
End If
Hasil.Text = "Rp." & total
End Sub
Private Sub CkGigi_CheckedChanged(sender As Object, e As EventArgs) Handles
CkGigi.CheckedChanged
If CkGigi.Checked Then
total = total + gigi
Else
total = total - gigi
End If
Hasil.Text = "Rp." & total
End Sub
Private Sub CkMata_CheckedChanged(sender As Object, e As EventArgs) Handles
CkMata.CheckedChanged
If CkMata.Checked Then
total = total + mata
Else
total = total - mata
End If
Hasil.Text = "Rp." & total
End Sub
Private Sub CkMedical_CheckedChanged(sender As Object, e As EventArgs) Handles
CkMedical.CheckedChanged
If CkMedical.Checked Then
total = total + medical
Else
total = total - medical
End If
Hasil.Text = "Rp." & total
End Sub
End Class
4. ComboBox dan Hitung Otomatis
Aplikasi yang dibuat, listing codenya berada pada Combo Box dan Text Box.
Interface
Listing Code
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.StartPosition = FormStartPosition.CenterScreen
End Sub
Private Sub ComboJenis_SelectedIndexChanged(sender As Object, e As EventArgs) Handles
ComboJenis.SelectedIndexChanged
If Me.ComboJenis.Text = "VIP" Then
Me.TeksSewa.Text = 850000
ElseIf Me.ComboJenis.Text = "Kelas I" Then
Me.TeksSewa.Text = 500000
ElseIf Me.ComboJenis.Text = "Kelas II" Then
Me.TeksSewa.Text = 300000
ElseIf Me.ComboJenis.Text = "Kelas III" Then
Me.TeksSewa.Text = 130000
End If
End Sub
Private Sub TeksInap_TextChanged(sender As Object, e As EventArgs) Handles
TeksInap.TextChanged
Dim nilai2, nilai3 As Double
Dim nilai1 As Integer
nilai1 = TeksInap.Text
nilai2 = TeksSewa.Text
nilai3 = nilai2 * nilai1
TeksBiaya.Text = nilai3
End Sub
Private Sub TeksBayar_TextChanged(sender As Object, e As EventArgs) Handles
TeksBayar.TextChanged
TeksKembalian.Text = Val(TeksBayar.Text) - TeksBiaya.Text
End Sub
Private Sub ButtonClose_Click(sender As Object, e As EventArgs) Handles
ButtonClose.Click
Me.Close()
End Sub
End Class