Menggunakan ListBox Pada VB.Net (+Source Code)

Apakah ListBox?

ListBox adalah salah satu komponen Graphical User Interface (GUI) yang sering digunakan. Komponen ini berfungsi untuk menyediakan daftar pilihan pada suatu form. Karakter dari komponen ini yaitu menampilkan daftar (list) pilihan lebih dari satu.

Baca: Mengenal Modifier, Variable Local, dan Variable Global

Apa yang Dapat Dilakukan Pada ListBox?

Beberapa hal yang dapat dilakukan pada komponen ListBox adalah:

  • Menambahkan data
  • Menghapus list yang dipilih
  • Menampilkan index list yang dipilih
  • Menampilkan jumlah list yang ada
  • Mengubah posisi index pada suatu list
  • Memindahkan semua isi listbox ke listbox lainnya.

Silahkan perhatikan coding di bawah, source code lengkap program disediakan melalui link di bawah coding.

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        addItemToList()
    End Sub

    ' Procedure menambahkan data ke listbox
    Sub addItemToList()
        Me.ListBox1.Items.Add("Jakarta")
        Me.ListBox1.Items.Add("Denpasar")
        Me.ListBox1.Items.Add("Yogyakarta")
        getRecordInfo()
    End Sub

    'Procedure untuk menambahkan data ke listbox
    Private Sub BtnAddToList_Click(sender As Object, e As EventArgs) Handles BtnAddToList.Click
        If Trim(Me.TextBox1.Text) <> "" Then
            Me.ListBox1.Items.Add(Me.TextBox1.Text)
        End If
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
        getRecordInfo()
    End Sub

    ' Procedure mendapatkan informasi list index dan jumlah list
    Sub getRecordInfo()
        Me.Label1.Text = Me.ListBox1.SelectedIndex & " of " & Me.ListBox1.Items.Count - 1
    End Sub

    'procedure memindahkan isi list1 yang sedang dipilih ke list2
    Private Sub BtnMoveSelected_Click(sender As Object, e As EventArgs) Handles BtnMoveSelected.Click
        If ListBox1.SelectedIndex >= 0 Then
            Me.ListBox2.Items.Add(Me.ListBox1.SelectedItem)
            Me.ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
        End If
    End Sub

    'procedure menghapus list yang dipilih
    Private Sub BtnRemove_Click(sender As Object, e As EventArgs) Handles BtnRemove.Click
        If ListBox1.SelectedIndex >= 0 Then
            Me.ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
        Else
            MsgBox("Select the list first, please!")
        End If
    End Sub

    'procedure memindahkan semua isi list1 ke list2
    Private Sub BtnMoveAll_Click(sender As Object, e As EventArgs) Handles BtnMoveAll.Click
        For i = 0 To Me.ListBox1.Items.Count - 1
            Me.ListBox2.Items.Add(Me.ListBox1.Items(0))
            Me.ListBox1.Items.RemoveAt(0)
        Next
    End Sub

    'procedure menaikkan posisi index dari list yang dipilih
    Private Sub BtnUp_Click(sender As Object, e As EventArgs) Handles BtnUp.Click
        Dim currentIndex As Integer
        Dim tempValue As String
        currentIndex = Me.ListBox1.SelectedIndex
        If currentIndex >= 1 Then
            tempValue = ListBox1.Items(currentIndex)
            Me.ListBox1.Items(currentIndex) = Me.ListBox1.Items(currentIndex - 1)
            Me.ListBox1.Items(currentIndex - 1) = tempValue
            Me.ListBox1.SelectedIndex = currentIndex - 1
        End If
    End Sub

    'procedure menurunkan posisi index dari list yang dipilih
    Private Sub BtnDown_Click(sender As Object, e As EventArgs) Handles BtnDown.Click
        Dim currentIndex As Integer
        Dim tempValue As String
        currentIndex = Me.ListBox1.SelectedIndex
        If currentIndex < ListBox1.Items.Count - 1 Then
            tempValue = ListBox1.Items(currentIndex)
            Me.ListBox1.Items(currentIndex) = Me.ListBox1.Items(currentIndex + 1)
            Me.ListBox1.Items(currentIndex + 1) = tempValue
            Me.ListBox1.SelectedIndex = currentIndex + 1
        End If
    End Sub

    'procedure mereset isi form/list ke kondisi awal
    Private Sub BtnResetForm_Click(sender As Object, e As EventArgs) Handles BtnResetForm.Click
        Me.ListBox1.Items.Clear()
        Me.ListBox2.Items.Clear()
        addItemToList()
    End Sub
End Class

Program lengkap dapat di download disini.

Pertanyaan Refleksi

  1. Dimana anda pernah melihat penggunaan listbox seperti program diatas?
  2. Setelah memahami penggunaan listbox, apa yang akan anda buat dengan komponen tersebut?
Baca: Menghitung Umur dengan VB.net (Artikel dan Source Code)