PINTO1927

Sort number GUICtrlCreateListView

4 posts in this topic

 

Hello guys,

I'm working with a simple script, complementing a GUICtrlCreateListView which has 5 columns. I would order the first column so that the numbers are decreasing.

#include <GuiListView.au3>
#include <ListViewConstants.au3>
#include <WindowsConstants.au3>
#include <GuiConstantsEx.au3>
#include <StructureConstants.au3>

Opt('MustDeclareVars', 1)

Global $search_LV, $B_DESCENDING
GUICreate("ListView Sort by Column Click", 400, 300)
$search_LV = GUICtrlCreateListView("String|Number|String", 2, 2, 394, 268)
GUICtrlSendMsg($search_LV, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_GRIDLINES, $LVS_EX_GRIDLINES)
GUICtrlSendMsg($search_LV, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_FULLROWSELECT, $LVS_EX_FULLROWSELECT)
GUICtrlCreateListViewItem("line4|5|1A", $search_LV)
GUICtrlCreateListViewItem("line5|4.50 |1B", $search_LV)
GUICtrlCreateListViewItem("line10|4.0 |2C", $search_LV)
GUICtrlCreateListViewItem("line3|23|01", $search_LV)
GUICtrlCreateListViewItem("line2|0.34560 |09", $search_LV)
GUICtrlCreateListViewItem("line1|1.0 |7A", $search_LV)
GUICtrlCreateListViewItem("line1|0.1 |8C", $search_LV)
GUICtrlCreateListViewItem("line1|97|5B", $search_LV)
GUICtrlCreateListViewItem("line1|910|9B", $search_LV)
GUICtrlCreateListViewItem("line1|99|11", $search_LV)
GUICtrlCreateListViewItem("line1|990.99|06", $search_LV)
_GUICtrlListView_SetColumnWidth($search_LV, 0, 75)
_GUICtrlListView_SetColumnWidth($search_LV, 1, 75)
_GUICtrlListView_SetColumnWidth($search_LV, 2, 75)
GUISetState()

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
;~ _GUICtrlListView_RegisterSortCallBack($search_LV, False)

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

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $iwParam
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView, $tInfo
    $hWndListView = $search_LV
;~  If Not IsHWnd($search_LV) Then $hWndListView = GUICtrlGetHandle($search_LV)

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

    Switch $hWndFrom
;~      Case $search_LV
        Case GUICtrlGetHandle($search_LV)
            Switch $iCode
                Case $LVN_COLUMNCLICK ; A column was clicked
                    ConsoleWrite("Header geklickt" & @CRLF)
                    Local $tInfo = DllStructCreate($tagNMLISTVIEW, $iLparam)
                    _GUICtrlListView_UnRegisterSortCallBack($search_LV)
                    Local $ColumnSorted = DllStructGetData($tInfo, 'SubItem')
                    If $ColumnSorted = 1 Then
                        ConsoleWrite('Numeric: ' & _GUICtrlListView_RegisterSortCallBack($search_LV, True) & @CRLF)
                    Else
                        ConsoleWrite('Literal: ' & _GUICtrlListView_RegisterSortCallBack($search_LV, False) & @CRLF)
                    EndIf
                    _GUICtrlListView_SortItems($search_LV, DllStructGetData($tInfo, 'SubItem'))
            EndSwitch
    EndSwitch
EndFunc   ;==>WM_NOTIFY

Ordering the "String" column, you notice that the line "line10" is not ordered last. And 'possible to order the "String" column in descending order?

thank you very much

Share this post


Link to post
Share on other sites



ciao,

I solved by changing the 1st column in the array.

Change:

GUICtrlCreateListViewItem("line4|5|1A", $search_LV)
GUICtrlCreateListViewItem("line5|4.50 |1B", $search_LV)
GUICtrlCreateListViewItem("line10|4.0 |2C", $search_LV)
GUICtrlCreateListViewItem("line3|23|01", $search_LV)
GUICtrlCreateListViewItem("line2|0.34560 |09", $search_LV)
GUICtrlCreateListViewItem("line1|1.0 |7A", $search_LV)
GUICtrlCreateListViewItem("line1|0.1 |8C", $search_LV)
GUICtrlCreateListViewItem("line1|97|5B", $search_LV)
GUICtrlCreateListViewItem("line1|910|9B", $search_LV)
GUICtrlCreateListViewItem("line1|99|11", $search_LV)
GUICtrlCreateListViewItem("line1|990.99|06", $search_LV)

to:

GUICtrlCreateListViewItem("line04|5|1A", $search_LV)
GUICtrlCreateListViewItem("line05|4.50 |1B", $search_LV)
GUICtrlCreateListViewItem("line10|4.0 |2C", $search_LV)
GUICtrlCreateListViewItem("line03|23|01", $search_LV)
GUICtrlCreateListViewItem("line02|0.34560 |09", $search_LV)
GUICtrlCreateListViewItem("line01|1.0 |7A", $search_LV)
GUICtrlCreateListViewItem("line01|0.1 |8C", $search_LV)
GUICtrlCreateListViewItem("line01|97|5B", $search_LV)
GUICtrlCreateListViewItem("line01|910|9B", $search_LV)
GUICtrlCreateListViewItem("line01|99|11", $search_LV)
GUICtrlCreateListViewItem("line01|990.99|06", $search_LV)

I Hope this helps

marco

Share this post


Link to post
Share on other sites

 

Hello Marco, thank you, but if I add but if you try to insert "line127" such as the problem persists.

Share this post


Link to post
Share on other sites

PINTO1927,

Try this - you look through the "line" values in the data to load and pad the numeric value as required:

#include <GuiListView.au3>
#include <ListViewConstants.au3>
#include <WindowsConstants.au3>
#include <GuiConstantsEx.au3>
#include <StructureConstants.au3>

#include <Array.au3>
#include <String.au3>

Global $search_LV, $B_DESCENDING

Global $aData[] = ["line4|5|1A", "line5|4.50 |1B", "line10|4.0 |2C", "line3|23|01", "line2|0.34560 |09", _
                    "line1|0.1 |8C", "line1|97|5B", "line127|910|9B", "line1|99|11", "line1|990.99|06"]
Global $iPad = 0
For $i = 0 To UBound($aData) - 1
    $sLine = $aData[$i]
    $iPadLen = StringLen((StringRegExp($sLine, "(?U)^line(.*)\|.*$", 3))[0])
    If $iPadLen > $iPad Then $iPad = $iPadLen
Next
For $i = 0 To UBound($aData) - 1
    $sLine = $aData[$i]
    $iPadLen = StringLen((StringRegExp($sLine, "(?U)^line(.*)\|.*$", 3))[0])
    $sLine = StringReplace($sLine, "line", "line" & _StringRepeat("0", $iPad - $iPadLen))
    $aData[$i] = $sLine
Next

GUICreate("ListView Sort by Column Click", 400, 300)

$search_LV = GUICtrlCreateListView("String|Number|String", 2, 2, 394, 268)
_GUICtrlListView_SetExtendedListViewStyle($search_LV, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT))
_GUICtrlListView_SetColumnWidth($search_LV, 0, 75)
_GUICtrlListView_SetColumnWidth($search_LV, 1, 75)
_GUICtrlListView_SetColumnWidth($search_LV, 2, 75)

For $i = 0 To UBound($aData) - 1
    GUICtrlCreateListViewItem($aData[$i], $search_LV)
Next

GUISetState()

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
_GUICtrlListView_RegisterSortCallBack($search_LV, False)

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

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $iwParam
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView, $tInfo
    $hWndListView = $search_LV
;~  If Not IsHWnd($search_LV) Then $hWndListView = GUICtrlGetHandle($search_LV)

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

    Switch $hWndFrom
;~      Case $search_LV
        Case GUICtrlGetHandle($search_LV)
            Switch $iCode
                Case $LVN_COLUMNCLICK ; A column was clicked
                    ConsoleWrite("Header geklickt" & @CRLF)
                    Local $tInfo = DllStructCreate($tagNMLISTVIEW, $iLparam)
                    _GUICtrlListView_UnRegisterSortCallBack($search_LV)
                    Local $ColumnSorted = DllStructGetData($tInfo, 'SubItem')
                    If $ColumnSorted = 1 Then
                        ConsoleWrite('Numeric: ' & _GUICtrlListView_RegisterSortCallBack($search_LV, True) & @CRLF)
                    Else
                        ConsoleWrite('Literal: ' & _GUICtrlListView_RegisterSortCallBack($search_LV, False) & @CRLF)
                    EndIf
                    _GUICtrlListView_SortItems($search_LV, DllStructGetData($tInfo, 'SubItem'))
            EndSwitch
    EndSwitch
EndFunc   ;==>WM_NOTIFY

Any use?

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

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

  • Similar Content

    • TheDcoder
      By TheDcoder
      Hi, I have a 2D array with 2 columns, the 1st column contains a "version string" and the 2nd column contains a generic string. I want to sort it in the descending order so the latest version comes first.
      #include <Array.au3> Local $aVersionsAndReleases[4][2] = [["0.2.8.9", "Release #1"], ["0.2.9.10", "Release #3"], ["0.2.9.11", "Release #4"], ["0.2.8.10", "Release #2"]] _ArraySort($aVersionsAndReleases, 1) ConsoleWrite(_ArrayToString($aVersionsAndReleases, ' - ')) _ArrayDisplay($aVersionsAndReleases) Unfortunately, _ArraySort isn't working here . This is the output generated by the script:
      0.2.9.11 - Release #4 0.2.9.10 - Release #3 0.2.8.9 - Release #1 0.2.8.10 - Release #2 The expected result should be:
      0.2.9.11 - Release #4 0.2.9.10 - Release #3 0.2.8.10 - Release #2 0.2.8.9 - Release #1 I am looking to develop an function which does this... but I don't know where to start . Can someone help me get started? Thanks in Advance!
      - TD.
    • david1337
      By david1337
      Hey guys
      Quick question:
      In this GUI the listview is created from items in test.txt
      I would like my "Get Info" button to show the actual text of the item that is selected. So far I have been able to make it show the number of the item.
      What is the last missing puzzle here?
       
      #include <GUIConstantsEx.au3> #include <GUIListViewEx.au3> Global $MainGUI_ManageItemList Global $File = "test.txt" Global $FileToArray = FileReadToArray("test.txt") Call ("MainGUI_ManageItemList") Func MainGUI_ManageItemList() Local $Button1 $MainGUI_ManageItemList = GUICreate("Manage Item List", 800, 400, -1, -1) $cLV = GUICtrlCreateListView("[items]", 10, 10, 400, 775, $LVS_NOCOLUMNHEADER) GUICtrlSetFont(-1, 12, 800, 0, "@Arial Unicode MS") _GUICtrlListView_SetColumnWidth($cLV, 0, 378) $Button1 = GUICtrlCreateButton("Button 1", 425, 10, 80, 30) $RemoveItem = GUICtrlCreateButton("Remove Item", 425, 50, 80, 30) $GetInfo = GUICtrlCreateButton("GetInfo", 425, 120, 80, 30) GUISetState(@SW_SHOW, $MainGUI_ManageItemList) ; Intialise ListView Global $iLV_Index = _GUIListViewEx_Init($cLV) ; Insert lines _GUIListViewEx_Insert($FileToArray, True) ; Register required messages _GUIListViewEx_MsgRegister(True, False, False, False) While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop Case $Button1 MsgBox(0,"","Button 1 is pressed") Case $RemoveItem _GUIListViewEx_Delete() Case $GetInfo $iIndex = _GUICtrlListView_GetSelectedIndices($cLV) msgbox (0, "Selected item", $iIndex) EndSwitch WEnd EndFunc ;==>Main  
    • PINTO1927
      By PINTO1927
      Hi Guys,
      I'm working on this project:
      $Import = GUICtrlCreateButton("Import", 15, 175, 90, 40, $WS_GROUP) $List = GUICtrlCreateListView("Name|Address|E-mail", 15, 220, 400, 363, $LVS_SORTDESCENDING) $Item1 = GUICtrlCreateListViewItem("test|test|test", $List) GUISetState(@SW_SHOW, $GUI) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE, $Exit Exit Case $Import $Table_Import = FileOpenDialog("Import list '*.txt'", @DesktopDir & "\", "Text (*.txt)") If @error Then ContinueLoop GUICtrlSetData($Item1, $Table_Import) In the text file the fields are delimited with "|" and the values are not in the same row but one below the other.
      However in the txt file the sum of the values may change.
      Can you help me?
      Thank's
    • PINTO1927
      By PINTO1927
      Hello guys,
      I built a gui where a GUICtrlCreateListView with two columns is present.
      Through an "Import" button I would be able to import the contents of an .xls or .txt.
      Can you help me?
      Thank's
    • Mannyfresh15
      By Mannyfresh15
      Hi guys, I wonder if there is a way to sort the filelist in an array from _FileListToArray () when files have the regular Windows enumeration example: File1 (1), File2 (2), File3 (3), etc. I'm posting two sample graphics so you can see what I mean I would like to get my list like in graphic 2 right now I'm getting my list like in graphic 1 which is the something I don't like. It's worth to say that I have no problem with a list when the files's names start with a number or is just a number thanks to the code found in this post. But, the problem is still present if I just get rid of the parenthesis that is () so in other words the problem happens when the enumeration is at the end of the names whether parenthesis are present or not.