VB.NET LListBox - Sorting Data in Ascending and Descending Manner

| Page Views: 1820 vb.net data sorting

Sorting data is such important operation when it comes to dealing with modern software. It helps the user easily reach the specific data he needs without necessarily having to go through the whole dataset line by line. This is a VB.NET tutorial.

We see how to sort a listbox data in an ascending or descending order. This is a very basic example. We simply use an Array to sort and reverse our data. The array is then bound to a listbox.

Questions this Examples helps answer.

  • How to sort data in ascending and descending manner.
  • How to sort data in vb.net listbox.
  • How to sort and reverse an array in vb.net.
  • VB.NET ListBox data sorting example.

Asssumptions.

We assume that you can drag and drop a listbox into a winform project. Just drag a listbox and a button that shall sort our data in ascending and descending manner. We also used the metro theme in our project. There is a tutorial on how to install a metro theme here.

Screenshot

  • Here's the screenshot of the project.

VB.NET ListBox Sorting Ascending and Descending.

  • We can insert,select, update and delete data to and from ms access database.

VB-NET ListBox Sort

Source Code

Lets have a look at the source code.

Form1.vb

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

 

Video/Preview

https://www.youtube.com/watch?v=XD9O0sgSKEo

How To Run

  1. Download the project above.
  2. You'll get a compressed file. Unzip it.
  3. Open Visual Studio.
  4. Go to File --> Open --> Project/Solution.
  5. Open the project in visual studio.
  6. That's it.

Conclusion

We've seen how to sort a listbox in both an ascending and descending manner.

More

YouTube

  • Visit our channel for more examples like these.

Facebook

Oclemy,Cheers.

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section.

About Me.

After completing his Software Engineering bachelors program, Oclemy(Clement Ochieng) these days is a man of two lives. At day he works for a startup in Nairobi, Kenya. At night he works tirelessly on building ProgrammingWizards TV, a tv channel for student coders and this website to help share the source code. In between he practices Meditation and Self actualization to help him keep balance. He also likes going for long solo walks to connect more with nature.




Recommendations


What do You Think


Previous Post Next Post