Hej jag undrar hur man sorterar data i ListView funktionen på samma sätt som man gör i utforskaren. Alltså genom att clicka på de gråa rubrikerna. Följande hittade jag i hjälptexten för SortKey... Här kommer en kodsnutt jag brukar användaSortera i listview kontrollen...
Tack på förhandSv: Sortera i listview kontrollen...
<code>
Private Sub ListView1_ColumnClick(ByVal ColumnHeader As ColumnHeader)
' When a ColumnHeader object is clicked, the ListView control is
' sorted by the subitems of that column.
' Set the SortKey to the Index of the ColumnHeader - 1
ListView1.SortKey = ColumnHeader.Index - 1
' Set Sorted to True to sort the list.
ListView1.Sorted = True
End Sub
</code>
Det ger nästan samma effekt, i Utforskaren kan du ändra sorteringen från stigande till fallande och tillbaka igen genom att klicka flera gånger på rubriken. DET fungerar inte i exemplet ovan, men det går väl också att fixa antar jag...Sv: Sortera i listview kontrollen...
Modul:
Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2001 VBnet, Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' You are free to use this code within your own applications,
' but you are expressly forbidden from selling or otherwise
' distributing this source code without prior written consent.
' This includes both posting free demo projects made from this
' code as well as reproducing the code in text or html format.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public objFind As LV_FINDINFO
Public objItem As LV_ITEM
'variable to hold the sort order (ascending or descending)
Public sOrder As Boolean
Public Type POINTAPI
x As Long
y As Long
End Type
Public Type LV_FINDINFO
flags As Long
psz As String
lParam As Long
pt As POINTAPI
vkDirection As Long
End Type
Public Type LV_ITEM
Mask As Long
iItem As Long
iSubItem As Long
state As Long
stateMask As Long
pszText As String
cchTextMax As Long
iImage As Long
lParam As Long
iIndent As Long
End Type
'Constants
Public Const LVFI_PARAM As Long = &H1
Public Const LVIF_TEXT As Long = &H1
Public Const LVM_FIRST As Long = &H1000
Public Const LVM_FINDITEM As Long = (LVM_FIRST + 13)
Public Const LVM_GETITEMTEXT As Long = (LVM_FIRST + 45)
Public Const LVM_SORTITEMS As Long = (LVM_FIRST + 48)
'API declarations
Public Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long
Public Function CompareDates(ByVal lParam1 As Long, _
ByVal lParam2 As Long, _
ByVal hwnd As Long) As Long
'CompareDates: This is the sorting routine that gets passed to the
'ListView control to provide the comparison test for date values.
'Compare returns:
' 0 = Less Than
' 1 = Equal
' 2 = Greater Than
Dim dDate1 As Date
Dim dDate2 As Date
'Obtain the item names and dates corresponding to the
'input parameters
dDate1 = ListView_GetItemDate(hwnd, lParam1)
dDate2 = ListView_GetItemDate(hwnd, lParam2)
'based on the Public variable sOrder set in the
'columnheader click sub, sort the dates appropriately:
Select Case sOrder
Case True: 'sort descending
If dDate1 < dDate2 Then
CompareDates = 0
ElseIf dDate1 = dDate2 Then
CompareDates = 1
Else: CompareDates = 2
End If
Case Else: 'sort ascending
If dDate1 > dDate2 Then
CompareDates = 0
ElseIf dDate1 = dDate2 Then
CompareDates = 1
Else: CompareDates = 2
End If
End Select
End Function
Public Function CompareValues(ByVal lParam1 As Long, _
ByVal lParam2 As Long, _
ByVal hwnd As Long) As Long
'CompareValues: This is the sorting routine that gets passed to the
'ListView control to provide the comparison test for numeric values.
'Compare returns:
' 0 = Less Than
' 1 = Equal
' 2 = Greater Than
Dim val1 As Long
Dim val2 As Long
'Obtain the item names and values corresponding
'to the input parameters
val1 = ListView_GetItemValueStr(hwnd, lParam1)
val2 = ListView_GetItemValueStr(hwnd, lParam2)
'based on the Public variable sOrder set in the
'columnheader click sub, sort the values appropriately:
Select Case sOrder
Case True: 'sort descending
If val1 < val2 Then
CompareValues = 0
ElseIf val1 = val2 Then
CompareValues = 1
Else: CompareValues = 2
End If
Case Else: 'sort ascending
If val1 > val2 Then
CompareValues = 0
ElseIf val1 = val2 Then
CompareValues = 1
Else: CompareValues = 2
End If
End Select
End Function
Public Function ListView_GetItemDate(hwnd As Long, lParam As Long) As Date
Dim hIndex As Long
Dim R As Long
'Convert the input parameter to an index in the list view
objFind.flags = LVFI_PARAM
objFind.lParam = lParam
hIndex = SendMessage(hwnd, LVM_FINDITEM, -1, objFind)
'Obtain the value of the specified list view item.
'The objItem.iSubItem member is set to the index
'of the column that is being retrieved.
objItem.Mask = LVIF_TEXT
'objItem.iSubItem = 4
objItem.pszText = Space$(32)
objItem.cchTextMax = Len(objItem.pszText)
'get the string at subitem 1
'and convert it into a date and exit
R = SendMessage(hwnd, LVM_GETITEMTEXT, hIndex, objItem)
If R > 0 Then
ListView_GetItemDate = CDate(Left$(objItem.pszText, R))
End If
End Function
Public Function ListView_GetItemValueStr(hwnd As Long, lParam As Long) As Long
Dim hIndex As Long
Dim R As Long
'Convert the input parameter to an index in the list view
objFind.flags = LVFI_PARAM
objFind.lParam = lParam
hIndex = SendMessage(hwnd, LVM_FINDITEM, -1, objFind)
'Obtain the value of the specified list view item.
'The objItem.iSubItem member is set to the index
'of the column that is being retrieved.
objItem.Mask = LVIF_TEXT
'objItem.iSubItem = 3
objItem.pszText = Space$(32)
objItem.cchTextMax = Len(objItem.pszText)
'get the string at subitem 2
'and convert it into a long
R = SendMessage(hwnd, LVM_GETITEMTEXT, hIndex, objItem)
If R > 0 Then
ListView_GetItemValueStr = CLng(Left$(objItem.pszText, R))
End If
End Function
Public Function FARPROC(ByVal pfn As Long) As Long
'A procedure that receives and returns
'the value of the AddressOf operator.
'This workaround is needed as you can't assign
'AddressOf directly to an API when you are also
'passing the value ByVal in the statement
'(as is being done with SendMessage)
FARPROC = pfn
End Function
Form:
Private Sub ListView1_ColumnClick(ByVal ColumnHeader As ComctlLib.ColumnHeader)
'toggle the sort order for use in the CompareXX routines
sOrder = Not sOrder
ListView1.SortKey = ColumnHeader.Index - 1
Select Case ColumnHeader.Index - 1
Case 0, 1, 2, 6, 7, 8 'Sorterar kolumn 0,1,2,6,7,8 efter alfabetet
'Use default sorting to sort the items in the list
ListView1.SortKey = ColumnHeader.Index - 1
ListView1.SortOrder = Abs(sOrder) '=Abs(Not ListView1.SortOrder = 1)
ListView1.Sorted = True
Case 5: 'sorterar kolumn 5 efter datum
'Use sort routine to sort by date
ListView1.Sorted = False
objItem.iSubItem = ColumnHeader.Index - 1
SendMessage ListView1.hwnd, _
LVM_SORTITEMS, _
ListView1.hwnd, _
ByVal FARPROC(AddressOf CompareDates)
Case 3, 4, 9 'sorterar kolumn 3,4,9 efter sifferordning
'Use sort routine to sort by value
ListView1.Sorted = False
objItem.iSubItem = ColumnHeader.Index - 1
SendMessage ListView1.hwnd, _
LVM_SORTITEMS, _
ListView1.hwnd, _
ByVal FARPROC(AddressOf CompareValues)
End Select
End Sub
Lycka till
Johan