Jump to content
Jewtus

Sorting multiple list views

Recommended Posts

Jewtus

I have a gui with multiple list views and I have been working with the script I found >HERE

Here is the sample code:

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <MsgBoxConstants.au3>
#include <GuiListView.au3>

$FormMain= GUICreate("Management Utility", 580, 406, 192, 110)
$AreaList = GUICtrlCreateListView("", 8, 40, 250, 310)
_GUICtrlListView_InsertColumn($AreaList, 0, "Name", 150)
_GUICtrlListView_InsertColumn($AreaList, 1, "Description", 150)
GUICtrlCreateListViewItem("Test1|1", $AreaList)
GUICtrlCreateListViewItem("Test2|2", $AreaList)
GUICtrlCreateListViewItem("Test3|3", $AreaList)
GUICtrlCreateListViewItem("Test4|4", $AreaList)
GUICtrlCreateListViewItem("Test5|5", $AreaList)
GUICtrlCreateListViewItem("Test6|6", $AreaList)
$SystemList = GUICtrlCreateListView("", 304, 40, 250, 310)
_GUICtrlListView_InsertColumn($SystemList, 0, "Name", 150)
_GUICtrlListView_InsertColumn($SystemList, 1, "Description", 150)
GUICtrlCreateListViewItem("Test1|1", $SystemList)
GUICtrlCreateListViewItem("Test2|2", $SystemList)
GUICtrlCreateListViewItem("Test3|3", $SystemList)
GUICtrlCreateListViewItem("Test4|4", $SystemList)
GUICtrlCreateListViewItem("Test5|5", $SystemList)
GUICtrlCreateListViewItem("Test6|6", $SystemList)
$AddArea_Btn = GUICtrlCreateButton("Add Area", 8, 360, 75, 25)
$EditArea_Btn = GUICtrlCreateButton("Edit Area", 96, 360, 75, 25)
$EditSystem_Btn = GUICtrlCreateButton("Edit System", 384, 360, 75, 25)
$AddSystem_Btn = GUICtrlCreateButton("Add System", 472, 360, 75, 25)
$AreasLabel = GUICtrlCreateLabel("Areas", 8, 8, 51, 27)
GUICtrlSetFont(-1, 16, 400, 0, "Times New Roman")
$SystemsLabel = GUICtrlCreateLabel("Systems", 304, 8, 71, 27)
GUICtrlSetFont(-1, 16, 400, 0, "Times New Roman")
GUISetState(@SW_SHOW)
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd


;===============================================================================
;
; Function Name:    _ListView_Sort()
; Description:      Sorting ListView items when column click
; Parameter(s):     $cIndex - Column index
; Return Value(s):  None
; Requirement(s):   AutoIt 3.2.12.0 and above
; Author(s):        R.Gilman (a.k.a rasim)
;
; ATTEMPT TO Mod for use with multi list views
;
;================================================================================
Func _ListView_Sort($cIndex = 0)
    Local $iColumnsCount, $iDimension, $iItemsCount, $aItemsTemp, $aItemsText, $iCurPos, $iImgSummand, $i, $j

    $iColumnsCount = _GUICtrlListView_GetColumnCount($AreaList)

    $iDimension = $iColumnsCount * 2

    $iItemsCount = _GUICtrlListView_GetItemCount($AreaList)

    Local $aItemsTemp[1][$iDimension]

    For $i = 0 To $iItemsCount - 1
        $aItemsTemp[0][0] += 1
        ReDim $aItemsTemp[$aItemsTemp[0][0] + 1][$iDimension]

        $aItemsText = _GUICtrlListView_GetItemTextArray($AreaList, $i)
        $iImgSummand = $aItemsText[0] - 1

        For $j = 1 To $aItemsText[0]
            $aItemsTemp[$aItemsTemp[0][0]][$j - 1] = $aItemsText[$j]
            $aItemsTemp[$aItemsTemp[0][0]][$j + $iImgSummand] = _GUICtrlListView_GetItemImage($AreaList, $i, $j - 1)
        Next
    Next

    $iCurPos = $aItemsTemp[1][$cIndex]
    _ArraySort($aItemsTemp, 0, 1, 0, $cIndex)
    If StringInStr($iCurPos, $aItemsTemp[1][$cIndex]) Then _ArraySort($aItemsTemp, 1, 1, 0, $cIndex)

    For $i = 1 To $aItemsTemp[0][0]
        For $j = 1 To $iColumnsCount
            _GUICtrlListView_SetItemText($AreaList, $i - 1, $aItemsTemp[$i][$j - 1], $j - 1)
            _GUICtrlListView_SetItemImage($AreaList, $i - 1, $aItemsTemp[$i][$j + $iImgSummand], $j - 1)
        Next
    Next
EndFunc
;================================================================================

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView
    $hWndListView = $AreaList
    If Not IsHWnd($AreaList) Then $hWndListView = GUICtrlGetHandle($AreaList)

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

    Switch $hWndFrom
    Case $hWndListView
        Switch $iCode
        Case $LVN_COLUMNCLICK
            Local $tInfo = DllStructCreate($tagNMLISTVIEW, $ilParam)
            Local $ColumnIndex = DllStructGetData($tInfo, "SubItem")
            _ListView_Sort($ColumnIndex)
        EndSwitch
    EndSwitch

    Return $GUI_RUNDEFMSG
EndFunc

How to I make both list boxes sortable? I plan on having 2-3 more list boxes but if I cannot figure this out, I'll have to rethink how to use my script.

Share this post


Link to post
Share on other sites
Danyfirex

why not  GUICtrlRegisterListViewSort?

 

so using your way is:

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <MsgBoxConstants.au3>
#include <GuiListView.au3>

$FormMain= GUICreate("Management Utility", 580, 406, 192, 110)
$AreaList = GUICtrlCreateListView("", 8, 40, 250, 310)
_GUICtrlListView_InsertColumn($AreaList, 0, "Name", 150)
_GUICtrlListView_InsertColumn($AreaList, 1, "Description", 150)
GUICtrlCreateListViewItem("Test1|1", $AreaList)
GUICtrlCreateListViewItem("Test2|2", $AreaList)
GUICtrlCreateListViewItem("Test3|3", $AreaList)
GUICtrlCreateListViewItem("Test4|4", $AreaList)
GUICtrlCreateListViewItem("Test5|5", $AreaList)
GUICtrlCreateListViewItem("Test6|6", $AreaList)
$SystemList = GUICtrlCreateListView("", 304, 40, 250, 310)
_GUICtrlListView_InsertColumn($SystemList, 0, "Name", 150)
_GUICtrlListView_InsertColumn($SystemList, 1, "Description", 150)
GUICtrlCreateListViewItem("Test1|1", $SystemList)
GUICtrlCreateListViewItem("Test2|2", $SystemList)
GUICtrlCreateListViewItem("Test3|3", $SystemList)
GUICtrlCreateListViewItem("Test4|4", $SystemList)
GUICtrlCreateListViewItem("Test5|5", $SystemList)
GUICtrlCreateListViewItem("Test6|6", $SystemList)
$AddArea_Btn = GUICtrlCreateButton("Add Area", 8, 360, 75, 25)
$EditArea_Btn = GUICtrlCreateButton("Edit Area", 96, 360, 75, 25)
$EditSystem_Btn = GUICtrlCreateButton("Edit System", 384, 360, 75, 25)
$AddSystem_Btn = GUICtrlCreateButton("Add System", 472, 360, 75, 25)
$AreasLabel = GUICtrlCreateLabel("Areas", 8, 8, 51, 27)
GUICtrlSetFont(-1, 16, 400, 0, "Times New Roman")
$SystemsLabel = GUICtrlCreateLabel("Systems", 304, 8, 71, 27)
GUICtrlSetFont(-1, 16, 400, 0, "Times New Roman")
GUISetState(@SW_SHOW)
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd


;===============================================================================
;
; Function Name:    _ListView_Sort()
; Description:      Sorting ListView items when column click
; Parameter(s):     $cIndex - Column index
; Return Value(s):  None
; Requirement(s):   AutoIt 3.2.12.0 and above
; Author(s):        R.Gilman (a.k.a rasim)
;
; ATTEMPT TO Mod for use with multi list views
;
;================================================================================
Func _ListView_Sort($Listview,$cIndex = 0)
    Local $iColumnsCount, $iDimension, $iItemsCount, $aItemsTemp, $aItemsText, $iCurPos, $iImgSummand, $i, $j

    $iColumnsCount = _GUICtrlListView_GetColumnCount($Listview)

    $iDimension = $iColumnsCount * 2

    $iItemsCount = _GUICtrlListView_GetItemCount($Listview)

    Local $aItemsTemp[1][$iDimension]

    For $i = 0 To $iItemsCount - 1
        $aItemsTemp[0][0] += 1
        ReDim $aItemsTemp[$aItemsTemp[0][0] + 1][$iDimension]

        $aItemsText = _GUICtrlListView_GetItemTextArray($Listview, $i)
        $iImgSummand = $aItemsText[0] - 1

        For $j = 1 To $aItemsText[0]
            $aItemsTemp[$aItemsTemp[0][0]][$j - 1] = $aItemsText[$j]
            $aItemsTemp[$aItemsTemp[0][0]][$j + $iImgSummand] = _GUICtrlListView_GetItemImage($Listview, $i, $j - 1)
        Next
    Next

    $iCurPos = $aItemsTemp[1][$cIndex]
    _ArraySort($aItemsTemp, 0, 1, 0, $cIndex)
    If StringInStr($iCurPos, $aItemsTemp[1][$cIndex]) Then _ArraySort($aItemsTemp, 1, 1, 0, $cIndex)

    For $i = 1 To $aItemsTemp[0][0]
        For $j = 1 To $iColumnsCount
            _GUICtrlListView_SetItemText($Listview, $i - 1, $aItemsTemp[$i][$j - 1], $j - 1)
            _GUICtrlListView_SetItemImage($Listview, $i - 1, $aItemsTemp[$i][$j + $iImgSummand], $j - 1)
        Next
    Next
EndFunc
;================================================================================

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView,$hWndListView2
    $hWndListView = $AreaList
    $hWndListView2= $SystemList
    If Not IsHWnd($AreaList) Then $hWndListView = GUICtrlGetHandle($AreaList)
     If Not IsHWnd($SystemList) Then $hWndListView2 = GUICtrlGetHandle($SystemList)

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

    Switch $hWndFrom
    Case $hWndListView,$hWndListView2
        Switch $iCode
        Case $LVN_COLUMNCLICK
            Local $tInfo = DllStructCreate($tagNMLISTVIEW, $ilParam)
            Local $ColumnIndex = DllStructGetData($tInfo, "SubItem")
            _ListView_Sort($hWndFrom,$ColumnIndex)
        EndSwitch
    EndSwitch

    Return $GUI_RUNDEFMSG
EndFunc 

Saludos

Edited by Danyfirex

Share this post


Link to post
Share on other sites
Jewtus

Mainly because I don't actually understand how it works.

Can you provide an example with multiple list views and I can see how to adapt it to what I'm trying to do? I played with the example from that function and I really don't understand how it works...

Share this post


Link to post
Share on other sites
Danyfirex

Share this post


Link to post
Share on other sites
Jewtus

Ahhh I see what you did there.

Its basically these line that I needed:

    $hWndListView = $AreaList
    $hWndListView2= $SystemList
    If Not IsHWnd($AreaList) Then $hWndListView = GUICtrlGetHandle($AreaList)
    If Not IsHWnd($SystemList) Then $hWndListView2 = GUICtrlGetHandle($SystemList)
 
 
Thanks!

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

×