1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
'classe perso
' Implemente le tri manual personnalise des items par colonnes.
Class ListViewItemComparer
Implements IComparer
Private col As Integer
Public Sub New()
col = 0
End Sub
Public Sub New(ByVal column As Integer)
col = column
End Sub
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer _
Implements IComparer.Compare
Return [String].Compare(CType(x, ListViewItem).SubItems(col).Text, CType(y, ListViewItem).SubItems(col).Text)
End Function
End Class
'code de la forme qui l'utilise
Imports System
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Collections
Public Class ListViewSortForm
Inherits Form
Private listView1 As ListView
Public Sub New()
'Initialise le ListView .
Me.listView1 = New ListView
' met le type de tri initial à aucun.
Me.listView1.Sorting = SortOrder.None
' Desactive tri automatique pour activer le mode manual (click colonne).
Me.listView1.View = View.Details
' ici remplir le listview.....
' Connecter le ListView.ColumnClick event à son event handler ColumnClick
AddHandler Me.listView1.ColumnClick, AddressOf ColumnClic
End Sub 'New
' ColumnClick event handler.
Private Sub ColumnClick(ByVal o As Object, ByVal e As ColumnClickEventArgs)
' Set the ListViewItemSorter property to a new ListViewItemComparer
' Setting this property immediately sorts the
' ListView using the ListViewItemComparer object.
Me.listView1.ListViewItemSorter = New ListViewItemComparer(e.Column)
End Sub
End Class |
Partager