Jump to content

Listview sorting.


Go to solution Solved by BrewManNH,

Recommended Posts

Hello all,

I'm struggling with a sorting problem with my Listview:

7pbmH.png

I want be able to sort it by clicking the Names on top.
 

Global $listview = GUICtrlCreateListView("Date           |Requests           |Impressions                                |Clicks             |CTR            |eCPM           |Earnings           ", 15, 160, 679, 105)


func _listview()
     _GUICtrlListView_DeleteAllItems($listview)
    GUICtrlSetBkColor($listview, 0xF2F6F7)

;~  _GUICtrlListView_SetBkColor($listview, 0x0000FF)
    _GUICtrlListView_SetColumnWidth($listview, 0, 85)
EndFunc


        GUICtrlCreateListViewItem($date & "          "&"|" &$totalreq&"          "&"|" &$totalimpes&"                                "&"|"& $total_clicks&"          "&"|"& $ctr&"%          "&"|$"& $ecpm&"             "&"|$"& $totalbalance&"             ", $listview)

Sorry for the weird sentences.
Anyway , i tried some sorting things in helpfile but everytime my script will just doing weird because it's doing a while loop.

So how to sort them, until i quit the gui or click the "$Button1" button.

Link to comment
Share on other sites

According to the help file: "Sorting the list by clicking the column name (as in Explorer) is not currently implemented."

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

I don't see anywhere in the posted code an attempt at sorting the listview. Have you attempted to search for the dozens of examples on the forum of how to do this?

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

To register a sort function please have a look at _GUICtrlListView_RegisterSortCallBack.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

  • Moderators

Jacko23,

You can do it like this: :)

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

Global $aData[4] = ["2014-03-09|456|201|3|0.45%|$2.94|$0.7654", "2014-03-08|236|231|2|0.65%|$1.25|$1.4321", _
            "2014-03-07|566|341|1|0.75%|$4.62|$0.4567", "2014-03-06|786|561|3|0.95%|$6.48|$0.8765"]

Global $fSortSense = False ; Set initial ascending sort

$hGUI = GUICreate("Test", 500, 500)

$cListView = GUICtrlCreateListView("", 10, 10, 480, 400)
_GUICtrlListView_AddColumn($cListView, "Date", 80)
_GUICtrlListView_AddColumn($cListView, "Requests", 80)
_GUICtrlListView_AddColumn($cListView, "Impressions", 80)
_GUICtrlListView_AddColumn($cListView, "Clicks")
_GUICtrlListView_AddColumn($cListView, "CTR")
_GUICtrlListView_AddColumn($cListView, "eCPM")
_GUICtrlListView_AddColumn($cListView, "Earnings", 80)

For $i = 0 To 3
    GUICtrlCreateListViewItem($aData[$i], $cListView)
Next

GUISetState()

GUIRegisterMsg($WM_NOTIFY, "_WM_NOTIFY")

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch

WEnd

Func _WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam
    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $iIDFrom
        Case $cListView
            Switch $iCode
                Case $LVN_COLUMNCLICK ; A column was clicked
                    $tInfo = DllStructCreate($tagNMLISTVIEW, $lParam)
                    _GUICtrlListView_SimpleSort($cListView, $fSortSense, DllStructGetData($tInfo, "SubItem"))
            EndSwitch
    EndSwitch
EndFunc
M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Sorry, Came to a another problem.

Func _RunAllOnStart()
    _listview()
    _HasVideoAds()
    _RegistrationDate()
    _CompletionRate()
    _CurrentBalance()
    _PayoutDate()
    _ServerDate()
    _Statistics()
    _LastRefresh()
    _ReferralStatus()
EndFunc

Func _RunAllFunc()
    GUISetState(@SW_LOCK)
    _listview()
    _ReferralStatus()
    _HasVideoAds()
    _RegistrationDate()
    _CompletionRate()
    _CurrentBalance()
    _PayoutDate()
    _ServerDate()
    _Statistics()
    _LastRefresh()
    _listview()
    GUISetState(@SW_UNLOCK)
EndFunc

Runallonstart that means when GUI starts up it will run all functions and then the sorting system works.

When i click $button1 = Refresh button then it will do the _RunAllFunc.

Then my listview is empty for some reason because i added the:

func _listview()
     _GUICtrlListView_DeleteAllItems($listview)
    GUICtrlSetBkColor($listview, 0xF2F6F7)

;~  _GUICtrlListView_SetBkColor($listview, 0x0000FF)
    _GUICtrlListView_SetColumnWidth($listview, 0, 85)
    GUIRegisterMsg($WM_NOTIFY, "_WM_NOTIFY")
EndFunc

Func _WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam
    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $iIDFrom
        Case $listview
            Switch $iCode
                Case $LVN_COLUMNCLICK ; A column was clicked
                    $tInfo = DllStructCreate($tagNMLISTVIEW, $lParam)
                    _GUICtrlListView_SimpleSort($listview, $fSortSense, DllStructGetData($tInfo, "SubItem"))
            EndSwitch
    EndSwitch
EndFunc

So how to fix this? 

Link to comment
Share on other sites

  • Solution

An easier way to sort a ListView.

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

Global $aData[4] = ["2014-03-09|456|201|3|0.45%|$2.94|$0.7654", "2014-03-08|236|231|2|0.65%|$1.25|$1.4321", _
        "2014-03-07|566|341|1|0.75%|$4.62|$0.4567", "2014-03-06|786|561|3|0.95%|$6.48|$0.8765"]

Global $fSortSense = False ; Set initial ascending sort

$hGUI = GUICreate("Test", 500, 500)

$cListView = GUICtrlCreateListView("", 10, 10, 480, 400)
_GUICtrlListView_AddColumn($cListView, "Date", 80)
_GUICtrlListView_AddColumn($cListView, "Requests", 80)
_GUICtrlListView_AddColumn($cListView, "Impressions", 80)
_GUICtrlListView_AddColumn($cListView, "Clicks")
_GUICtrlListView_AddColumn($cListView, "CTR")
_GUICtrlListView_AddColumn($cListView, "eCPM")
_GUICtrlListView_AddColumn($cListView, "Earnings", 80)

For $i = 0 To 3
    GUICtrlCreateListViewItem($aData[$i], $cListView)
Next

GUISetState()

Global $iCol
While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $cListView
            $iCol = GUICtrlGetState($cListView)
            _GUICtrlListView_SimpleSort($cListView, $fSortSense, $iCol)
    EndSwitch

WEnd

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

  • Moderators

BrewManNH,

I keep forgetting that you can do it that way! :>

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

I'm infinitely lazy, I only like to type as much/little as possible to achieve my objective. ;)

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

  • 1 month later...

Might someone take a look at my code and tell me why it's not sorting? Nothing happens when I click the column header, although I do believe I have implemented BrewMan's code correctly. I'm wondering if it has anything to do with the data I pull from the excel spreadsheet...can anything from that process cause problems with sorting this way?
 
Thanks for any help:

#include <Excel.au3>
#include <Array.au3>
#include <GuiImageList.au3>
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>

Global $fSortSense = False ; Set initial ascending sort

$oExcel = _ExcelBookOpen("c:\users.xls", @SW_HIDE)
_ExcelSheetActivate($oExcel, "Sheet1")
$result = _ExcelReadSheetToArray($oExcel)
_ExcelBookClose($oExcel)


#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 986, 581, 192, 124)
$hListView = _GUICtrlListView_Create($Form1, "Computer name|User|Dept|Location", 20, 15, 500, 170)  ; create the list view
_GUICtrlListView_SetColumnWidth($hListView, 0, 100)
_GUICtrlListView_SetColumnWidth($hListView, 1, 175)
_GUICtrlListView_SetColumnWidth($hListView, 2, 130)
_GUICtrlListView_SetColumnWidth($hListView, 4, 50)
_GUICtrlListView_SetExtendedListViewStyle($hListView, $LVS_EX_FULLROWSELECT )                       ; selecting one item highlights entire row
$Button1 = GUICtrlCreateButton("Ninite", 525, 56, 89, 33)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

for $i = 0 to UBound($result,1) - 3
    _GUICtrlListView_AddItem($hListView, $result[$i + 2][1], 0)
    _GUICtrlListView_AddSubItem ($hListView, $i, $result[$i + 2][2], 1)
    _GUICtrlListView_AddSubItem ($hListView, $i, $result[$i + 2][3], 2)
    _GUICtrlListView_AddSubItem ($hListView, $i, $result[$i + 2][4], 3)
    ;Sleep(2000)
Next

GUISetState()

Global $iCol

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hListView
            $iCol = GUICtrlGetState($hListView)
            _GUICtrlListView_SimpleSort($hListView, $fSortSense, $iCol)
    EndSwitch
WEnd
Link to comment
Share on other sites

GUIGetMsg won't work with control handles, and the listview was created using the UDF and not the native functions. If you create your listview with the native function it should work.

$hListView = GUICtrlCreateListView("Computer name|User|Dept|Location", 20, 15, 500, 170)  ; create the list view

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

Ok great, that worked!

However, I'm not entirely clear on why. When I switched to using a native function, what does that mean in terms of what GUIGetMsg is receiving? It is not getting a control handle anymore? I believe I have a [very] incomplete understanding of how all the pieces work together, so please excuse me if my code indicates that I'm farther along than I actually am :-)

Link to comment
Share on other sites

GUIGetMsg works with the control IDs that AutoIt's native functions return, and with the handle from the GUICreate function. It will not work with the handles returned by the UDF functions which is why your script wouldn't work when you used _GUICtrlListView_Create to create the listview. You can get the handle from a control ID using GUICtrlGetHandle, but you can't get the control ID from the handle because there isn't one.

The built-in functions in AutoIt will usually only work with control IDs returned from AutoIt built-in functions. The UDFs will most of the time work with either, although some of them don't work 100% accurately with a control ID.

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