Jump to content

Sorting Listview with Checkboxes


Recommended Posts

Hi,

I've some problems sorting listview which contains checkboxes. I figured out that a listview with checkboxes can't be sorted by _GUICtrlListView_SimpleSort. So I took a look at _GUICtrlListView_SortItems. The example in the helpfile works fine, but I cant get it to work if the listview contains no images. I modified the code from helpfile to get closer to this behavior. After all I had a listview which fires _GUICtrlListView_SortItems, shows up arrows in the columheader but isn''t sorted.

This is the code (modified code from the helpfile)

CODE
#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6

#include <GUIConstantsEx.au3>

#include <GuiListView.au3>

#include <GuiImageList.au3>

#include <WindowsConstants.au3>

Opt('MustDeclareVars', 1)

Global $hListView, $hListView2

_Example1()

_Example2()

Func _Example1()

Local $hImage, $aIcons[3] = [0, 3, 6]

Local $iExWindowStyle = BitOR($WS_EX_DLGMODALFRAME, $WS_EX_CLIENTEDGE)

Local $iExListViewStyle = BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES, $LVS_EX_GRIDLINES, $LVS_EX_CHECKBOXES, $LVS_EX_DOUBLEBUFFER)

GUICreate("ListView Sort", 300, 200)

$hListView = GUICtrlCreateListView("Column1|Col2|Col3", 10, 10, 280, 180, -1, $iExWindowStyle)

_GUICtrlListView_SetExtendedListViewStyle($hListView, $iExListViewStyle)

_AddRow($hListView, "ABC|000666|10.05.2004")

_AddRow($hListView, "DEF|444|11.05.2005")

_AddRow($hListView, "CDE|555|12.05.2004")

GUISetState()

_GUICtrlListView_RegisterSortCallBack($hListView)

While 1

Switch GUIGetMsg()

Case $GUI_EVENT_CLOSE

ExitLoop

Case $hListView

; Kick off the sort callback

_GUICtrlListView_SortItems($hListView, GUICtrlGetState($hListView))

EndSwitch

WEnd

_GUICtrlListView_UnRegisterSortCallBack($hListView)

GUIDelete()

EndFunc ;==>_Example1

Func _Example2()

Local $hImage, $aIcons[3] = [0, 3, 6]

Local $iExWindowStyle = BitOR($WS_EX_DLGMODALFRAME, $WS_EX_CLIENTEDGE)

Local $iExListViewStyle = BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_GRIDLINES, $LVS_EX_CHECKBOXES, $LVS_EX_DOUBLEBUFFER)

GUICreate("ListView Sort Treat Numbers as Strings", 300, 200, 100)

$hListView = GUICtrlCreateListView("Column1|Col2|Col3", 10, 10, 280, 180, -1, $iExWindowStyle)

_GUICtrlListView_SetExtendedListViewStyle($hListView, $iExListViewStyle)

; Load images

_AddRow($hListView, "ABC|000666|10.05.2004")

_AddRow($hListView, "DEF|444|11.05.2005")

_AddRow($hListView, "CDE|555|12.05.2004")

GUISetState()

GUICreate("ListView Sort Treat Numbers as Numbers", 300, 200, 400)

$hListView2 = GUICtrlCreateListView("Column1|Col2|Col3", 10, 10, 280, 180, -1, $iExWindowStyle)

_GUICtrlListView_SetExtendedListViewStyle($hListView2, $iExListViewStyle)

_GUICtrlListView_SetImageList($hListView2, $hImage, 1)

_AddRow($hListView2, "ABC|000666|10.05.2004")

_AddRow($hListView2, "DEF|444|11.05.2005")

_AddRow($hListView2, "CDE|555|12.05.2004")

GUISetState()

_GUICtrlListView_RegisterSortCallBack($hListView, False)

_GUICtrlListView_RegisterSortCallBack($hListView2)

GUIRegisterMsg($WM_NOTIFY, "_WM_NOTIFY")

While 1

Switch GUIGetMsg()

Case $GUI_EVENT_CLOSE

ExitLoop

EndSwitch

WEnd

_GUICtrlListView_UnRegisterSortCallBack($hListView)

_GUICtrlListView_UnRegisterSortCallBack($hListView2)

GUIRegisterMsg($WM_NOTIFY, "")

EndFunc ;==>_Example2

Func _AddRow($hWnd, $sItem)

Local $aItem = StringSplit($sItem, "|")

Local $iIndex = _GUICtrlListView_AddItem($hWnd, $aItem[1])

_GUICtrlListView_SetColumnWidth($hWnd, 0, $LVSCW_AUTOSIZE_USEHEADER)

For $x = 2 To $aItem[0]

_GUICtrlListView_AddSubItem($hWnd, $iIndex, $aItem[$x], $x - 1)

_GUICtrlListView_SetColumnWidth($hWnd, $x - 1, $LVSCW_AUTOSIZE)

Next

EndFunc ;==>_AddRow

Func _WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)

#forceref $hWnd, $iMsg, $iwParam

Local $hWndFrom, $iCode, $tNMHDR, $hWndListView, $hWndListView2

$hWndListView = $hListView

$hWndListView2 = $hListView2

If Not IsHWnd($hListView) Then $hWndListView = GUICtrlGetHandle($hListView)

If Not IsHWnd($hListView2) Then $hWndListView2 = GUICtrlGetHandle($hListView2)

$tNMHDR = DllStructCreate($tagNMHDR, $ilParam)

$hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))

$iCode = DllStructGetData($tNMHDR, "Code")

Switch $hWndFrom

Case $hWndListView, $hWndListView2

Switch $iCode

Case $LVN_COLUMNCLICK ; A column was clicked

Local $tInfo = DllStructCreate($tagNMLISTVIEW, $ilParam)

; Kick off the sort callback

_GUICtrlListView_SortItems($hWndFrom, DllStructGetData($tInfo, "SubItem"))

; No return value

EndSwitch

EndSwitch

Return $__LISTVIEWCONSTANT_GUI_RUNDEFMSG

EndFunc ;==>_WM_NOTIFY

Can anyone take a look at the code and help me. I can't figure out why the listview isn't sorted.

Many thanks in advance

Paul

Link to comment
Share on other sites

Add a $iParam - Application Defined data in a _GUICtrlListView_AddItem() function.

#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <GuiImageList.au3>
#include <WindowsConstants.au3>

Global $hListView, $hListView2

_Example1()
_Example2()

Func _Example1()
Local $iExWindowStyle = BitOR($WS_EX_DLGMODALFRAME, $WS_EX_CLIENTEDGE)
Local $iExListViewStyle = BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES, $LVS_EX_GRIDLINES, $LVS_EX_CHECKBOXES, $LVS_EX_DOUBLEBUFFER)

GUICreate("ListView Sort", 300, 200)

$hListView = GUICtrlCreateListView("Column1|Col2|Col3", 10, 10, 280, 180, -1, $iExWindowStyle)
_GUICtrlListView_SetExtendedListViewStyle($hListView, $iExListViewStyle)

_AddRow($hListView, "ABC|000666|10.05.2004")
_AddRow($hListView, "DEF|444|11.05.2005")
_AddRow($hListView, "CDE|555|12.05.2004")

GUISetState()

_GUICtrlListView_RegisterSortCallBack($hListView)

While 1
Switch GUIGetMsg()
Case $GUI_EVENT_CLOSE
ExitLoop
Case $hListView
; Kick off the sort callback
_GUICtrlListView_SortItems($hListView, GUICtrlGetState($hListView))
EndSwitch
WEnd

_GUICtrlListView_UnRegisterSortCallBack($hListView)
GUIDelete()
EndFunc ;==>_Example1

Func _Example2()
Local $hImage
Local $iExWindowStyle = BitOR($WS_EX_DLGMODALFRAME, $WS_EX_CLIENTEDGE)
Local $iExListViewStyle = BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_GRIDLINES, $LVS_EX_CHECKBOXES, $LVS_EX_DOUBLEBUFFER)

GUICreate("ListView Sort Treat Numbers as Strings", 300, 200, 100)

$hListView = GUICtrlCreateListView("Column1|Col2|Col3", 10, 10, 280, 180, -1, $iExWindowStyle)
_GUICtrlListView_SetExtendedListViewStyle($hListView, $iExListViewStyle)

; Load images

_AddRow($hListView, "ABC|000666|10.05.2004")
_AddRow($hListView, "DEF|444|11.05.2005")
_AddRow($hListView, "CDE|555|12.05.2004")

GUISetState()

GUICreate("ListView Sort Treat Numbers as Numbers", 300, 200, 400)

$hListView2 = GUICtrlCreateListView("Column1|Col2|Col3", 10, 10, 280, 180, -1, $iExWindowStyle)
_GUICtrlListView_SetExtendedListViewStyle($hListView2, $iExListViewStyle)
_GUICtrlListView_SetImageList($hListView2, $hImage, 1)

_AddRow($hListView2, "ABC|000666|10.05.2004")
_AddRow($hListView2, "DEF|444|11.05.2005")
_AddRow($hListView2, "CDE|555|12.05.2004")

GUISetState()

_GUICtrlListView_RegisterSortCallBack($hListView, False)
_GUICtrlListView_RegisterSortCallBack($hListView2)
GUIRegisterMsg($WM_NOTIFY, "_WM_NOTIFY")

While 1
Switch GUIGetMsg()
Case $GUI_EVENT_CLOSE
ExitLoop
EndSwitch
WEnd

_GUICtrlListView_UnRegisterSortCallBack($hListView)
_GUICtrlListView_UnRegisterSortCallBack($hListView2)
GUIRegisterMsg($WM_NOTIFY, "")
EndFunc ;==>_Example2

Func _AddRow($hWnd, $sItem)
Local $aItem = StringSplit($sItem, "|")
Local $iIndex = _GUICtrlListView_AddItem($hWnd, $aItem[1], -1, _GUICtrlListView_GetItemCount($hListView) + 9999)
_GUICtrlListView_SetColumnWidth($hWnd, 0, $LVSCW_AUTOSIZE_USEHEADER)

For $x = 2 To $aItem[0]
_GUICtrlListView_AddSubItem($hWnd, $iIndex, $aItem[$x], $x - 1)
_GUICtrlListView_SetColumnWidth($hWnd, $x - 1, $LVSCW_AUTOSIZE)
Next
EndFunc ;==>_AddRow

Func _WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
#forceref $hWnd, $iMsg, $iwParam
Local $hWndFrom, $iCode, $tNMHDR, $hWndListView, $hWndListView2

$hWndListView = $hListView
$hWndListView2 = $hListView2
If Not IsHWnd($hListView) Then $hWndListView = GUICtrlGetHandle($hListView)
If Not IsHWnd($hListView2) Then $hWndListView2 = GUICtrlGetHandle($hListView2)

$tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
$hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
$iCode = DllStructGetData($tNMHDR, "Code")

Switch $hWndFrom
    Case $hWndListView, $hWndListView2
        Switch $iCode
            Case $LVN_COLUMNCLICK ; A column was clicked
                Local $tInfo = DllStructCreate($tagNMLISTVIEW, $ilParam)
                ConsoleWrite("!> " & DllStructGetData($tInfo, "SubItem") & @LF)
                ; Kick off the sort callback
                _GUICtrlListView_SortItems($hWndFrom, DllStructGetData($tInfo, "SubItem"))
                ; No return value
            EndSwitch
        EndSwitch
        
        Return $__LISTVIEWCONSTANT_GUI_RUNDEFMSG
EndFunc ;==>_WM_NOTIFY
Link to comment
Share on other sites

Add a $iParam - Application Defined data in a _GUICtrlListView_AddItem() function.

many thanks for help. This does the trick.

I wasn't able to find out what's meant by "Application Defined data" and for what it's used.

best wishes

Paul

Link to comment
Share on other sites

  • 1 year later...

Maybe it is an idea to be a bit more specific on this topic in the helpfile?

Spent 2 full days trying to figure out OP's problem. Simple line in the helpfile, that you need to define a $iParam for _GuiCtrlListview_SortItems() to work, would have made all the difference.

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...