# Re:Answer for "listview 排序的问题"

Posted by sp on May 31, 1999 at 01:51:22:

In Reply to: listview 排序的问题... posted by Rong on May 30, 1999 at 16:36:20:

来源: 王国荣《Visual Basic 6.0实战讲座 》第11章pp.434-444

下列两个sub,分别为将c driver的档案置入listview object 及 mouse click title of listview object后自动排序，相信可以解决你的问题。不过先决的条件是的环境为visual basic 6.0,假如是visual basic 5.0，本人建议你放弃尝试，因为程式码实在非常长的。

Private Sub Form_Load()
Dim fs As New FileSystemObject
Dim fd As Folder
Dim f As File, Index As Integer
ListView1.View = lvwReport ' 「详细资料」检视模式
' 设定「详细资料」的抬头
ListView1.ColumnHeaders.Add , , "档案名称"
ListView1.ColumnHeaders.Add , , "大小", , lvwColumnRight
ListView1.ColumnHeaders.Add , , "类型"
ListView1.ColumnHeaders.Add , , "修改日期"

Set fd = fs.GetFolder("c:\")
Index = 1
' 逐一加入每一个档案
For Each f In fd.Files
' 名称
ListView1.ListItems.Add Index, , f.Name
' 大小
Dim Size As String
Size = Format(f.Size, "###,###,###,##0")
Size = Space(15 - Len(Size)) + Size
ListView1.ListItems(Index).SubItems(1) = Size
' 类型
ListView1.ListItems(Index).SubItems(2) = f.Type
' 修改日期
ListView1.ListItems(Index).SubItems(3) = f.DateLastModified
Index = Index + 1
Next
ListView1.Sorted = True
End Sub

' 排序
Private Sub ListView1_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader)
ListView1.SortKey = ColumnHeader.Index - 1
End Sub

Follow Ups: