Friday, January 11, 2013

Contoh Penggunaan Listview Pada Visual Studio 2008


Klik gambar untuk memperbesar.


Public Class Form1
    Sub buattabel()
        LV.Columns.Add("Maskapai", 100, HorizontalAlignment.Center)
        LV.Columns.Add("Tujuan", 120, HorizontalAlignment.Center)
        LV.Columns.Add("Harga", 100, HorizontalAlignment.Center)
        LV.Columns.Add("Nama Pemesan", 120, HorizontalAlignment.Center)
        LV.Columns.Add("Jumlah Pesan", 80, HorizontalAlignment.Center)
        LV.Columns.Add("Total Harga", 120, HorizontalAlignment.Center)
        LV.View = View.Details
        LV.GridLines = True
        LV.FullRowSelect = True
     End Sub

    Sub isitabel()
        Dim lst As New ListViewItem
        lst.Text = Maskapai.Text
        lst.SubItems.Add(Tujuan.Text)
        lst.SubItems.Add(Harga.Text)
        lst.SubItems.Add(Nama.Text)
        lst.SubItems.Add(Jumlah.Text)
        lst.SubItems.Add(Total.Text)
        LV.Items.Add(lst)
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Call isimaskapai()
        Call isitujuan()
        Call bersih()
        buattabel()
    End Sub

    Sub isimaskapai()
        Maskapai.Items.Add("Garuda")
        Maskapai.Items.Add("Lion")
        Maskapai.Items.Add("Sriwijaya")
    End Sub


    Sub isitujuan()
        Tujuan.Items.Add("Medan-Padang")
        Tujuan.Items.Add("Medan-Jakarta")
        Tujuan.Items.Add("Medan-Bali")
    End Sub

    Sub bersih()
        Maskapai.Text = ""
        Tujuan.Text = ""
        Harga.Text = ""
        Nama.Text = ""
        Jumlah.Text = ""
        Total.Text = ""
    End Sub

    Private Sub Batal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Batal.Click
        Call bersih()
    End Sub

    Private Sub Keluar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Keluar.Click
        Me.Close()
    End Sub

    Private Sub Hitung_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Hitung.Click
        Total.Text = Harga.Text * Jumlah.Text
    End Sub

    Private Sub Tujuan_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Tujuan.SelectedIndexChanged
        If Maskapai.Text = "Garuda" Then
            If Tujuan.Text = "Medan-Padang" Then
                Harga.Text = 570000
            ElseIf Tujuan.Text = "Medan-Jakarta" Then
                Harga.Text = 800000
            ElseIf Tujuan.Text = "Medan-Bali" Then
                Harga.Text = 9800000
            End If
        ElseIf Maskapai.Text = "Lion" Then
            If Tujuan.Text = "Medan-Padang" Then
                Harga.Text = 390000
            ElseIf Tujuan.Text = "Medan-Jakarta" Then
                Harga.Text = 630000
            ElseIf Tujuan.Text = "Medan-Bali" Then
                Harga.Text = 780000
            End If
        ElseIf Maskapai.Text = "Sriwijaya" Then
            If Tujuan.Text = "Medan-Padang" Then
                Harga.Text = 420000
            ElseIf Tujuan.Text = "Medan-Jakarta" Then
                Harga.Text = 700000
            ElseIf Tujuan.Text = "Medan-Bali" Then
                Harga.Text = 870000
            End If
        End If
        Nama.Focus()
    End Sub

    Private Sub Maskapai_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Maskapai.SelectedIndexChanged
        Tujuan_SelectedIndexChanged(sender, e)
        Nama.Focus()
    End Sub

    Private Sub Jumlah_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Jumlah.KeyPress
        If Asc(e.KeyChar) = 13 Then
            Hitung_Click(sender, e)
            Total.Focus()
        End If
    End Sub

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

        If Maskapai.Text = "" Or Tujuan.Text = "" Or Harga.Text = "" Or Nama.Text = "" Or Jumlah.Text = "" Or Total.Text = "" Then
            MsgBox("Data Harus Lengkap")
        Else
            isitabel()
            bersih()
        End If
    End Sub

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

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        If LV.Items.Count <> 0 Then
            LV.Items.Remove(LV.SelectedItems(0))
        Else
            MsgBox("Tabel Sudah Kosong")
        End If
    End Sub

    Private Sub Nama_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Nama.KeyPress
        If Asc(e.KeyChar) = 13 Then
            Jumlah.Focus()
        End If
    End Sub

    Private Sub Total_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Total.KeyPress
        If Asc(e.KeyChar) = 13 Then
            Button1_Click(sender, e)
        End If
    End Sub

End Class


Semoga Bermanfaat

No comments:

Post a Comment