Pemrograman Visual:Latihan Ujian

Kode ini memerlukan sebuah objek ComboBox yang disebut cboJenis yang digunakan untuk memilih jenis mobil, sebuah objek TextBox yang disebut txtJumlahHari yang digunakan untuk memasukkan jumlah hari sewa, sebuah objek TextBox yang disebut txtSewaPerHari yang digunakan untuk menampilkan harga sewa per hari, dan sebuah objek TextBox yang disebut txtJumlahBayar yang digunakan untuk menampilkan jumlah bayar.

Button Hitung, untuk menjalankan program

Public Class Form1
    Private Sub btnHitung_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHitung.Click
        Dim jenis As String
        jenis = cboJenis.Text
        Select Case jenis
            Case "Bus"
                txtSewaPerHari.Text = "1000000"
                txtJumlahBayar.Text = Val(txtSewaPerHari.Text) * Val(txtJumlahHari.Text)
            Case "Sedan"
                txtSewaPerHari.Text = "800000"
                txtJumlahBayar.Text = Val(txtSewaPerHari.Text) * Val(txtJumlahHari.Text)
            Case "Kijang"
                txtSewaPerHari.Text = "400000"
                txtJumlahBayar.Text = Val(txtSewaPerHari.Text) * Val(txtJumlahHari.Text)
            Case "Carry"
                txtSewaPerHari.Text = "200000"
                txtJumlahBayar.Text = Val(txtSewaPerHari.Text) * Val(txtJumlahHari.Text)
            Case Else
                txtSewaPerHari.Text = ""
                txtJumlahBayar.Text = ""
                MsgBox("Jenis mobil tidak ditemukan!")
        End Select
    End Sub
End Class

Kode dibawah memiliki 2 event, pada saat index pada comboBox berubah, dan satu lagi pada saat text pada TextBox JumlahHari berubah, sehingga otomatis akan menghitung dengan sendirinya

Public Class Form1
    Private Sub cboJenis_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboJenis.SelectedIndexChanged
        Dim jenis As String
        Dim harga As Integer

        jenis = cboJenis.Text
        Select Case jenis
            Case "Bus"
                harga = 1000000
            Case "Sedan"
                harga = 800000
            Case "Kijang"
                harga = 400000
            Case "Carry"
                harga = 200000
            Case Else
                harga = 0
                MsgBox("Jenis mobil tidak ditemukan!")
        End Select

        If harga <> 0 Then
            txtSewaPerHari.Text = harga
            txtJumlahBayar.Text = harga * Val(txtJumlahHari.Text)
        Else
            txtSewaPerHari.Text = ""
            txtJumlahBayar.Text = ""
        End If
    End Sub

    Private Sub txtJumlahHari_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtJumlahHari.TextChanged
        Dim harga As Integer
        harga = Val(txtSewaPerHari.Text)
        txtJumlahBayar.Text = harga * Val(txtJumlahHari.Text)
    End Sub
End Class

Dibawah ini adalah kode untuk menambahkan pilihan pada comboBox

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cboJenis.Items.Add("Bus")
        cboJenis.Items.Add("Sedan")
        cboJenis.Items.Add("Kijang")
        cboJenis.Items.Add("Carry")
    End Sub

    Private Sub cboJenis_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboJenis.SelectedIndexChanged
        Dim jenis As String
        Dim harga As Integer

        jenis = cboJenis.Text
        Select Case jenis
            Case "Bus"
                harga = 1000000
            Case "Sedan"
                harga = 800000
            Case "Kijang"
                harga = 400000
            Case "Carry"
                harga = 200000
            Case Else
                harga = 0
                MsgBox("Jenis mobil tidak ditemukan!")
        End Select

        If harga <> 0 Then
            txtSewaPerHari.Text = harga
            txtJumlahBayar.Text = harga * Val(txtJumlahHari.Text)
        Else
            txtSewaPerHari.Text = ""
            txtJumlahBayar.Text = ""
        End If
    End Sub

    Private Sub txtJumlahHari_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtJumlahHari.TextChanged
        Dim harga As Integer
        harga = Val(txtSewaPerHari.Text)
        txtJumlahBayar.Text = harga * Val(txtJumlahHari.Text)
    End Sub
End Class

Kode dibawah ini di event btnBersihkan_Click digunakan untuk mengosongkan semua input pada form, sedangkan pada event btnKeluar_Click digunakan untuk menutup form.

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cboJenis.Items.Add("Bus")
        cboJenis.Items.Add("Sedan")
        cboJenis.Items.Add("Kijang")
        cboJenis.Items.Add("Carry")
    End Sub

    Private Sub cboJenis_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboJenis.SelectedIndexChanged
        Dim jenis As String
        Dim harga As Integer

        jenis = cboJenis.Text
        Select Case jenis
            Case "Bus"
                harga = 1000000
            Case "Sedan"
                harga = 800000
            Case "Kijang"
                harga = 400000
            Case "Carry"
                harga = 200000
            Case Else
                harga = 0
                MsgBox("Jenis mobil tidak ditemukan!")
        End Select

        If harga <> 0 Then
            txtSewaPerHari.Text = harga
            txtJumlahBayar.Text = harga * Val(txtJumlahHari.Text)
        Else
            txtSewaPerHari.Text = ""
            txtJumlahBayar.Text = ""
        End If
    End Sub

    Private Sub txtJumlahHari_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtJumlahHari.TextChanged
        Dim harga As Integer
        harga = Val(txtSewaPerHari.Text)
        txtJumlahBayar.Text = harga * Val(txtJumlahHari.Text)
    End Sub

    Private Sub btnBersihkan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBersihkan.Click
        cboJenis.Text = ""
        txtJumlahHari.Text = ""
        txtSewaPerHari.Text = ""
        txtJumlahBayar.Text = ""
        txtNamaPenyewa.Text = ""
        txtNomorSewa.Text = ""
    End Sub

    Private Sub btnKeluar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnKeluar.Click
        Me.Close()
    End Sub
End Class