Tuesday, July 1, 2025

Visual Basic Lanjutan Teori 2 Percabangan (Struktur If-Then, If-Then-Else dan Nested IF)

Ilham Ramadhan
Tuesday, July 1, 2025

 

Assalamu’alaikum warahmatullahi wabarakatuh..
Kembali lagi bersama saya Ilham Ramadhan, saya akan melanjutkan pelajaran yang pernah saya bahas yaitu Operator silahkan cek di beranda . Kali ini saya akan membahas tentang Visual Basic Lanjutan Percabangan(Struktur If-Then dan Nested IF), oke langsung saja kita bahas tanpa berlama-lama, oke check it out...


Percabangan


Percabangan atau penyeleksian kondisi (Conditional Statement) merupakan suatu pernyataan yang menganalisa suatu keadaan dan mengambil keputusan berdasarkan pada hasil analisa itu.


Hasil dari penyeleksian adalah, jika bernilai benar maka akan dikerjakan instruksi tertentu.


Sedang jika kondisi salah, maka akan dikerjakan instruksi yang lain.


Percabangan IF - THEN

Struktur If – Then disebut juga dengan Branch Structure merupakan struktur percabangan dimana suatu ekspresi akan dikerjakan bila kondisinya terpenuhi.

Tetapi jika kondisinya tidak terpenuhi maka ekspresi di dalam struktur if tidak akan dijalankan dan blok If akan dilompati serta program akan melakukan tindakan berikutnya.


Kondisi : Berisi perbandingan antara satu variabel/konstanta dengan variabel/konstanta lain dengan menggunakan tanda <, >, =, <> dari suatu keadaan yang akan menghasilkan nilai Benar” atau Salah.

Ekspresi : Rangkaian kode program (blok program) yang akan dijalankan jika kondisinya memenuhi syarat “Benar.

Percabangan IF - THEN - ELSE

Struktur If – Then – Else disebut juga dengan Selection Structure merupakan struktur percabangan di mana suatu ekspresi akan dikerjakan bila kondisinya terpenuhi.

Struktur If – Then – Else disebut juga dengan Selection Structure merupakan struktur percabangan di mana suatu ekspresi akan dikerjakan bila kondisinya terpenuhi.


Kondisi : Berisi perbandingan antara satu variabel/konstanta dengan variabel/konstanta lain dengan menggunakan tanda <, >, =, <> dari suatu keadaan yang akan menghasilkan nilai Benar” atau Salah.

Ekspresi 1 Rangkaian kode program (blok program) yang akan dijalankan jika kondisinya bernilai “Benar” (memenuhi syarat).

Ekspresi 2 Rangkaian kode program (blok program) yang akan dijalankan jika kondisinya bernilai “Salah” (tidak memenuhi syarat).

Percabangan Nested If
  • Pada kondisi tertentu di dalam struktur If –ThenElse bisa ditempatkan struktur If–Then atau If–Then–Else yang lain.
  • Bentuk ini disebut dengan If Tersarang atau Nested If.


Latihan 1 (IF - THEN - ELSE)
  • Kode Buku, Nama Buku, Kategori Buku, harga buku, dan jumlah beli diinput.
  • Gunakan Event Keypress utk input dengan Keyboard.
  • Gunakan Validasi utk huruf dan angka.
  • Ketika di enter di textbox jumlah beli, sub total, diskon dan total tagihan akan muncul.
    - Subtotal=harga buku x jumlah beli.
    - Diskon = jika subtotal >= 100.000, diskon 5 % dari subtotal, selain itu tidak dapat diskon.
    - Total tagihan =subtotal-diskon.
  • Uang bayar diinput, klik enter maka uang kembali akan muncul.
  • Button Clear utk membersihkan textbox dan combobox.
  • Button Clear utk membersihkan textbox dan combobox.
  • Source Code
Public Class Form1

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

        Dim ket As String
        If TextBox1.Text >= 60 Then 'LaptopInformatika.com
            ket = "Lulus"
        Else
            ket = "Tidak Lulus"
        End If
        TextBox2.Text = ket
    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


Latihan 2 (Nested IF)
  • Source Code
Public Class Form2

    Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
        If ComboBox1.Text = "Garuda" Then
            If ComboBox2.Text = "Medan-Padang" Then
                TextBox1.Text = 500000
            ElseIf ComboBox2.Text = "Medan-Jakarta" Then 'LaptopInformatika.com
                TextBox1.Text = 1000000
            ElseIf ComboBox2.Text = "Medan-Bali" Then
                TextBox1.Text = 1500000
            End If
        ElseIf ComboBox1.Text = "Lion" Then
            If ComboBox2.Text = "Medan-Padang" Then
                TextBox1.Text = 300000
            ElseIf ComboBox2.Text = "Medan-Jakarta" Then 'LaptopInformatika.com
                TextBox1.Text = 600000
            ElseIf ComboBox2.Text = "Medan-Bali" Then
                TextBox1.Text = 800000
            End If
        ElseIf ComboBox1.Text = "Sriwijaya" Then
            If ComboBox2.Text = "Medan-Padang" Then
                TextBox1.Text = 400000
            ElseIf ComboBox2.Text = "Medan-Jakarta" Then 'LaptopInformatika.com
                TextBox1.Text = 800000
            ElseIf ComboBox2.Text = "Medan-Bali" Then
                TextBox1.Text = 1000000
            End If
        End If
    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

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ComboBox1.Text = ""
        ComboBox2.Text = ""
        TextBox1.Text = ""
    End Sub
End Class

Latihan 3 (Percabangan IIF)


  • Source Code
Public Class Form3

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

        Dim hari As String
        Dim ke As String = ComboBox1.Text
        hari = IIf(ke = "1", "SENIN", IIf(ke = "2", "SELASA", IIf(ke = "3", "RABU", IIf(ke = "4", "KAMIS",
                IIf(ke = "5", "JUMAT", IIf(ke = "6", "SABTU", "MINGGU"))))))
        TextBox1.Text = hari '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 1 (If - Then - Else)




  • Source Code
Public Class Form4
    Dim harga, jumlah, 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

        If e.KeyChar = Chr(13) Then
            Me.TextBox2.Focus() 'LaptopInformatika.com
        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
            keyascii = 0
        End If
        If e.KeyChar = Chr(13) Then
            Me.ComboBox1.Focus()
        End If
        e.Handled = CBool(keyascii) 'LaptopInformatika.com
    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() 'LaptopInformatika.com
        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
            Me.TextBox4.Focus() 'LaptopInformatika.com
        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)
            jumlah = Val(TextBox4.Text)
            subtotal = harga * jumlah
            TextBox5.Text = subtotal
            If subtotal >= 100000 Then
                TextBox6.Text = subtotal * 5 / 100
            ElseIf subtotal <= 100 Then
                TextBox6.Text = subtotal * 0 / 100
            End If
            Me.TextBox6.Focus() 'LaptopInformatika.com
        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
            subtotal = Val(TextBox5.Text)
            diskon = Val(TextBox6.Text)
            total = subtotal - diskon
            TextBox7.Text = total
        End If
        Me.TextBox8.Focus() 'LaptopInformatika.com
    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
            total = Val(TextBox7.Text)
            bayar = Val(TextBox8.Text)
            kembali = bayar - total
            TextBox9.Text = kembali 'LaptopInformatika.com
        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") 'LaptopInformatika.com
        If pesan = vbYes Then
            Close()
            End
        End If
    End Sub
End Class

Kasus 2 (If - Then - Else)


  • Source Code
Public Class Form5
    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
            Me.TextBox2.Focus() 'LaptopInformatika.com
        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
            keyascii = 0
        End If
        If e.KeyChar = Chr(13) Then
            Me.ComboBox1.Focus()
        End If
        e.Handled = CBool(keyascii) 'LaptopInformatika.com
    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() 'LaptopInformatika.com
        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
            Me.TextBox4.Focus() 'LaptopInformatika.com
        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
            Me.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
            Me.TextBox6.Focus() 'LaptopInformatika.com
        End If
    End Sub

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

        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 >= 84 Then
            TextBox8.Text = "A"
            TextBox9.Text = "Memuaskan"
        ElseIf akhir >= 64 Then
            TextBox8.Text = "B"
            TextBox9.Text = "Baik"
        ElseIf akhir >= 50 Then
            TextBox8.Text = "C"
            TextBox9.Text = "Cukup"
        ElseIf akhir < 50 Then
            TextBox8.Text = "D"
            TextBox9.Text = "Gagal" 'LaptopInformatika.com
        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") 'LaptopInformatika.com
        If pesan = vbYes Then
            Close()
            End
        End If
    End Sub
End Class

Kasus 3 (If - Then - Else)
  • Kode Obat,nama obat dan Satuan diinput.
  • Combox satuan dipilih, harga satuan akan muncul dari penyeleksian kondisi.
  • Gunakan Event Keypress utk input dengan Keyboard.
  • Gunakan Validasi utk huruf dan angka.
  • Ketika di enter di textbox jumlah beli, sub total, diskon dan total tagihan akan muncul.
    - Subtotal=harga buku x jumlah beli.
    - Diskon = jika jumlah beli >= 100, diskon 10 %
      dari subtotal, jika jumlah beli >= 50, diskon 7 %
      dari subtotal, jika jumlah beli >= 20, diskon 4 %
      dari subtotal selain itu tidak dapat diskon.
    - Total tagihan =subtotal-diskon.
  • Uang bayar diinput, klik enter maka uang kembali akan muncul.
  • Button Clear utk membersihkan textbox dan combobox.
  • Button Clear utk membersihkan textbox dan combobox.
  • Source Code
Public Class Form6
    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 e.KeyChar = Chr(13) Then
            Me.TextBox2.Focus() 'LaptopInformatika.com
        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
            keyascii = 0
        End If
        If e.KeyChar = Chr(13) Then
            Me.ComboBox1.Focus()
        End If
        e.Handled = CBool(keyascii) 'LaptopInformatika.com
    End Sub

    Private Sub ComboBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ComboBox1.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
        If e.KeyChar = Chr(13) Then
            Me.TextBox4.Focus()
        End If
        e.Handled = CBool(keyascii) 'LaptopInformatika.com
        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
            TextBox3.Text = 5000
            If e.KeyChar = Chr(13) Then
            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
            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 'LaptopInformatika.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 'LaptopInformatika.com
            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
            total = Val(TextBox7.Text)
            bayar = Val(TextBox8.Text)
            kembali = bayar - total
            TextBox9.Text = kembali
            Me.TextBox9.Focus() 'LaptopInformatika.com
        End If
    End Sub

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

        If (e.KeyChar = Chr(13)) Then
            Me.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 = "" '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 4 (Nested If)
  • Source Code
Public Class Form7
    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() 'LaptopInformatika.com
        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
            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

        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 5 (Nested If)


  • Source Code
Public Class Form8
    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

Oke segitu dulu ya pelajaran kita, nanti kita lanjut lagi hehehe :D

Show comments
Hide comments