VB.NET LListBox - Data Sorting

July 11, 2017 Oclemy VB.NET ListBox 0 minutes, 54 seconds

This is a VB.NET tutorial, ListBox sorting ascending and descending.We sort when the user clicks a button,toggling between ascending and descending. This is the plan :

  • We first fill up an array with data.
  • Then bind the array to our ListBox.
  • When the user clicks the sort button we sort ascending first.
  • When he clicks again,we sort descending and vice versa.

Here's the code :  

Public Class Form1

    'DECLARATIONS
    ReadOnly spacecrafts As String() = {"Kepler", "Casini", "Voyager", "New Horizon", "James Web", "Apollo 15", "Enterprise", "WMAP", "Spitzer", "Galileo"}
    Dim ascending As Boolean = True

    'WHEN FORM IS LOADED
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Populate()
    End Sub

    'POPULATE
    Private Sub Populate()
        ListBox1.Items.Clear()
        For Each s In spacecrafts
            ListBox1.Items.Add(s)
        Next
    End Sub

    'SORT
    Private Sub Sort(ByVal asc As Boolean)
        If asc Then
            Array.Sort(spacecrafts)
        Else
            Array.Reverse(spacecrafts)
        End If

        'FILL SORTED
        Populate()
    End Sub

    'SORT BTN CLICKED
    Private Sub sortBtn_Click(sender As Object, e As EventArgs) Handles sortBtn.Click
        Sort(ascending)
        ascending = Not ascending
    End Sub

End Class

Best Regards.

Comments