1'
2' Visual Basic.Net Compiler
3' Copyright (C) 2004 - 2010 Rolf Bjarne Kvinge, RKvinge@novell.com
4'
5' This library is free software; you can redistribute it and/or
6' modify it under the terms of the GNU Lesser General Public
7' License as published by the Free Software Foundation; either
8' version 2.1 of the License, or (at your option) any later version.
9'
10' This library is distributed in the hope that it will be useful,
11' but WITHOUT ANY WARRANTY; without even the implied warranty of
12' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13' Lesser General Public License for more details.
14'
15' You should have received a copy of the GNU Lesser General Public
16' License along with this library; if not, write to the Free Software
17' Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18'
19
20' Implements the manual sorting of items by columns.
21Class ListViewItemComparer
22    Implements IComparer
23
24    Private m_Column As Integer
25    Private m_Ascending As Boolean = True
26
27    Private WithEvents m_List As ListView
28
29    Public Sub New(Optional ByVal List As ListView = Nothing)
30        m_Column = 0
31        m_List = List
32    End Sub
33
34    Public Sub New(ByVal column As Integer, ByVal Ascending As Boolean)
35        m_Column = column
36        m_Ascending = Ascending
37    End Sub
38
39    Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
40        Dim asc As Integer = CInt(IIf(m_Ascending, 1, -1))
41        Return [String].Compare(CType(x, ListViewItem).SubItems(m_Column).Text, CType(y, ListViewItem).SubItems(m_Column).Text) * CInt(asc)
42    End Function
43
44    Private Sub m_List_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles m_List.ColumnClick
45        Try
46            Dim header As ColumnHeader
47            Dim list As ListView = DirectCast(sender, ListView)
48
49            header = list.Columns(e.Column)
50            If e.Column = m_Column Then
51                m_Ascending = Not m_Ascending
52            Else
53                m_Column = e.Column
54                m_Ascending = True
55            End If
56
57            list.ListViewItemSorter = Me
58            list.Sort()
59        Catch ex As Exception
60            MsgBox(ex.Message & vbNewLine & ex.StackTrace)
61        End Try
62    End Sub
63End Class
64