Sign in to follow this  
Followers 0
buymeapc

Retain ListView Sort After Refresh?

2 posts in this topic

Hey all,

I'm trying to figure out how to retain the sort order of a listview after the items have been refreshed; essentially, deleted and repopulated. It sounds like an easy thing to do, but I'm just hitting a roadblock. Here's a modified script from the help file where you may add lines to the listview, click the column header to sort and then click the refresh button and the sort order should retain...but it doesn't retain.

#include <GUIConstantsEx.au3>
#include <GUIListView.au3>
#include <WindowsConstants.au3>

Global $g__idListView
Global $g__aArrayOfData[1]
Global $g__bSortSense = False ; Set initial ascending sort
Global $g__iCount = 0

; Step 1: Click the Add Item button several times to create a list
; Step 2: Click one of the column headers to sort
; Step 3: Click the Refresh List button to delete all items (which are saved to an array) and repopulate with that array
; Step 4: Verify the list is still in the sorted order from before the Refresh button was clicked

Example()

Func Example()
    GUICreate("SimpleSort", 300, 300)
    $g__idListView = GUICtrlCreateListView("Item|Number", 10, 10, 280, 240)
    _GUICtrlListView_SetColumnWidth($g__idListView, 0, 200)
    Local $idButton_Add = GUICtrlCreateButton("Add Item", 10, 260, 80, 30)
    Local $idButton_Set = GUICtrlCreateButton("Refresh List", 110, 260, 180, 30)

    GUISetState(@SW_SHOW)

    GUIRegisterMsg($WM_NOTIFY, "_WM_NOTIFY")

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $idButton_Add
                Local $sData = Chr(Random(65, 90, 1))
                Local $sNum = Random(65, 90, 1)
                ReDim $g__aArrayOfData[$g__iCount + 1]
                $g__aArrayOfData[$g__iCount] = $sData & "|" & $sNum
                GUICtrlCreateListViewItem($g__aArrayOfData[$g__iCount], $g__idListView)
                _GUICtrlListView_SimpleSort($g__idListView, $g__bSortSense, 0, False) ; Prevent sort direction toggling for next insertion
                $g__iCount += 1
            Case $idButton_Set
                _GUICtrlListView_DeleteAllItems($g__idListView)
                For $i = 0 To UBound($g__aArrayOfData) - 1
                    GUICtrlCreateListViewItem($g__aArrayOfData[$i], $g__idListView)
                Next
        EndSwitch
    WEnd
EndFunc   ;==>Example

Func _WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam

    Local $hWndListView = $g__idListView
    If Not IsHWnd($g__idListView) Then $hWndListView = GUICtrlGetHandle($g__idListView)
    Local $tNMHDR = DllStructCreate($tagNMLISTVIEW, $lParam)
    Local $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    Local $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
        Case $hWndListView
            Switch $iCode
                Case $LVN_COLUMNCLICK ; A column was clicked
                    _GUICtrlListView_SimpleSort($hWndListView, $g__bSortSense, DllStructGetData($tNMHDR, "SubItem")) ; Sort direction for next sort toggled by default
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>_WM_NOTIFY

 

Share this post


Link to post
Share on other sites



You could sort the array while sorting the listview

Case $LVN_COLUMNCLICK ; A column was clicked
                     $subitem = DllStructGetData($tNMHDR, "SubItem")
                    _GUICtrlListView_SimpleSort($hWndListView, $g__bSortSense, $subitem) 
                    _ArraySort($g__aArrayOfData, not $g__bSortSense, 0, 0, $subitem)

Obviously it will work if the sorting is done in first column only, as in this sample code the array is 1D only

Share this post


Link to post
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
Sign in to follow this  
Followers 0