Performing Various Listview Control Tasks in VB.NET | Part 2 – Sorting and Shuffling or Randomizing Items

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 4.33 out of 5)
 

Part Two on performing various Listview Control tasks for Visual Basic.NET, VB 2008, Visual Basic 2010, and VB 2013.

The listview control has been available to VBers since the classic days. VB.NET continues with its version of the listview control as well. The control can provide VERY user-friendly features to your program. You can add Groups, Columns, and more with various display preferences like Icons and Details. The purpose of this post is simply to give you some basic code that i’ve learned/used over time for the Listview Control.

Note: These codes should have no problems working with Visual Basic.NET 2008, and Visual Basic 2010. For this article I am calling my Listview control’s name: Listview1. So all of the code will be using that name when referring to the listview object. I also set the controls ‘View’ property to: “Details” and added 2 columns.


Sorting Items…

I have a few tasks to show related to item based sorting. I will show how to Sort the Listviews item contents by Name, using Ascending and Descending styles. I want to show how to shuffle or randomize (or is it randomise?)  the items in a listview control. I will also show how to allow the user to click on one of the listviews columns to sort items both Asending and Desending,

Sorting Items – Ascending

            Listview1.Sorting = SortOrder.Ascending

Sorting Items – Descending

            Listview1.Sorting = SortOrder.Descending

Sorting Items – None (No Sorting at All)

            Listview1.Sorting = SortOrder.None

Shuffling, or Randomizing Items…

This source code will randomly sort all of the items in the listview control. There may be a more efficient way for this, but this code does work.

        '
        'This will go through the list contents and reorder the items randomly.
        '
        Dim r As New Random
        Dim item As ListViewItem
        Dim index As Integer
        '
        'You first need to set sorting to None.
        Listview1.Sorting = SortOrder.None
        '
        'Now go through the contents of the list.
        For i As Integer = 0 To Listview1.Items.Count - 1
            '
            'Get a randon number to use as the index to insert the item again.
            index = r.Next(i, Listview1.Items.Count)
            '
            'Set to each item in the list.
            item = Listview1.Items.Item(i)
            '
            'First remove that item.
            Listview1.Items.Remove(item)
            '
            'Then insert that item using the new random index number.
            Listview1.Items.Insert(index, item)

        Next

Sorting Items with a Column Click

OK, this last bit of code will sort the items when a user clicks on the column. You just need to make minor changes to the code below, depending on which column in the Index you want to activate the sorting process. The code Has to go in the Listview controls _ColumnClicked event.

    Private Sub Listview1_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles Listview1.ColumnClick
        '
        'These codes will check which sorting style is enabled and will either set to Ascending or Descending.
        'e.Column is the Index of the column that was clicked. I check for the first Column only which is 0 (Zero).
        '
        If e.Column = 0 AndAlso Listview1.Sorting = SortOrder.Descending OrElse Listview1.Sorting = SortOrder.None Then

            Listview1.Sorting = SortOrder.Ascending

        ElseIf e.Column = 0 AndAlso Listview1.Sorting = SortOrder.Ascending Then

            Listview1.Sorting = SortOrder.None

        End If

    End Sub

You see that it really is easy to do all of these tasks. I don’t exactly like the code I made on shuffling/randomizing the items but it works. Just not sure how good the performance would be with a thousand items listed. I haven’t decided yet on what to do for the next Listview Control article. Feel Free to leave a comment if you have something in mind.  Have fun!

Jason

Minor Revision: 2015

Leave a Reply

Your email address will not be published.