Visual Basic Lanjutan Praktikum 2 Percabangan (IIF dan Select Case)




Assalamu’alaikum warahmatullahi wabarakatuh..
Kembali lagi bersama saya Ilham Ramadhan, saya akan melanjutkan pelajaran yang pernah saya bahas yaitu Operator cek link DISINIKali ini saya akan membahas tentang Visual Basic Lanjutan Percabangan(IIF dan Select Case), oke langsung saja kita bahas tanpa berlama-lama, oke check it out...


Percabangan (IIF)


  • Bentuk penulisan dari struktur If–Then–Else adalah tegak atau vertikal.
  • Bentuk semacam ini tentu banyak memakan baris atau terulang panjang, apalagi jika bentuknya Nested If.
  • Untuk mengatasi kekurangan tersebut, dapat menggunakan struktur IIF yaitu struktur If–Then-else yang bentuknya datar atau horisontal.
  • Bentuk penulisan IIF butuh variabel untuk menampung jawabannya.
  • Dari segi kecepatan struktur IIF lebih cepat prosesnya dibandingkan dengan struktur If – Then – Else.

  • kondisi: Berisi perbandingan antara satu variabel/konstanta dengan variabel/konstanta
    lain dengan menggunakan tanda <, >, =, <> dari suatu keadaan yang akan menghasilkan nilai“
    Benar” atau “Salah”.
  • Jawaban 1 Jawaban yang diberikan jika kondisi penyeleksian bernilai “Benar”.
  • Jawaban 2 Jawaban yang diberikan jika kondisi penyeleksian bernilai “Salah”.

Percabangan (Select Case)
  • tes_ekspresi : bisa bernilai numerik maupun string.
  • ekspresi 1 : Nilai pertama proses seleksi dari tes_ekspresi.
  • ekspresi 2 : Nilai pertama proses seleksi dari tes_ekspresi. Penulisan nilai paa ekspresi 1 maupun ekspresi 2 bisa langsung berbentuk konstanta atau melalui operator pembanding.
  • blok perintah 1 : Suatu rangkaian perintah yang dikerjakan bila kondisi ekspresi 1 terpenuhi.
  • blok perintah 2 : Suatu rangkaian perintah yang dikerjakan bila kondisi ekspresi 2 terpenuhi.
  • blok perintah 3 : Suatu rangkaian perintah yang dikerjakan bila kondisi ekspresi 3 terpenuhi.

Latihan 1

  • Source Code


Public Class Form1



    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim bil As Integer = TextBox1.Text
        Select Case bil
            Case 80 To 100
                TextBox2.Text = "A"
            Case 65 To 79
                TextBox2.Text = "B"
            Case 47 To 64
                TextBox2.Text = "C"
            Case 26 To 46
                TextBox2.Text = "D"
            Case Else
                TextBox2.Text = "E" 'LaptopInformatika.com
        End Select
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim pesan As String
        pesan = MsgBox("Yakin Mau Keluar ??", vbYesNo, "Konfirmasi") 'LaptopInformatika.com
        If pesan = vbYes Then
            Close()
            End
        End If
    End Sub
End Class

Kasus 6

  • Source Code
Public Class Form2
    Dim hadir, tugas, uts, uas As Integer
    Dim akhir As Double
    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        If Not ((e.KeyChar >= "0" And e.KeyChar <= "9") Or e.KeyChar = vbBack) Then e.Handled = True
        If (e.KeyChar = Chr(13)) Then
            TextBox2.Focus()
        End If
    End Sub

    Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress

        Dim keyascii As Short = Asc(e.KeyChar)
        If (e.KeyChar Like "[A-Z,a-z]" _
            OrElse keyascii = Keys.Back _
            OrElse keyascii = Keys.Space _
            OrElse keyascii = Keys.Return _
            OrElse keyascii = Keys.Delete) Then 'LaptopInformatika.com
            keyascii = 0
        End If
        e.Handled = CBool(keyascii)
        If e.KeyChar = Chr(13) Then
            Me.ComboBox1.Focus()
        End If
    End Sub

    Private Sub ComboBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ComboBox1.KeyPress

        If e.KeyChar = Chr(13) Then
            Me.TextBox3.Focus()
        End If
    End Sub

    Private Sub TextBox3_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox3.KeyPress

        If Not ((e.KeyChar >= "0" And e.KeyChar <= "9") Or e.KeyChar = vbBack) Then e.Handled = True
        If (e.KeyChar = Chr(13)) Then
            TextBox4.Focus()
        End If
    End Sub

    Private Sub TextBox4_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox4.KeyPress

         If Not ((e.KeyChar >= "0" And e.KeyChar <= "9") Or e.KeyChar = vbBack) Then e.Handled = True
        If (e.KeyChar = Chr(13)) Then
            TextBox5.Focus() 'LaptopInformatika.com
        End If
    End Sub

    Private Sub TextBox5_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox5.KeyPress

        If Not ((e.KeyChar >= "0" And e.KeyChar <= "9") Or e.KeyChar = vbBack) Then e.Handled = True
        If (e.KeyChar = Chr(13)) Then
            TextBox6.Focus()
        End If
    End Sub

    Private Sub TextBox6_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox6.KeyPress

        If Not ((e.KeyChar >= "0" And e.KeyChar <= "9") Or e.KeyChar = vbBack) Then e.Handled = True
        If (e.KeyChar = Chr(13)) Then
            hadir = Val(TextBox3.Text)
            tugas = Val(TextBox4.Text)
            uts = Val(TextBox5.Text)
            uas = Val(TextBox6.Text)
            akhir = (hadir * 20 / 100) + (tugas * 25 / 100) + (uts * 25 / 100) + (uas * 30 / 100)
            TextBox7.Text = akhir
            If akhir >= 86 Then
                TextBox8.Text = "A"
            ElseIf akhir >= 74 Then
                TextBox8.Text = "B"
            ElseIf akhir >= 60 Then
                TextBox8.Text = "C"
            ElseIf akhir >= 46 Then
                TextBox8.Text = "D"
            Else : TextBox8.Text = "E" 'LaptopInformatika.com
            End If
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        TextBox1.Text = ""
        TextBox2.Text = ""
        TextBox3.Text = ""
        TextBox4.Text = ""
        TextBox5.Text = ""
        TextBox6.Text = ""
        TextBox7.Text = ""
        TextBox8.Text = ""
        ComboBox1.Text = "" 'LaptopInformatika.com
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        Dim pesan As String
        pesan = MsgBox("Yakin Mau Keluar ??", vbYesNo, "Konfirmasi") 'LaptopInformatika.com
        If pesan = vbYes Then
            Close()
            End
        End If
    End Sub

End Class


Kasus 7
  • Source Code
Public Class Form3
    Dim tunjangan, pokok, total, potongan, bersih As Double

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

        Dim keyascii As Short = Asc(e.KeyChar)
        If (e.KeyChar Like "[A-Z,a-z]" _
            OrElse keyascii = Keys.Back _
            OrElse keyascii = Keys.Space _
            OrElse keyascii = Keys.Return _
            OrElse keyascii = Keys.Delete) Then 'LaptopInformatika.com
            keyascii = 0
        End If
        e.Handled = CBool(keyascii)
        If e.KeyChar = Chr(13) Then
            Me.RadioButton1.Focus()
        End If
    End Sub

    Private Sub RadioButton1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles RadioButton1.KeyPress

        Me.ComboBox1.Focus()
    End Sub

    Private Sub RadioButton2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles RadioButton2.KeyPress

        Me.ComboBox1.Focus()
    End Sub

    Private Sub ComboBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ComboBox1.KeyPress

        If e.KeyChar = Chr(13) Then
            Me.TextBox3.Focus()
        End If
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

        If ComboBox1.Text = "MANAGER" Then
            TextBox2.Text = 5000000
        ElseIf ComboBox1.Text = "SPV" Then
            TextBox2.Text = 2500000
        ElseIf ComboBox1.Text = "Operational" Then 'LaptopInformatika.com
            TextBox2.Text = 1000000
        End If
    End Sub

    Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress

        If Not ((e.KeyChar >= "0" And e.KeyChar <= "9") Or e.KeyChar = vbBack) Then e.Handled = True
        If (e.KeyChar = Chr(13)) Then
            TextBox3.Focus()
        End If
    End Sub

    Private Sub TextBox3_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox3.KeyPress

        If Not ((e.KeyChar >= "0" And e.KeyChar <= "9") Or e.KeyChar = vbBack) Then e.Handled = True
        If (e.KeyChar = Chr(13)) Then
            tunjangan = Val(TextBox3.Text) 'LaptopInformatika.com
            pokok = Val(TextBox2.Text)
            total = tunjangan + pokok
            TextBox4.Text = total
            TextBox5.Focus()
        End If
    End Sub

    Private Sub TextBox4_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox4.KeyPress

        If Not ((e.KeyChar >= "0" And e.KeyChar <= "9") Or e.KeyChar = vbBack) Then e.Handled = True
        If (e.KeyChar = Chr(13)) Then
        End If
    End Sub

    Private Sub TextBox5_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox5.KeyPress

        If Not ((e.KeyChar >= "0" And e.KeyChar <= "9") Or e.KeyChar = vbBack) Then e.Handled = True
        If (e.KeyChar = Chr(13)) Then
            potongan = Val(TextBox5.Text) 'LaptopInformatika.com
            bersih = total - potongan
            TextBox6.Text = bersih
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        TextBox1.Text = ""
        RadioButton1.Checked = False
        RadioButton2.Checked = False
        ComboBox1.Text = ""
        TextBox2.Text = ""
        TextBox3.Text = ""
        TextBox4.Text = ""
        TextBox5.Text = ""
        TextBox6.Text = ""
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        Dim pesan As String
        pesan = MsgBox("Yakin Mau Keluar ??", vbYesNo, "Konfirmasi") 'LaptopInformatika.com
        If pesan = vbYes Then
            Close()
            End
        End If
    End Sub
End Class

Kasus 8

  • Source Code
Public Class Form4
    Dim harga, jumbel, subtotal, diskon, total As Integer
    Dim bayar, kembali As Double

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

        If Not ((e.KeyChar >= "0" And e.KeyChar <= "9") Or e.KeyChar = vbBack) Then e.Handled = True
        If (e.KeyChar = Chr(13)) Then
            TextBox2.Focus() 'LaptopIinformatika.com
    End Sub

    Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress

        Dim keyascii As Short = Asc(e.KeyChar)
        If (e.KeyChar Like "[A-Z,a-z]" _
            OrElse keyascii = Keys.Back _
            OrElse keyascii = Keys.Space _
            OrElse keyascii = Keys.Return _
            OrElse keyascii = Keys.Delete) Then
            keyascii = 0
        End If
        e.Handled = CBool(keyascii)
        If e.KeyChar = Chr(13) Then
            Me.ComboBox1.Focus() 'LaptopIinformatika.com
        End If
    End Sub

    Private Sub ComboBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ComboBox1.KeyPress

        If e.KeyChar = Chr(13) Then
            Me.TextBox4.Focus()
        End If
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

        If ComboBox1.Text = "Dus" Then
            TextBox3.Text = 100000
        ElseIf ComboBox1.Text = "Box" Then
            TextBox3.Text = 30000
        ElseIf ComboBox1.Text = "Botol" Then
            TextBox3.Text = 15000
        ElseIf ComboBox1.Text = "Kaplet" Then 'LaptopIinformatika.com
            TextBox3.Text = 5000
        End If
    End Sub

    Private Sub TextBox4_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox4.KeyPress

        If Not ((e.KeyChar >= "0" And e.KeyChar <= "9") Or e.KeyChar = vbBack) Then e.Handled = True
        If (e.KeyChar = Chr(13)) Then
            harga = Val(TextBox3.Text)
            jumbel = (TextBox4.Text)
            subtotal = harga * jumbel
            TextBox5.Text = subtotal
            Me.TextBox8.Focus()
            If (e.KeyChar = Chr(13)) Then
                TextBox6.Text = diskon
                If jumbel >= 100 Then
                    TextBox6.Text = subtotal * 10 / 100 'LaptopIinformatika.com
                ElseIf jumbel >= 50 Then
                    TextBox6.Text = subtotal * 7 / 100
                ElseIf jumbel >= 20 Then
                    TextBox6.Text = subtotal * 4 / 100
                End If
                subtotal = Val(TextBox5.Text)
                diskon = Val(TextBox6.Text)
                total = subtotal - diskon
                TextBox7.Text = total
            End If
        End If
    End Sub

    Private Sub TextBox8_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox8.KeyPress

        If Not ((e.KeyChar >= "0" And e.KeyChar <= "9") Or e.KeyChar = vbBack) Then e.Handled = True
        If (e.KeyChar = Chr(13)) Then
            bayar = TextBox8.Text
            kembali = bayar - total
            TextBox9.Text = kembali 'LaptopIinformatika.com
            Me.TextBox9.Focus()
        End If
    End Sub

    Private Sub TextBox9_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox9.KeyPress

        If Not ((e.KeyChar >= "0" And e.KeyChar <= "9") Or e.KeyChar = vbBack) Then e.Handled = True
        If (e.KeyChar = Chr(13)) Then
            Button1.Focus()
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        TextBox1.Text = ""
        TextBox2.Text = ""
        ComboBox1.Text = ""
        TextBox3.Text = ""
        TextBox4.Text = ""
        TextBox5.Text = ""
        TextBox6.Text = ""
        TextBox7.Text = ""
        TextBox8.Text = ""
        TextBox9.Text = ""
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        Dim pesan As String
        pesan = MsgBox("Yakin Mau Keluar ??", vbYesNo, "Konfirmasi") 'LaptopIinformatika.com
        If pesan = vbYes Then
            Close()
            End
        End If
    End Sub

End Class


Kasus 9



  • Source Code
Public Class Form5
    Dim pokok, jabatan, keluarga, total, ppn, bersih As Integer

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

        Dim keyascii As Short = Asc(e.KeyChar)
        If (e.KeyChar Like "[A-Z,a-z]" _
            OrElse keyascii = Keys.Back _
            OrElse keyascii = Keys.Space _
            OrElse keyascii = Keys.Return _
            OrElse keyascii = Keys.Delete) Then
            keyascii = 0
        End If
        e.Handled = CBool(keyascii)
        If e.KeyChar = Chr(13) Then
            Me.ComboBox1.Focus() 'LaptopInformatika.com
        End If
    End Sub

    Private Sub ComboBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ComboBox1.KeyPress

        If e.KeyChar = Chr(13) Then
            Me.ComboBox2.Focus()
        End If
    End Sub

    Private Sub ComboBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ComboBox2.KeyPress

        If e.KeyChar = Chr(13) Then
            Me.ComboBox3.Focus()
        End If
    End Sub

    Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged

        Select Case ComboBox2.Text
            Case "Manager"
                TextBox2.Text = 3000000
                TextBox3.Text = 2000000
            Case "SPV"
                TextBox2.Text = 1500000
                TextBox3.Text = 1000000
            Case "Operational"
                TextBox2.Text = 1000000
                TextBox3.Text = 500000 'LaptopInformatika.com
        End Select
    End Sub

    Private Sub ComboBox3_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ComboBox3.KeyPress

        pokok = Val(TextBox2.Text)
        jabatan = Val(TextBox3.Text)
        keluarga = Val(TextBox4.Text)
        total = pokok + jabatan + keluarga
        TextBox5.Text = total
        ppn = total * 10 / 100
        TextBox6.Text = ppn
        bersih = total - ppn 'PPn 10% dari Gaji Total
        TextBox7.Text = bersih 'LaptopInformatika.com
    End Sub

    Private Sub ComboBox3_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox3.SelectedIndexChanged

        Select Case ComboBox3.Text
            Case "0"
                TextBox4.Text = 100000
            Case "1"
                TextBox4.Text = 300000
            Case "2"
                TextBox4.Text = 500000 'LaptopInformatika.com
        End Select
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        TextBox1.Text = ""
        TextBox1.Text = ""
        ComboBox1.Text = ""
        ComboBox2.Text = ""
        ComboBox3.Text = ""
        TextBox1.Text = ""
        TextBox1.Text = ""
        TextBox1.Text = ""
        TextBox1.Text = ""
        TextBox1.Text = ""
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        Dim pesan As String
        pesan = MsgBox("Yakin Mau Keluar ??", vbYesNo, "Konfirmasi") 'LaptopInformatika.com
        If pesan = vbYes Then
            Close()
            End
        End If
    End Sub
End Class

Kasus 10



  • Source Code
Public Class Form6
    Dim sks, jumlahsks, total, bayar, kembali As Integer

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

        If Not ((e.KeyChar >= "0" And e.KeyChar <= "9") Or e.KeyChar = vbBack) Then e.Handled = True
        If (e.KeyChar = Chr(13)) Then
            Me.TextBox2.Focus()
        End If
    End Sub

    Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress

        Dim keyascii As Short = Asc(e.KeyChar)
        If (e.KeyChar Like "[A-Z,a-z]" _
            OrElse keyascii = Keys.Back _
            OrElse keyascii = Keys.Space _
            OrElse keyascii = Keys.Return _
            OrElse keyascii = Keys.Delete) Then 'LaptopInformatika.com
            keyascii = 0
        End If
        e.Handled = CBool(keyascii)
        If e.KeyChar = Chr(13) Then
            Me.ComboBox1.Focus()
        End If
    End Sub

    Private Sub ComboBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ComboBox1.KeyPress

        If e.KeyChar = Chr(13) Then
            Me.ComboBox2.Focus()
        End If
    End Sub

    Private Sub ComboBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ComboBox2.KeyPress

        Dim keyascii As Short = Asc(e.KeyChar)
        If (e.KeyChar Like "[A-Z,a-z]" _
            OrElse keyascii = Keys.Back _
            OrElse keyascii = Keys.Space _
            OrElse keyascii = Keys.Return _
            OrElse keyascii = Keys.Delete) Then 'LaptopInformatika.com
            keyascii = 0
        End If
        e.Handled = CBool(keyascii)
        If e.KeyChar = Chr(13) Then
            Me.TextBox4.Focus()
        End If
    End Sub

    Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged

        If ComboBox1.Text = "Karyawan" Then
            If ComboBox2.Text = "Akutansi" Then
                TextBox3.Text = 500000
            ElseIf ComboBox2.Text = "Manajemen" Then
                TextBox3.Text = 450000
            ElseIf ComboBox2.Text = "Informatika" Then 'LaptopInformatika.com
                TextBox3.Text = 400000
            ElseIf ComboBox2.Text = "Informasi" Then
                TextBox3.Text = 380000
            End If
        ElseIf ComboBox1.Text = "Reguler" Then
            If ComboBox2.Text = "Akutansi" Then
                TextBox3.Text = 450000
            ElseIf ComboBox2.Text = "Manajemen" Then
                TextBox3.Text = 400000
            ElseIf ComboBox2.Text = "Informatika" Then 'LaptopInformatika.com
                TextBox3.Text = 350000
            ElseIf ComboBox2.Text = "Informasi" Then
                TextBox3.Text = 330000
            End If
        End If
    End Sub

    Private Sub TextBox4_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox4.KeyPress

        If Not ((e.KeyChar >= "0" And e.KeyChar <= "9") Or e.KeyChar = vbBack) Then e.Handled = True
        If (e.KeyChar = Chr(13)) Then
            sks = Val(TextBox3.Text)
            jumlahsks = Val(TextBox4.Text) 'LaptopInformatika.com
            total = sks * jumlahsks
            TextBox5.Text = total
            TextBox6.Focus()
        End If
    End Sub

    Private Sub TextBox6_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox6.KeyPress

        If Not ((e.KeyChar >= "0" And e.KeyChar <= "9") Or e.KeyChar = vbBack) Then e.Handled = True
        If (e.KeyChar = Chr(13)) Then
            bayar = Val(TextBox6.Text)
            kembali = bayar - total
            TextBox7.Text = kembali
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        TextBox1.Text = ""
        TextBox2.Text = ""
        ComboBox1.Text = ""
        ComboBox2.Text = ""
        TextBox3.Text = ""
        TextBox4.Text = ""
        TextBox5.Text = ""
        TextBox6.Text = ""
        TextBox7.Text = ""
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        Dim pesan As String
        pesan = MsgBox("Yakin Mau Keluar ??", vbYesNo, "Konfirmasi") 'LaptopInformatika.com
        If pesan = vbYes Then
            Close()
            End
        End If
    End Sub
End Class

Kasus 11




  • Source Code
Public Class Form7
    Dim harga, beli, subtotal, diskon, total, bayar, kembali As Integer

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

        Dim keyascii As Short = Asc(e.KeyChar)
        If (e.KeyChar Like "[A-Z,a-z]" _
            OrElse keyascii = Keys.Back _
            OrElse keyascii = Keys.Space _
            OrElse keyascii = Keys.Return _
            OrElse keyascii = Keys.Delete) Then
            keyascii = 0
        End If
        e.Handled = CBool(keyascii)
        If e.KeyChar = Chr(13) Then
            Me.ComboBox1.Focus() 'LaptopInformatika.com
        End If
    End Sub

    Private Sub ComboBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ComboBox1.KeyPress

        If e.KeyChar = Chr(13) Then
            Me.ComboBox2.Focus()
        End If
    End Sub

    Private Sub ComboBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ComboBox2.KeyPress

        If e.KeyChar = Chr(13) Then
            Me.TextBox3.Focus()
        End If
    End Sub

    Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged

        If ComboBox1.Text = "IMP" Then
            If ComboBox2.Text = "XL" Then
                TextBox2.Text = 250000
            ElseIf ComboBox2.Text = "L" Then
                TextBox2.Text = 240000
            ElseIf ComboBox2.Text = "M" Then 'LaptopInformatika.com
                TextBox2.Text = 230000
            End If
        ElseIf ComboBox1.Text = "Prada" Then
            If ComboBox2.Text = "XL" Then
                TextBox2.Text = 170000
            ElseIf ComboBox2.Text = "L" Then
                TextBox2.Text = 160000
            ElseIf ComboBox2.Text = "M" Then
                TextBox2.Text = 150000
            End If
        ElseIf ComboBox1.Text = "Gucci" Then
            If ComboBox2.Text = "XL" Then
                TextBox2.Text = 280000
            ElseIf ComboBox2.Text = "L" Then
                TextBox2.Text = 270000
            ElseIf ComboBox2.Text = "M" Then 'LaptopInformatika.com
                TextBox2.Text = 260000
            End If
        ElseIf ComboBox1.Text = "Louis" Then
            If ComboBox2.Text = "XL" Then
                TextBox2.Text = 360000
            ElseIf ComboBox2.Text = "L" Then
                TextBox2.Text = 350000
            ElseIf ComboBox2.Text = "M" Then
                TextBox2.Text = 340000
            End If
        ElseIf ComboBox1.Text = "Denim" Then
            If ComboBox2.Text = "XL" Then
                TextBox2.Text = 130000
            ElseIf ComboBox2.Text = "L" Then
                TextBox2.Text = 120000
            ElseIf ComboBox2.Text = "M" Then 'LaptopInformatika.com
                TextBox2.Text = 110000
            End If
        End If
    End Sub

    Private Sub TextBox3_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox3.KeyPress

        If Not ((e.KeyChar >= "0" And e.KeyChar <= "9") Or e.KeyChar = vbBack) Then e.Handled = True
        If (e.KeyChar = Chr(13)) Then
            harga = Val(TextBox2.Text)
            beli = Val(TextBox3.Text)
            subtotal = harga * beli
            TextBox4.Text = subtotal
            Dim jumdis As Integer = TextBox3.Text
            Select Case jumdis
                Case 1 To 20
                    TextBox5.Text = subtotal * 5 / 100
                Case 21 To 50
                    TextBox5.Text = subtotal * 10 / 100 'LaptopInformatika.com
            End Select
            diskon = Val(TextBox5.Text)
            total = subtotal - diskon
            TextBox6.Text = total
            Me.TextBox7.Focus()
        End If
    End Sub

    Private Sub TextBox7_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox7.KeyPress

        If Not ((e.KeyChar >= "0" And e.KeyChar <= "9") Or e.KeyChar = vbBack) Then e.Handled = True
        If (e.KeyChar = Chr(13)) Then
            total = Val(TextBox6.Text)
            bayar = Val(TextBox7.Text)
            kembali = bayar - total
            TextBox8.Text = kembali
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        TextBox1.Text = ""
        ComboBox1.Text = ""
        ComboBox2.Text = ""
        TextBox2.Text = ""
        TextBox3.Text = ""
        TextBox4.Text = ""
        TextBox5.Text = ""
        TextBox6.Text = ""
        TextBox7.Text = ""
        TextBox8.Text = ""
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        Dim pesan As String
        pesan = MsgBox("Yakin Mau Keluar ??", vbYesNo, "Konfirmasi") 'LaptopInformatika.com
        If pesan = vbYes Then
            Close()
            End
        End If
    End Sub
End Class

Berlangganan update artikel terbaru via email:

1 Response to "Visual Basic Lanjutan Praktikum 2 Percabangan (IIF dan Select Case)"

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel