by Bradley Plies (2 Submissions)
Category: OLE/COM/DCOM/Active-X
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Tue 8th May 2001
Date Added: Mon 8th February 2021
Rating: (1 Votes)
Sorts a ListView Control in Ascending or Descending order based on the column header that is clicked. You need to find Up and Down arrow icons.
'Procedure:
' SortListView
'
'Purpose:
' Sorts a given ListView Control based on the a column header that
' is clicked
'
'Inputs:
' List // Reference to a ListView Control to sort
' ColHeadIndex // Index of the Column Header that was clicked
'
'Outputs:
' No return value
' The given ListView Control is sorted by the given column in ascending or descending order
'
'Assumptions:
' The given ListView Control has its ColumnHeaderIcon set to an Image
' collection for example:
' Set lstQueryResults.ColumnHeaderIcons = ImageList1
'
' Within this image set, it is assumed you have an icon named
' "UpArrow" and "DownArrow"
'
'Notes:
' In ListView Controls all data is treated as text, so be
' mindful of sorting number columns because "10" < "9"
'
'Example Usage:
' Private Sub ListView1_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader)
' Call SortListView(ListView1, ColumnHeader.Index)
' End Sub
Public Sub SortListView(ByRef List As ListView, ColHeadIndex As Integer)
Dim lcv As Long 'Loop Control Variable
With List
' Make sure the Sorted property is set to true
.Sorted = True
' Sort according to the colum that was clicked (off by one)
.SortKey = ColHeadIndex - 1
' Does the column already have an icon?
If .ColumnHeaders(ColHeadIndex).Icon = 0 Then
'No, So we will assume this column is not sorted
' Set to Ascending order
.SortOrder = lvwAscending
' Set the ColumnHeader to be the Up Arrow
.ColumnHeaders(ColHeadIndex).Icon = "UpArrow"
' Does the column have an UpArrow icon?
ElseIf .ColumnHeaders(ColHeadIndex).Icon = "UpArrow" Then
' Yes, So the column is in Ascending order, switch to descending
' Set the Column Icon to the Down Arrow
.ColumnHeaders(ColHeadIndex).Icon = "DownArrow"
' Set the sort order to descending
.SortOrder = lvwDescending
Else
' Otherwise sort into ascending order
' Set to Ascending order
.SortOrder = lvwAscending
' Set the ColumnHeader to be the Up Arrow
.ColumnHeaders(ColHeadIndex).Icon = "UpArrow"
End If
' Remove any icon (presumably an arrow icon) from all other columns
' For every Column in the ListView Control...
For lcv = 1 To List.ColumnHeaders.Count
' Is the current column the clicked column?
If Not (lcv = ColHeadIndex) Then
' No, remove any icon it may have
.ColumnHeaders(lcv).Icon = 0
End If
Next lcv
' Refresh the display of the ListView Control
.Refresh
End With
End Sub
No comments have been posted about Sorts a ListView Control in Ascending or Descending order based on the column header that is clicke. Why not be the first to post a comment about Sorts a ListView Control in Ascending or Descending order based on the column header that is clicke.