ListView custom sort : ListView Sort « GUI « VB.Net Tutorial






ListView custom sort
'Visual Basic 2005 Programmer's Reference
'by Rod Stephens (Author) 

'# Publisher: Wrox (October 21, 2005)
'# Language: English
'# ISBN-10: 0764571982
'# ISBN-13: 978-0764571985

Imports System.Windows.Forms
Imports System.Collections

public class ListViewCustomSort
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class
Public Class Form1
    ' The column currently used for sorting.
    Private m_SortingColumn As ColumnHeader

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Make the ListView column headers.
        ListViewMakeColumnHeaders(lvwBooks, _
            "Title", HorizontalAlignment.Left, 120, _
            "URL", HorizontalAlignment.Left, 120, _
            "ISBN", HorizontalAlignment.Left, 90, _
            "Picture", HorizontalAlignment.Left, 120, _
            "Pages", HorizontalAlignment.Right, 50, _
            "Year", HorizontalAlignment.Right, 40)

        ListViewMakeRow(lvwBooks, 0, _
            "Visual Basic and XML", _
            "http://www.vb-helper.com/xml.htm", _
            "0-471-12060-X", _
            "http://www.vb-helper.com/xml.jpg", _
            "503", _
            "2002")
        ListViewMakeRow(lvwBooks, 0, _
            "Visual Basic Graphics Programming, 2e", _
            "http://www.vb-helper.com/vbgp.htm", _
            "0-471-35599-2", _
            "http://www.vb-helper.com/vbgp.jpg", _
            "712", _
            "2000")
        ListViewMakeRow(lvwBooks, 0, _
            "Ready-to-Run Visual Basic Algorithms", _
            "http://www.vb-helper.com/vba.htm", _
            "0-471-24268-3", _
            "http://www.vb-helper.com/vba.jpg", _
            "395", _
            "1998")

        ListViewSizeColumns(lvwBooks, True)
        Text = "RunTimeListView (" & lvwBooks.View.ToString & ")"
    End Sub

    ' Make the ListView's column headers.
    ' The ParamArray entries should be triples holding
    ' column title, HorizontalAlignment value, and width.
    Private Sub ListViewMakeColumnHeaders(ByVal lvw As ListView, ByVal ParamArray header_info() As Object)
        ' Remove any existing headers.
        lvw.Columns.Clear()

        ' Make the column headers.
        For i As Integer = header_info.GetLowerBound(0) To header_info.GetUpperBound(0) Step 3
            Dim col_header As ColumnHeader = lvw.Columns.Add( _
                DirectCast(header_info(i), String), _
                -1, _
                DirectCast(header_info(i + 1), HorizontalAlignment))
            col_header.Width = DirectCast(header_info(i + 2), Integer)
        Next i
    End Sub

    ' Make a ListView row.
    Private Sub ListViewMakeRow(ByVal lvw As ListView, ByVal image_index As Integer, ByVal item_title As String, ByVal ParamArray subitem_titles() As String)
        ' Make the item.
        Dim new_item As ListViewItem = lvw.Items.Add(item_title)
        new_item.ImageIndex = image_index

        ' Make the sub-items.
        For i As Integer = subitem_titles.GetLowerBound(0) To subitem_titles.GetUpperBound(0)
            new_item.SubItems.Add(subitem_titles(i))
        Next i
    End Sub

    ' Set column widths to -1 to fit data,
    ' -2 to fit data and header.
    Private Sub ListViewSizeColumns(ByVal lvw As ListView, ByVal allow_room_for_header As Boolean)
        Dim new_wid As Integer = -1
        If allow_room_for_header Then new_wid = -2

        ' Set the width for each column.
        For i As Integer = 0 To lvw.Columns.Count - 1
            lvw.Columns(i).Width = new_wid
        Next i
    End Sub

    Private Sub mnuViewDetails_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuViewDetails.Click
        lvwBooks.View = View.Details
        Text = "RunTimeListView (" & lvwBooks.View.ToString & ")"
    End Sub

    Private Sub mnuViewLargeIcons_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuViewLargeIcons.Click
        lvwBooks.View = View.LargeIcon
        Text = "RunTimeListView (" & lvwBooks.View.ToString & ")"
    End Sub

    Private Sub mnuViewList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuViewList.Click
        lvwBooks.View = View.List
        Text = "RunTimeListView (" & lvwBooks.View.ToString & ")"
    End Sub

    Private Sub mnuViewSmallIcons_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuViewSmallIcons.Click
        lvwBooks.View = View.SmallIcon
        Text = "RunTimeListView (" & lvwBooks.View.ToString & ")"
    End Sub

    Private Sub mnuViewTile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuViewTile.Click
        lvwBooks.View = View.Tile
        Text = "RunTimeListView (" & lvwBooks.View.ToString & ")"
    End Sub

    ' Sort using the clicked column.
    Private Sub lvwBooks_ColumnClick(ByVal sender As System.Object, _
     ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles lvwBooks.ColumnClick
        ' Get the new sorting column.
        Dim new_sorting_column As ColumnHeader = _
            lvwBooks.Columns(e.Column)

        ' Figure out the new sorting order.
        Dim sort_order As System.Windows.Forms.SortOrder
        If m_SortingColumn Is Nothing Then
            ' New column. Sort ascending.
            sort_order = SortOrder.Ascending
        Else
            ' See if this is the same column.
            If new_sorting_column.Equals(m_SortingColumn) Then
                ' Same column. Switch the sort order.
                If m_SortingColumn.Text.StartsWith("> ") Then
                    sort_order = SortOrder.Descending
                Else
                    sort_order = SortOrder.Ascending
                End If
            Else
                ' New column. Sort ascending.
                sort_order = SortOrder.Ascending
            End If

            ' Remove the old sort indicator.
            m_SortingColumn.Text = m_SortingColumn.Text.Substring(2)
        End If

        ' Display the new sort order.
        m_SortingColumn = new_sorting_column
        If sort_order = SortOrder.Ascending Then
            m_SortingColumn.Text = "> " & m_SortingColumn.Text
        Else
            m_SortingColumn.Text = "< " & m_SortingColumn.Text
        End If

        ' Create a comparer.
        lvwBooks.ListViewItemSorter = New ListViewComparer(e.Column, sort_order)

        ' Sort.
        lvwBooks.Sort()
    End Sub

End Class

' Implements a comparer for ListView columns.
Public Class ListViewComparer
    Implements IComparer

    Private m_ColumnNumber As Integer
    Private m_SortOrder As SortOrder

    Public Sub New(ByVal column_number As Integer, ByVal sort_order As SortOrder)
        m_ColumnNumber = column_number
        m_SortOrder = sort_order
    End Sub

    ' Compare the items in the appropriate column
    ' for objects x and y.
    Public Function Compare(ByVal x As Object, ByVal y As Object) _
     As Integer Implements System.Collections.IComparer.Compare
        Dim item_x As ListViewItem = DirectCast(x, ListViewItem)
        Dim item_y As ListViewItem = DirectCast(y, ListViewItem)

        ' Get the sub-item values.
        Dim string_x As String
        If item_x.SubItems.Count <= m_ColumnNumber Then
            string_x = ""
        Else
            string_x = item_x.SubItems(m_ColumnNumber).Text
        End If

        Dim string_y As String
        If item_y.SubItems.Count <= m_ColumnNumber Then
            string_y = ""
        Else
            string_y = item_y.SubItems(m_ColumnNumber).Text
        End If

        ' Compare them.
        If m_SortOrder = SortOrder.Ascending Then
            Return String.Compare(string_x, string_y)
        Else
            Return String.Compare(string_y, string_x)
        End If
    End Function
End Class
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Public Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Me.MenuStrip1 = New System.Windows.Forms.MenuStrip
        Me.ViewToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem
        Me.mnuViewDetails = New System.Windows.Forms.ToolStripMenuItem
        Me.mnuViewLargeIcons = New System.Windows.Forms.ToolStripMenuItem
        Me.mnuViewList = New System.Windows.Forms.ToolStripMenuItem
        Me.mnuViewSmallIcons = New System.Windows.Forms.ToolStripMenuItem
        Me.mnuViewTile = New System.Windows.Forms.ToolStripMenuItem
        Me.imlSmall = New System.Windows.Forms.ImageList(Me.components)
        Me.lvwBooks = New System.Windows.Forms.ListView
        Me.ColumnHeader3 = New System.Windows.Forms.ColumnHeader
        Me.ColumnHeader2 = New System.Windows.Forms.ColumnHeader
        Me.ColumnHeader1 = New System.Windows.Forms.ColumnHeader
        Me.MenuStrip1.SuspendLayout()
        Me.SuspendLayout()
        '
        'MenuStrip1
        '
        Me.MenuStrip1.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.ViewToolStripMenuItem})
        Me.MenuStrip1.Location = New System.Drawing.Point(0, 0)
        Me.MenuStrip1.Name = "MenuStrip1"
        Me.MenuStrip1.Size = New System.Drawing.Size(772, 24)
        Me.MenuStrip1.TabIndex = 1
        Me.MenuStrip1.Text = "MenuStrip1"
        '
        'ViewToolStripMenuItem
        '
        Me.ViewToolStripMenuItem.DropDownItems.AddRange(New System.Windows.Forms.ToolStripItem() {Me.mnuViewDetails, Me.mnuViewLargeIcons, Me.mnuViewList, Me.mnuViewSmallIcons, Me.mnuViewTile})
        Me.ViewToolStripMenuItem.Name = "ViewToolStripMenuItem"
        Me.ViewToolStripMenuItem.Text = "&View"
        '
        'mnuViewDetails
        '
        Me.mnuViewDetails.Name = "mnuViewDetails"
        Me.mnuViewDetails.Text = "&Details"
        '
        'mnuViewLargeIcons
        '
        Me.mnuViewLargeIcons.Name = "mnuViewLargeIcons"
        Me.mnuViewLargeIcons.Text = "Large Icons"
        '
        'mnuViewList
        '
        Me.mnuViewList.Name = "mnuViewList"
        Me.mnuViewList.Text = "&List"
        '
        'mnuViewSmallIcons
        '
        Me.mnuViewSmallIcons.Name = "mnuViewSmallIcons"
        Me.mnuViewSmallIcons.Text = "&Small Icons"
        '
        'mnuViewTile
        '
        Me.mnuViewTile.Name = "mnuViewTile"
        Me.mnuViewTile.Text = "Tile"
        '
        'lvwBooks
        '
        Me.lvwBooks.Columns.AddRange(New System.Windows.Forms.ColumnHeader() {Me.ColumnHeader1, Me.ColumnHeader2, Me.ColumnHeader3})
        Me.lvwBooks.Dock = System.Windows.Forms.DockStyle.Fill
        Me.lvwBooks.Location = New System.Drawing.Point(0, 24)
        Me.lvwBooks.Name = "lvwBooks"
        Me.lvwBooks.Size = New System.Drawing.Size(772, 249)
        Me.lvwBooks.SmallImageList = Me.imlSmall
        Me.lvwBooks.TabIndex = 2
        Me.lvwBooks.View = System.Windows.Forms.View.Details
        '
        'ColumnHeader2
        '
        Me.ColumnHeader2.Text = "URL"
        '
        'ColumnHeader1
        '
        Me.ColumnHeader1.Text = "Title"
        '
        'Form1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(772, 273)
        Me.Controls.Add(Me.lvwBooks)
        Me.Controls.Add(Me.MenuStrip1)
        Me.Name = "Form1"
        Me.Text = "ListViewCustomSort"
        Me.MenuStrip1.ResumeLayout(False)
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub
    Friend WithEvents MenuStrip1 As System.Windows.Forms.MenuStrip
    Friend WithEvents ViewToolStripMenuItem As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents mnuViewDetails As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents mnuViewLargeIcons As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents mnuViewList As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents mnuViewSmallIcons As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents mnuViewTile As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents imlSmall As System.Windows.Forms.ImageList
    Friend WithEvents lvwBooks As System.Windows.Forms.ListView
    Friend WithEvents ColumnHeader3 As System.Windows.Forms.ColumnHeader
    Friend WithEvents ColumnHeader2 As System.Windows.Forms.ColumnHeader
    Friend WithEvents ColumnHeader1 As System.Windows.Forms.ColumnHeader

End Class








14.16.ListView Sort
14.16.1.ListView custom sortListView custom sort
14.16.2.Sort a listviewSort a listview