Jump to content

Listview header sorting


Info
 Share

Recommended Posts

  • Moderators

Info,

You need to use either the simple _GUICtrlListView_SimpleSort function or rather more complicated _GUICtrlListView_SortItems/_GUICtrlListView_RegisterSortCallBack combination from the GuiListView UDF. The examples in the Help file are pretty easy to follow. :)

M23

Edit: Typnig!

Edited by Melba23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

A more complex but faster way:

#Include <Date.au3>
#Include <GUIConstantsEx.au3>
#Include <GUIListView.au3>
#Include <GUIHeader.au3>
#Include <StructureConstants.au3>
#Include <WinAPI.au3>
#Include <WindowsConstants.au3>

$hProc = DllCallbackRegister('_SortProc', 'int', 'lparam;lparam;lparam')
$pProc = DllCallbackGetPtr($hProc)

$hForm = GUICreate('MyGUI', 400, 400)
$LV = GUICtrlCreateListView('Number|Date', 10, 10, 380, 380)
$hHdr = _GUICtrlListView_GetHeader(-1)

Dim $FT[100][3]

$tFT = DllStructCreate('dword;dword')
$tDT = DllStructCreate('uint64', DllStructGetPtr($tFT))
For $i = 0 To UBound($FT) - 1
    DllStructSetData($tFT, 2, Random(29400000, 30150000, 1))
    $FT[$i][0] = StringFormat('%.2f', Random(0, 9))
    $FT[$i][1] = DllStructGetData($tDT, 1)
    $FT[$i][2] = GUICtrlCreateListViewItem($FT[$i][0] & '|' & StringRegExpReplace(_Date_Time_FileTimeToStr($tFT), '\s.[^\s]*\z', ''), $LV)
Next

_GUICtrlHeader_SetItemFormat($hHdr, 0, BitOR(_GUICtrlHeader_GetItemFormat($hHdr, 0), $HDF_SORTUP))
_GUICtrlListView_SetSelectedColumn($LV, 0)
_Sort($LV, 0)

GUIRegisterMsg($WM_NOTIFY, 'WM_NOTIFY')
GUISetState()

While 1
    Switch GUIGetMsg()
        Case -3
            ExitLoop
    EndSwitch
WEnd

DllCallbackFree($hProc)

Func _Sort($CtrlID, $iSubItem)
    Return GUICtrlSendMsg($CtrlID, $LVM_SORTITEMS, $iSubItem, $pProc)
EndFunc   ;==>_Sort

Func _SortProc($iItem1, $iItem2, $iSubItem)

    Local $Format = _GUICtrlHeader_GetItemFormat($hHdr, $iSubItem)
    Local $Order

    Select
        Case BitAND($Format, $HDF_SORTDOWN)
            $Order =-1
        Case BitAND($Format, $HDF_SORTUP)
            $Order = 1
        Case Else
            Return 0
    EndSelect
    Select
        Case $FT[$iItem1 - $FT[0][2]][$iSubItem] > $FT[$iItem2 - $FT[0][2]][$iSubItem]
            Return  $Order
        Case $FT[$iItem1 - $FT[0][2]][$iSubItem] < $FT[$iItem2 - $FT[0][2]][$iSubItem]
            Return -$Order
        Case Else
            Return 0
    EndSelect
EndFunc   ;==>_SortProc

Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
    Switch $hWnd
        Case $hForm

            Local $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
            Local $CtrlID = DllStructGetData($tNMHDR, 'IDFrom')
            Local $Code = DllStructGetData($tNMHDR, 'Code')

            Switch $CtrlID
                Case $LV
                    Switch $Code
                        Case $LVN_COLUMNCLICK

                            Local $tNMLISTVIEW = DllStructCreate('hwnd hWndFrom;uint_ptr IDFrom;uint_ptr Code;int Item;int SubItem;uint NewState;uint OldState;uint Changed;int X;int Y;lparam Param', $lParam)
                            Local $SubItem = DllStructGetData($tNMLISTVIEW, 'SubItem')
                            Local $Format = _GUICtrlHeader_GetItemFormat($hHdr, $SubItem)

                            If Not BitAND($Format, BitOR($HDF_SORTDOWN, $HDF_SORTUP)) Then
                                For $i = 0 To 1
                                    _GUICtrlHeader_SetItemFormat($hHdr, $i, BitAND(_GUICtrlHeader_GetItemFormat($hHdr, $i), BitNOT(BitOR($HDF_SORTDOWN, $HDF_SORTUP))))
                                Next
                                Switch $SubItem
                                    Case 0 ; Number
                                        _GUICtrlHeader_SetItemFormat($hHdr, $SubItem, BitOR(BitAND($Format, BitNOT(BitOR($HDF_SORTDOWN, $HDF_SORTUP))), $HDF_SORTUP))
                                    Case 1 ; Date
                                        _GUICtrlHeader_SetItemFormat($hHdr, $SubItem, BitOR(BitAND($Format, BitNOT(BitOR($HDF_SORTDOWN, $HDF_SORTUP))), $HDF_SORTDOWN))
                                EndSwitch
                            Else
                                Select
                                    Case BitAND($Format, $HDF_SORTDOWN)
                                        _GUICtrlHeader_SetItemFormat($hHdr, $SubItem, BitOR(BitAND($Format, BitNOT($HDF_SORTDOWN)), $HDF_SORTUP))
                                    Case BitAND($Format, $HDF_SORTUP)
                                        _GUICtrlHeader_SetItemFormat($hHdr, $SubItem, BitOR(BitAND($Format, BitNOT($HDF_SORTUP)), $HDF_SORTDOWN))
                                EndSelect
                            EndIf
                            _GUICtrlListView_SetSelectedColumn($LV, $SubItem)
                            _Sort($LV, $SubItem)
                    EndSwitch
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...