VB.NET ListBox – Sort Ascending and Descending

VB.NET ListBox – Sort Ascending and Descending



Rating : 5/5 (1)




Introduction

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.vbVideo/PreviewDownload
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

 

VB.NET Examples S1E1 : ListBox - Sort Ascending/Descending

Coming soon.

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.



Rating :

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    five × 1 =

    COMMENTS