Jump to content

ListView Sorting


 Share

Recommended Posts

Hi guys,

I'm making an application with a listview that gets populated from an .ini file, but I don't understand how to implement sorting in this case. How would I modify this app to incorporate sorting by each column?

#include <GUIConstants.au3>
GUICreate("listview items", 600, 400, (@DesktopWidth / 2) - 300, (@DesktopHeight / 2) - 200, -1)
$colSrv = "Server        "
$colApp = "Application                          "
$colPID = "Pid           "
$listview = GUICtrlCreateListView($colSrv & "|" & $colApp & "|" & $colPID, 20, 60, 550, 200)
$btnPopulate = GUICtrlCreateButton("Populate", 150, 350, 100, 20)
$btnValue = GUICtrlCreateButton("Value?", 400, 350, 70, 20)
GUISetState()
Do
    $msg = GUIGetMsg()
    Select
        Case $msg = $listview
            MsgBox(0, "listview", "clicked=" & GUICtrlGetState($listview), 2)
        Case $msg = $btnPopulate
            _PopulateList()
        Case $msg = $btnValue
            $value = StringSplit(GUICtrlRead(GUICtrlRead($listview)), "|")
            MsgBox(0, "listview item", $value[1] & @LF & $value[2] & @LF & $value[3], 5)
    EndSelect
Until $msg = $GUI_EVENT_CLOSE

Func _PopulateList()
    $file = "C:\work\sample.ini"
    $iniSvr = IniReadSectionNames($file)
    For $i = 1 To UBound($iniSvr) - 1
        $iniApp = IniReadSection($file, $iniSvr[$i])
        For $x = 1 To UBound($iniApp) - 1
            $string = $iniSvr[$i] & "|" & $iniApp[$x][0] & "|" & $iniApp[$x][1]
            GUICtrlCreateListViewItem($string, $listview)
        Next
    Next
EndFunc   ;==>_PopulateList

The Sample.ini file that I am working with is

[Server1]
Application1=4321
Application2=5432
Application3=9561
[Server2]
Application1=3241
Application2=6345
Application3=9567
[Server3]
Application1=8653
Application2=3658
Application3=1378

[u]Helpful tips:[/u]If you want better answers to your questions, take the time to reproduce your issue in a small "stand alone" example script whenever possible. Also, make sure you tell us 1) what you tried, 2) what you expected to happen, and 3) what happened instead.[u]Useful links:[/u]BrettF's update to LxP's "How to AutoIt" pdfValuater's Autoit 1-2-3 Download page for the latest versions of Autoit and SciTE[quote]<glyph> For example - if you came in here asking "how do I use a jackhammer" we might ask "why do you need to use a jackhammer"<glyph> If the answer to the latter question is "to knock my grandmother's head off to let out the evil spirits that gave her cancer", then maybe the problem is actually unrelated to jackhammers[/quote]

Link to comment
Share on other sites

There is several ways, here's a new one I introduced into 3.2.10.0

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

GUICreate("listview items", 600, 400, (@DesktopWidth / 2) - 300, (@DesktopHeight / 2) - 200, -1)
$colSrv = "Server        "
$colApp = "Application                          "
$colPID = "Pid           "
$listview = GUICtrlCreateListView($colSrv & "|" & $colApp & "|" & $colPID, 20, 60, 550, 200)
$btnPopulate = GUICtrlCreateButton("Populate", 150, 350, 100, 20)
$btnValue = GUICtrlCreateButton("Value?", 400, 350, 70, 20)
GUISetState()

_GUICtrlListView_RegisterSortCallBack($listview)

Do
    $msg = GUIGetMsg()
    Select
        Case $msg = $listview
            MsgBox(0, "listview", "clicked=" & GUICtrlGetState($listview), 2)
        Case $msg = $btnPopulate
            _PopulateList()
        Case $msg = $btnValue
            $value = StringSplit(GUICtrlRead(GUICtrlRead($listview)), "|")
            MsgBox(0, "listview item", $value[1] & @LF & $value[2] & @LF & $value[3], 5)
    EndSelect
Until $msg = $GUI_EVENT_CLOSE

_GUICtrlListView_UnRegisterSortCallBack($listview)

Func _PopulateList()
    $file = @ScriptDir & "\sample.ini"
    $iniSvr = IniReadSectionNames($file)
    For $i = 1 To UBound($iniSvr) - 1
        $iniApp = IniReadSection($file, $iniSvr[$i])
        For $x = 1 To UBound($iniApp) - 1
            $string = $iniSvr[$i] & "|" & $iniApp[$x][0] & "|" & $iniApp[$x][1]
            GUICtrlCreateListViewItem($string, $listview)
        Next
    Next
EndFunc   ;==>_PopulateList

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Link to comment
Share on other sites

Hrm...

I am using 3.2.10.0 but I am getting these errors when I attempt your version:

ERROR: _GUICtrlListView_RegisterSortCallBack(): undefined function.
ERROR: _GUICtrlListView_UnRegisterSortCallBack(): undefined function.

Is there a separate UDF I should be looking for?

Edited by SpookMeister

[u]Helpful tips:[/u]If you want better answers to your questions, take the time to reproduce your issue in a small "stand alone" example script whenever possible. Also, make sure you tell us 1) what you tried, 2) what you expected to happen, and 3) what happened instead.[u]Useful links:[/u]BrettF's update to LxP's "How to AutoIt" pdfValuater's Autoit 1-2-3 Download page for the latest versions of Autoit and SciTE[quote]<glyph> For example - if you came in here asking "how do I use a jackhammer" we might ask "why do you need to use a jackhammer"<glyph> If the answer to the latter question is "to knock my grandmother's head off to let out the evil spirits that gave her cancer", then maybe the problem is actually unrelated to jackhammers[/quote]

Link to comment
Share on other sites

Hrm...

I am using 3.2.10.0 but I am getting these errors when I attempt your version:

ERROR: _GUICtrlListView_RegisterSortCallBack(): undefined function.
ERROR: _GUICtrlListView_UnRegisterSortCallBack(): undefined function.

Is there a separate UDF I should be looking for?

Whoops, sorry that will be in the next beta.

Can do the simple sort for this one easy enough.

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

Global $listview

GUICreate("listview items", 600, 400, (@DesktopWidth / 2) - 300, (@DesktopHeight / 2) - 200, -1)
$colSrv = "Server        "
$colApp = "Application                          "
$colPID = "Pid           "
$listview = GUICtrlCreateListView($colSrv & "|" & $colApp & "|" & $colPID, 20, 60, 550, 200)
$btnPopulate = GUICtrlCreateButton("Populate", 150, 350, 100, 20)
$btnValue = GUICtrlCreateButton("Value?", 400, 350, 70, 20)
GUISetState()

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

Global $B_DESCENDING[_GUICtrlListView_GetColumnCount ($listview) ]

Do
    $msg = GUIGetMsg()
    Select
        Case $msg = $listview
            MsgBox(0, "listview", "clicked=" & GUICtrlGetState($listview), 2)
        Case $msg = $btnPopulate
            _PopulateList()
        Case $msg = $btnValue
            $value = StringSplit(GUICtrlRead(GUICtrlRead($listview)), "|")
            MsgBox(0, "listview item", $value[1] & @LF & $value[2] & @LF & $value[3], 5)
    EndSelect
Until $msg = $GUI_EVENT_CLOSE

Func _PopulateList()
    $file = @ScriptDir & "\sample.ini"
    $iniSvr = IniReadSectionNames($file)
    For $i = 1 To UBound($iniSvr) - 1
        $iniApp = IniReadSection($file, $iniSvr[$i])
        For $x = 1 To UBound($iniApp) - 1
            $string = $iniSvr[$i] & "|" & $iniApp[$x][0] & "|" & $iniApp[$x][1]
            GUICtrlCreateListViewItem($string, $listview)
        Next
    Next
EndFunc   ;==>_PopulateList

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

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
        Case $hWndListView
            Switch $iCode
                Case $LVN_COLUMNCLICK ; A column was clicked
                    Local $tInfo = DllStructCreate($tagNMLISTVIEW, $ilParam)
                    _GUICtrlListView_SimpleSort ($hWndListView, $B_DESCENDING, DllStructGetData($tInfo, "SubItem"))
                    ; No return value
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Link to comment
Share on other sites

Thanks Gary. You rock 8)

[u]Helpful tips:[/u]If you want better answers to your questions, take the time to reproduce your issue in a small "stand alone" example script whenever possible. Also, make sure you tell us 1) what you tried, 2) what you expected to happen, and 3) what happened instead.[u]Useful links:[/u]BrettF's update to LxP's "How to AutoIt" pdfValuater's Autoit 1-2-3 Download page for the latest versions of Autoit and SciTE[quote]<glyph> For example - if you came in here asking "how do I use a jackhammer" we might ask "why do you need to use a jackhammer"<glyph> If the answer to the latter question is "to knock my grandmother's head off to let out the evil spirits that gave her cancer", then maybe the problem is actually unrelated to jackhammers[/quote]

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