Jump to content

[SOLVED!!] Sorting ListViews and losing track of item location


BrewManNH
 Share

Recommended Posts

I am creating a media player, yeah I know another one of THOSE projects but it's giving me a lot of insight into how to manipulate data, but my problem is if I include the option to sort the listview items by clicking on a column I can't find the order of the items in the listview. If I create the listview items and don't include the sorting options I can seek to the next or the previous items in it without a problem. As soon as I sort the list I lose track of which items are in which positions. Every other function works great but I can't find the next or previous items.

The way the listview works without sorting is like this

ID #                Position in the list
1                          1
2                          2
3                          3
4                          4

After sorting it could look like this

ID #                Position in the list
3                          1
2                          2
4                          3
1                          4

Trying to go from one item in the list to the next is becoming problematical for me as I'd like to include sorting on this but can't figure out how to do it without screwing up the next and previous buttons. I've looked at _GUICtrlListView_MapIndexToID and its counterpart _GUICtrlListView_MapIDToIndex but the help file makes no sense to me and I can't figure out if those are even the functions I would need. Any help would be greatly appreciated.

Edited by BrewManNH

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

A bump for another cry for help.

Thanks for looking.

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 finally solved this question after a lot of looking, and it's a lot easier to do than I had originally thought. It takes a lot of code to do this, but the cure is pretty painless. Here's what I came up with.

#include <Array.au3>
#include <Buttonconstants.au3>
#include <File.au3>
#include <GuiConstants.au3>
#include <GuiConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GuiMenu.au3>
#include <GuiListView.au3>
#include <GuiStatusBar.au3>
Opt("Guioneventmode" , 1)
GUIRegisterMsg($WM_NOTIFY, "_WM_NOTIFY")
$start = TimerInit()
Global $szDrive, $szDir, $szFName, $szExt, $BASS_STARTUP_BYPASS_VERSIONCHECK = False, $aMusicList[10000], $Initload = True
$Form1 = GUICreate("Bob's Media Player"       , 544, 500, 292, 218)
GUISetOnEvent($GUI_EVENT_CLOSE, "Form1Close")
GUISetBkColor(0x00080FF, $Form1)
$ListView = GUICtrlCreateListView("Track|     Title        |         Artist     |       Album     |Year|           Original Filename and path"       , 10, 200, 527, 280)
$Button = GUICtrlCreateButton("Read"       , 50, 50, 50, 60)
GUICtrlSetOnEvent(-1, "ReadButton")
Open()
GUISetState()
While 1
    Sleep(100)
WEnd
Func _WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $iwParam
    Global $hWndFrom, $iCode, $tNMHDR, $hWndListView

    $hWndListView = $ListView
    If Not IsHWnd($ListView) Then $hWndListView = GUICtrlGetHandle($ListView)

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

    Switch $hWndFrom
        Case $hWndListView
            Switch $iCode
                Case $LVN_COLUMNCLICK ; A column was clicked
                    Global $tInfo = DllStructCreate($tagNMLISTVIEW, $ilParam)
                    _GUICtrlListView_RegisterSortCallBack($ListView)
                    _GUICtrlListView_SortItems($hWndFrom, DllStructGetData($tInfo, "SubItem"       ))
                    _GUICtrlListView_UnRegisterSortCallBack($ListView)
            EndSwitch
    EndSwitch
    Return $__LISTVIEWCONSTANT_GUI_RUNDEFMSG
EndFunc   ;==>_WM_NOTIFY
Func form1close()
    Exit
EndFunc   ;==>form1close
Func Open()
    $Open = FileOpen(@ScriptDir & "\MusicList.txt", 0)
    $I = 1
    While 1
        $aMusicList[$I] = FileReadLine($Open)
        If @error = -1 Then ExitLoop
        $I += 1
    WEnd
    ReDim $aMusicList[$I + 1]
    $aMusicList[0] = $I
    FileClose($Open)
    Global $hOpenLVItem[$aMusicList[0] + 1]
    _GUICtrlListView_DeleteAllItems($ListView)
    For $I = 1 To ($aMusicList[0] - 1)
        $hOpenLVItem[$I] = GUICtrlCreateListViewItem($aMusicList[$I], $ListView)

        GUICtrlSetBkColor(-1, 0x00FF00)
    Next
    $hOpenLVItem[0] = $I - 1
    $Built = True
    $Saved = True
    If Not $Initload Then MsgBox(64, "Loaded", "The music list has been loaded!")
    $Initload = False
    For $I = 1 To _GUICtrlListView_GetColumnCount($ListView)
        _GUICtrlListView_SetColumnWidth($ListView, $I, $LVSCW_AUTOSIZE)
    Next
EndFunc   ;==>Open
Func ReadButton()
    Local $aMusicList1[_GUICtrlListView_GetItemCount($ListView)]
    For $I = 0 To _GUICtrlListView_GetItemCount($ListView) - 1
        _GUICtrlListView_SetItemState($ListView, $I, $LVIS_selected, $LVIS_selected)
        $Read1 = GUICtrlRead(GUICtrlRead($ListView))
        $aMusicList1[$I] = $Read1
    Next
    _ArrayDisplay($aMusicList1)
    $aMusicList1 = 0
EndFunc   ;==>ReadButton

In this example script I read a text file that has a previously built list of song titles, and tag information. This text file is attached to this post. After reading in this file it populates the listview box. I have enabled sorting on any column using WM_NOTIFY. Normally, after sorting the listview items, the item indexes would be all over the place and trying to go from one song in the list to the previous or next one was extremely difficult. Index item 3400 might be just before item 2756 after you've sorted a listview.

What I came up with is fairly simple to implement. I sort the list, then using the function _GUICtrlListView_SetItemState($ListView, $I, $LVIS_selected, $LVIS_selected) in a loop, I read every item in the listview, as it is currently placed, into an array. Then I save this array to a text file, read that file back into the listview to populate the listview again, resetting the item indexes. This way it is very easy to read the next and previous items in the list view box because they are now in order.

If you run this example script, you will see how it works by pressing the "Read" button. This will scan the listview box and then display the array of items as they are shown in the ListView. I didn't include the part of my script that saves and then reloads the text file that I would save the array to, but this snippet should show how I accomplished it.

If anyone has anything that would streamline this I would really appreciate it. It is a quickly written fix for my problem and I really didn't take much time to optimize it because as the saying goes "If it ain't broke, don't fix it".

Edited by BrewManNH

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