Jump to content

Manually arrange array in listview?


Recommended Posts

I have all of the rest of my larger script working except for this function. Is there anyway to "drag" an item in a listview to change the order of the array? Among other things, my larger script needs to print out a selected set of pdf files in a set order such as: A1.pdf, A2.pdf, E1.pdf, E2,pdf, E3.pdf, B1.pdf, B2.pdf, 1.pdf, 2.pdf, 3.pdf etc. If it isn't possible to drag, then anyone have any other suggestions?

#include <Array.au3>
#include <GUIConstantsEx.au3>

$_1 = GUICreate("Jesse's Plot Wizard", 395, 389, 325, 178)

Local $filemenu, $exititem, $helpmenu, $infoitem
$filemenu = GUICtrlCreateMenu("&File")
$fileitem = GUICtrlCreateMenuItem("Open", $filemenu)
$exititem = GUICtrlCreateMenuItem("Exit", $filemenu)
GUISetState(@SW_SHOW)
Local $_1_AccelTable[3][2] = [["^!+4", $filemenu],["{NUM 5}", $fileitem],["{UP}", $infoitem]]
GUISetAccelerators($_1_AccelTable)

While 1
    dim $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $fileitem
            dim     $folder = "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}"
            $file = FileOpenDialog("", $folder, "Images (*.tif;*.jpg;*.pdf)", 1 + 4)
            $array = StringSplit($file, "|")
            If $array[0] = 1 Then
                $len = StringLen($folder)
                $trim = $len - StringInStr($folder, "\", 0, -1, $len)
            EndIf
            _ArraySort($array, 0, 2)
            _listview()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

Func _listview()
    $Form1 = GUICreate("Arrange Array", 200, 300)
    $ListView1 = GUICtrlCreateListView("File                       ", 10, 10, 180, 280)
    $incr = 2
    $filecount = $array[0]
    While $incr <= $filecount
        GUICtrlCreateListViewItem($array[$incr], $ListView1)
        $incr += 1
    WEnd
    GUISetState(@SW_SHOW)
EndFunc
Link to comment
Share on other sites

Well I have made progress but still not exactly where I want to be and need help.

#include <Array.au3>
#include <GUIConstantsEx.au3>
#include <ListviewConstants.au3>

$_1 = GUICreate("Jesse's Plot Wizard", 395, 389, 325, 178)

Local $filemenu, $exititem, $helpmenu, $infoitem
$filemenu = GUICtrlCreateMenu("&File")
$fileitem = GUICtrlCreateMenuItem("Open", $filemenu)
$exititem = GUICtrlCreateMenuItem("Exit", $filemenu)
GUISetState(@SW_SHOW)
Local $_1_AccelTable[3][2] = [["^!+4", $filemenu],["{NUM 5}", $fileitem],["{UP}", $infoitem]]
GUISetAccelerators($_1_AccelTable)

While 1
    Dim $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $fileitem
            Dim $folder = "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}"
            $file = FileOpenDialog("", $folder, "Images (*.tif;*.jpg;*.pdf)", 1 + 4)
            $array = StringSplit($file, "|")
            If $array[0] = 1 Then
                $len = StringLen($folder)
                $trim = $len - StringInStr($folder, "\", 0, -1, $len)
            EndIf
            _ArraySort($array, 0, 2)
            _listview()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

Func _listview()
    $Form1 = GUICreate("Arrange Array", 200, 400)
    $listview = GUICtrlCreateListView("File                    ", 10, 10, 180, 350, $LVS_SINGLESEL,$LVS_EX_CHECKBOXES)
    $up = GUICtrlCreateButton("UP",20,370,50,20)
    $down = GUICtrlCreateButton("DOWN",130,370,50,20)
    GUICtrlSetState(-1, $GUI_DROPACCEPTED)
    GUISetState()
    $incr = 2
    $filecount = $array[0]
    While $incr <= $filecount
        GUICtrlCreateListViewItem($array[$incr], $listview)
        $incr += 1
    WEnd
    GUISetState(@SW_SHOW, $Form1)
    While 1
        Dim $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $up
                $sel = GUICtrlRead(GUICtrlRead($listview),2)
                $sel = StringTrimRight($sel, 1)
                $cnt = _ArraySearch($array, $sel)
                _ArraySwap($array[$cnt], $array[$cnt - 1])
                GUISetState(@SW_HIDE, $Form1)
                _listview()
            Case $GUI_EVENT_CLOSE
                exit
        EndSwitch
    WEnd
EndFunc  ;==>_listview

This will move the highlighted item up one position everytime the up button is pushed. But I really need it to read the checked item instead of the highlighted item. Also hoping someone could point me to a way to refresh the listview and keep the selected item selected.

Link to comment
Share on other sites

Well I have it now so that I can move the selected item up or down in both the listview and the array. Is there a way to refresh the listview without having to delete and then rebuild the gui?

Come on now, 46 views. someone, somewhere must have a helpfull tip. If someone wants to give it a try then just go to any folder with tif, jpg or pdfs, select several and open.

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


$_1 = GUICreate("Jesse's Plot Wizard", 395, 389, 325, 178)
Local $filemenu, $exititem, $helpmenu, $infoitem
$filemenu = GUICtrlCreateMenu("&File")
$fileitem = GUICtrlCreateMenuItem("Open", $filemenu)
$exititem = GUICtrlCreateMenuItem("Exit", $filemenu)
GUISetState(@SW_SHOW)
Local $_1_AccelTable[3][2] = [["^!+4", $filemenu],["{NUM 5}", $fileitem],["{UP}", $infoitem]]
GUISetAccelerators($_1_AccelTable)
$cnt = 0

While 1
    Dim $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $fileitem
            Dim $folder = "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}"
            $file = FileOpenDialog("", $folder, "Images (*.tif;*.jpg;*.pdf)", 1 + 4)
            $array = StringSplit($file, "|")
            If $array[0] = 1 Then
                $len = StringLen($folder)
                $trim = $len - StringInStr($folder, "\", 0, -1, $len)
            EndIf
            _ArraySort($array, 0, 2)
            _listview()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

Func _listview()
    $child = GUICreate("Arrange Array", 200, 400)
    $listview = GUICtrlCreateListView("", 10, 10, 180, 350, $LVS_SINGLESEL)
    GUISetState()
    _GUICtrlListView_AddColumn($listview, "Items", 100)
    $up = GUICtrlCreateButton("UP",20,370,50,20)
    $down = GUICtrlCreateButton("DOWN",130,370,50,20)
    $filecount = $array[0]
    For $incr = 2 To $filecount
        _GUICtrlListView_AddItem($listview, $array[$incr])
    Next
    _GUICtrlListView_SetItemSelected($listview, $cnt)
    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $up
                $cnt = _GUICtrlListView_GetNextItem($listview)
                _ArraySwap($array[$cnt + 2], $array[$cnt + 1])
                $cnt -= 1
                GUIDelete()
                _listview()
            Case $GUI_EVENT_CLOSE
                exit
            Case $down
                $cnt = _GUICtrlListView_GetNextItem($listview)
                _ArraySwap($array[$cnt + 2], $array[$cnt + 3])
                $cnt += 1
                GUIDelete()
                _listview()
        EndSwitch
    WEnd
EndFunc ;==>_listview
Edited by JesseBarnett
Link to comment
Share on other sites

I have a very similar problem, with the desire to create a manually sorted listview item. _GUICtrlListView_InsertItem() works great, but items created with this function cannot be deleted, thus they can only be manually sorted once. There's got to be a simple solution... I don't see a move function in the UDFs or regular functions. You could update the text in the two indices you want to switch using _GUICtrlListView_SetItemText(). That should solve your problem.

Link to comment
Share on other sites

I haven't actually looked though the code much as I'm used to seeing it using the other method of Gui control.

Opt("GUIOnEventMode", 1) It seems more natural to me somehow. Most of the examples are your way however. Drives me nuts trying to figure it all out.

But I did run it. This listview part is similar to something I was working on recently.

Two things.

You can just delete the listview itself and redisplay it instead of rebuilding the gui. That should help get rid of the flicker whenever you press a button. Use a controlfocus to put focus back on the listview after it gets recreated to make the hightlight line look likes its moving with each button press without any flicker.

You should also check to see if your at the top or bottom of the list when you press the button. you get some interesting results if at the top already and an error if at the bottom.

I know drag and drop is possible. I've looked though some of the stuff for it in the help files. But have not yet got it going myself. If you do get that part going, please post it.

Link to comment
Share on other sites

Thanks everyone!. crzftx's suggestion was what I needed to get it working. Now it works fine without having to create/delete the gui and the selected item stays selected until you select a different entry. I will be looking at dragging after I get the main script up and running. Also am trying to wrap my head around being able to select multiple items at one time.

I will be adding in array boundary checks, I am new to Autoit but I learned from other languages to keep it simple until I get the basics working then add in more, one layer at a time. In this specific case, my main script is up around 800 lines, but I work in side scripts and then add it into the main script when it works. The only nasty surprise I had with this listview was adding in a second Case set which meant 2 different $GUI_EVENT_CLOSE. ExitLoop stopped the mother gui from processing the child gui $GUI_EVENT_CLOSE.

I will also be looking at learning how to convert my main script over to onevent mode to see the difference.

Func _listview()
    $child = GUICreate("Arrange Array", 200, 400)
    $listview = GUICtrlCreateListView("", 10, 10, 180, 350)
    GUISetState()
    _GUICtrlListView_AddColumn($listview, "Items", 100)
    $up = GUICtrlCreateButton("UP", 20, 370, 50, 20)
    $down = GUICtrlCreateButton("DOWN", 130, 370, 50, 20)
    $filecount = $array[0]
    For $incr = 2 To $filecount
        _GUICtrlListView_AddItem($listview, $array[$incr])
    Next
        While 1
        $Msg = GUIGetMsg()
        Switch $Msg 
            Case $up
                $cnt = _GUICtrlListView_GetNextItem($listview)
                _ArraySwap($array[$cnt + 2], $array[$cnt + 1])
                _GUICtrlListView_SetItemText($listview, $cnt, $array[$cnt + 2])
                _GUICtrlListView_SetItemText($listview, $cnt - 1, $array[$cnt + 1])
                _GUICtrlListView_SetItemSelected($listview, $cnt - 1, True)
            Case $GUI_EVENT_CLOSE 
                GUIDelete($child)
                ExitLoop
            Case $down
                $cnt = _GUICtrlListView_GetNextItem($listview)
                _ArraySwap($array[$cnt + 2], $array[$cnt + 3])
                _GUICtrlListView_SetItemText($listview, $cnt, $array[$cnt + 2])
                _GUICtrlListView_SetItemText($listview, $cnt + 1, $array[$cnt + 3])
                _GUICtrlListView_SetItemSelected($listview, $cnt + 1, True)
        EndSwitch
    WEnd
EndFunc  ;==>_listview
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...