I am trying to sort Columns in Listview. I have used Comparer in my code. I don't understand how to sort a Numerical column....(file.length, third column)
Any help is greatly appreciated.
Here is my code.
Thanks
Nick
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
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
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.
Friend WithEvents ListView1 As System.Windows.Forms.ListView
Friend WithEvents ReSize1 As LarcomAndYoung.Windows.Forms.ReSize
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container
Me.ListView1 = New System.Windows.Forms.ListView
Me.ReSize1 = New LarcomAndYoung.Windows.Forms.ReSize(Me.components)
Me.SuspendLayout()
'
'ListView1
'
Me.ListView1.Location = New System.Drawing.Point(56, 40)
Me.ListView1.Name = "ListView1"
Me.ListView1.Size = New System.Drawing.Size(688, 304)
Me.ListView1.TabIndex = 0
'
'ReSize1
'
Me.ReSize1.About = Nothing
Me.ReSize1.AutoCenterFormOnLoad = False
Me.ReSize1.Enabled = True
Me.ReSize1.HostContainer = Me
Me.ReSize1.InitialHostContainerHeight = 374
Me.ReSize1.InitialHostContainerWidth = 808
Me.ReSize1.Tag = Nothing
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(808, 374)
Me.Controls.Add(Me.ListView1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Fi As FileInfo
Dim Sitem As New ListViewItem
Dim File As String
ChDir("c:\temp")
ListView1.View = View.Details
ListView1.GridLines = True
ListView1.MultiSelect = False
ListView1.FullRowSelect = True
ListView1.AllowColumnReorder = True
ListView1.Columns.Add("File Name", 400, HorizontalAlignment.Left)
ListView1.Columns.Add("Date & time", 150, HorizontalAlignment.Left)
ListView1.Columns.Add("Size", 100, HorizontalAlignment.Left)
For Each File In Directory.GetFiles(Directory.GetCurrentDirectory)
Fi = New FileInfo(File)
Sitem = ListView1.Items.Add(Fi.Name)
Sitem.SubItems.Add(Fi.CreationTime)
Sitem.SubItems.Add(Fi.Length)
Next
End Sub
Private Sub ListView1_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles ListView1.ColumnClick
ListView1.ListViewItemSorter = New ListViewItemComparer(e.Column)
ListView1.Sort()
End Sub
End Class
Any help is greatly appreciated.
Here is my code.
Thanks
Nick
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
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
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.
Friend WithEvents ListView1 As System.Windows.Forms.ListView
Friend WithEvents ReSize1 As LarcomAndYoung.Windows.Forms.ReSize
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container
Me.ListView1 = New System.Windows.Forms.ListView
Me.ReSize1 = New LarcomAndYoung.Windows.Forms.ReSize(Me.components)
Me.SuspendLayout()
'
'ListView1
'
Me.ListView1.Location = New System.Drawing.Point(56, 40)
Me.ListView1.Name = "ListView1"
Me.ListView1.Size = New System.Drawing.Size(688, 304)
Me.ListView1.TabIndex = 0
'
'ReSize1
'
Me.ReSize1.About = Nothing
Me.ReSize1.AutoCenterFormOnLoad = False
Me.ReSize1.Enabled = True
Me.ReSize1.HostContainer = Me
Me.ReSize1.InitialHostContainerHeight = 374
Me.ReSize1.InitialHostContainerWidth = 808
Me.ReSize1.Tag = Nothing
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(808, 374)
Me.Controls.Add(Me.ListView1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Fi As FileInfo
Dim Sitem As New ListViewItem
Dim File As String
ChDir("c:\temp")
ListView1.View = View.Details
ListView1.GridLines = True
ListView1.MultiSelect = False
ListView1.FullRowSelect = True
ListView1.AllowColumnReorder = True
ListView1.Columns.Add("File Name", 400, HorizontalAlignment.Left)
ListView1.Columns.Add("Date & time", 150, HorizontalAlignment.Left)
ListView1.Columns.Add("Size", 100, HorizontalAlignment.Left)
For Each File In Directory.GetFiles(Directory.GetCurrentDirectory)
Fi = New FileInfo(File)
Sitem = ListView1.Items.Add(Fi.Name)
Sitem.SubItems.Add(Fi.CreationTime)
Sitem.SubItems.Add(Fi.Length)
Next
End Sub
Private Sub ListView1_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles ListView1.ColumnClick
ListView1.ListViewItemSorter = New ListViewItemComparer(e.Column)
ListView1.Sort()
End Sub
End Class