Jump to content

How to sort listview items?


Recommended Posts

Apparently it's not that simple when another process created the entry, this has been baffling me for the past few hours.

Anyone know how to make it sortable?

#include <GuiListView.au3>

If StringInStr($CmdLineRaw, "/Hwnd=", 2) Then
    Global $Hwnd = StringStripWS(StringReplace($Cmdline[$Cmdline[0] - 1], "/Hwnd=", ""), 8)
    Global $CTRL = StringStripWS(StringReplace($Cmdline[$Cmdline[0] - 2], "/CTRL=", ""), 8)
    Global $Entry = StringStripWS(StringReplace($Cmdline[$Cmdline[0]], "/DATA=", ""), 8)
    $Hwnd = ControlGetHandle(HWnd($Hwnd), "", HWnd($CTRL))
    Global $ItemCount = _GUICtrlListView_GetItemCount($Hwnd)
    _GUICtrlListView_AddItem($Hwnd, $Entry)
    _GUICtrlListView_AddSubItem($Hwnd, $ItemCount, @HOUR&":"&@MIN&":"&@SEC, 1)
    Exit 0
EndIf

AutoItSetOption("GUIOnEventMode", 1)
AutoItSetOption("GUIResizeMode", 1)

Global Const $WS_RESIZABLE = 0x00070000
Global $GUI
Global $ListView1

$GUI = WinGetHandle(GUICreate("test", 465, 280, -1, -1, $WS_RESIZABLE))
GUISetOnEvent(-3, "Terminate")
$ListView1 = GUICtrlCreateListView("1|2", 4, 49, 450, 166)
GUICtrlSetOnEvent(-1, "SortIt")
DllCall("USER32.DLL", "lresult", "SendMessageW", "hwnd", GUICtrlGetHandle($ListView1), "uint", 0x1000 + 30, "wparam", 0, "lparam", 120)
GUICtrlCreateButton("Test this", 172, 225, 65, 25)
GUICtrlSetOnEvent(-1, "TEST")
GUISetState(@SW_SHOW)

_GUICtrlListView_RegisterSortCallBack($ListView1)

While 1
    Sleep(100)
WEnd

Func TEST()
        For $I = 0 To 8
            Switch @Compiled
                Case True
                    $ChildPID = Run(FileGetShortName(@ScriptFullPath) & " /CTRL=" & GUICtrlGetHandle($ListView1) & " /Hwnd=" & $GUI & " /DATA=" & $I)
                Case False
                    $ChildPID = Run(FileGetShortName(@AutoItExe) & ' /AutoIt3ExecuteScript "' & @ScriptFullPath & '"' & " /CTRL=" & GUICtrlGetHandle($ListView1) & " /Hwnd=" & $GUI & " /DATA=" & $I)
            EndSwitch
            Sleep(1000)
        Next
    Return 1
EndFunc   ;==>TEST

Func SortIt()
    _GUICtrlListView_SortItems($ListView1, GUICtrlGetState($ListView1))
    Return $__LISTVIEWCONSTANT_GUI_RUNDEFMSG
EndFunc   ;==>SortIt

Func Terminate()
_GUICtrlListView_UnRegisterSortCallBack($ListView1)
    Exit 0
EndFunc   ;==>Terminate
Edited by ApudAngelorum
Link to comment
Share on other sites

_GuiCtrlListView_SimpleSort, _GuiCtrlListView_SortItems ?

----------------------------------------

:bye: Hey there, was I helpful?

----------------------------------------

My Current OS: Win8 PRO (64-bit); Current AutoIt Version: v3.3.8.1

Link to comment
Share on other sites

If you add items to the listview using the UDF functions, and you want to make them sortable, you need to add them using a line like this:

_GUICtrlListView_AddItem($Hwnd, $Entry, -1,_GUICtrlListView_GetItemCount($Hwnd) + 9000)

There's an issue with the ListView that requires you to take the current count of the items in the listview, and then add at least 9000 to that number to put the next item in the listview or it won't be sortable.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

I don't know what I'm doing wrong but it's still not working, although I did find a work around involving reading all the entries after the operation has completed and saving them to a variable and deleting the list view and recreating it with the native autoit functions.

Link to comment
Share on other sites

I took your script, changed that line that I posted, and it sorts just fine. Also, I missed where you already had gotten the item count of the listview, so you can shorten it further by using this line

_GUICtrlListView_AddItem($Hwnd, $Entry, -1, $ItemCount + 9000)

This replaces your line #9 in the script you posted in the first post.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

I ran into this problem myself a while ago, couldn't sort the listview because I was adding items using the UDF functions, finally found the answer I gave you after a LOT of false leads going nowhere.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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