Sign in to follow this  
Followers 0
Melba23

ListView - Move item up/Down

6 posts in this topic

In response to a PM, here is some working code which moves items up and down a ListView. This is extracted and modified from a larger script, so it might not be as tight as it could be, but I am not prepared to fine tune it. :-)

You want more features - you try to code them and ask if you run into problems:

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

Global $aList_Array[1]
For $i = 1 To 20
    _ArrayAdd($aList_Array, $i & "-----" & $i & "-----" & $i)
    $aList_Array[0] += 1
Next

; Create dialog
Local $hList_Win = GUICreate("UP/Down ListView", 500, 500, Default, Default, BitOr($WS_POPUPWINDOW, $WS_CAPTION))
    GUISetBkColor(0xCECECE)
    
; Create list box with no col headers and permit only 1 selection
Local $hListView = GUICtrlCreateListView ("_", 10, 10, 480, 390, $LVS_NOCOLUMNHEADER + $LVS_SINGLESEL, $LVS_EX_FULLROWSELECT )
; Set up alt line colouring
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_LV_ALTERNATE)
; Set col width
_GUICtrlListView_SetColumnWidth(-1, 0, $LVSCW_AUTOSIZE_USEHEADER)

; Create buttons
Local $hList_Move_Up_Button   = GUICtrlCreateButton("Move Up",   270, 410, 80, 30)
Local $hList_Move_Down_Button = GUICtrlCreateButton("Move Down", 270, 450, 80, 30)
Local $hList_Cancel_Button  = GUICtrlCreateButton("Cancel", 370, 410, 80, 30)

; Fill listbox, list handles indexed on this dummy
Local $hStart_ID = GUICtrlCreateDummy()     

; Fill listbox
; Add placeholders for the list items
    For $i = 1 To 20
        GUICtrlCreateListViewItem($aList_Array[$i], $hListView)
            GUICtrlSetBkColor(-1, 0xCCFFCC)
    Next

; Show dialog
GUISetState(@SW_SHOW, $hList_Win)

While 1

    Local $aMsg = GUIGetMsg(1)
    
    If $aMsg[1] = $hList_Win Then
    
        Switch $aMsg[0]
            Case $GUI_EVENT_CLOSE, $hList_Cancel_Button
                Exit
            Case $hList_Move_Up_Button
                List_Item_Up($aList_Array, $hListView, $hStart_ID)
            Case $hList_Move_Down_Button
                List_Item_Down($aList_Array, $hListView, $hStart_ID)
        EndSwitch
        
    EndIf
            
Wend

; -------

Func List_Item_Up(ByRef $aList_Array, $hListView, $hStart_ID)

If $aList_Array[0] < 2 Then Return

; Get value of listview selection via handle count
$iList_Index = GUICtrlRead($hListView) - $hStart_ID
; If already at top or no selection or out of range
If $iList_Index < 2 Or $iList_Index > $aList_Array[0] Then Return

; Swap array elements
_ArraySwap($aList_Array[$iList_Index],  $aList_Array[$iList_Index - 1])

; Rewrite list items
For $i = 1 To $aList_Array[0]
    GUICtrlSetData($hStart_ID + $i, $aList_Array[$i])
Next

; Unselect all items to force selection before next action
_GUICtrlListView_SetItemSelected ($hListView, -1, False)

EndFunc

; -------

Func List_Item_Down(ByRef $aList_Array, $hListView, $hStart_ID)

If $aList_Array[0] < 2 Then Return

; Get value of listview selection via handle count
$iList_Index = GUICtrlRead($hListView) - $hStart_ID
; If already at bottom or no selection or out of range
If $iList_Index < 1 Or $iList_Index > $aList_Array[0] - 1 Then Return

; Swap array elements
_ArraySwap($aList_Array[$iList_Index], $aList_Array[$iList_Index + 1])

; Rewrite list items
For $i = 1 To $aList_Array[0]
    GUICtrlSetData($hStart_ID + $i, $aList_Array[$i])
Next

; Unselect all items to force selection before next action
_GUICtrlListView_SetItemSelected ($hListView, -1, False)

EndFunc

For any onlookers and newcomers: Do NOT rpt NOT PM asking for help - the idea of this forum is to help everyone, so open a thread if you have a question.

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



Hi, thank you for sharing.

For fine tuning you could just swap text of the previous or next selected ListView Item instead of looping through the whole lot of listview of items and setting data.

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

Global $hGui, $LV, $Up, $Down, $msg

$hGui = GUICreate("UP/Down ListView", 290, 340)
$LV = GUICtrlCreateListView ("ListView Items", 10, 10, 220, 320)
For $i = 1 To 20
    GUICtrlCreateListViewItem("Item " & $i, $LV)
Next
$Up = GUICtrlCreateButton("Up", 240, 30, 40, 20)
$Down = GUICtrlCreateButton("Down", 240, 60, 40, 20)
GUISetState(@SW_SHOW, $hGui)    

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Up
            If UD($LV, 0) = -1 Then MsgBox(64, "Top", "Item is at the top of the ListView", 1)
        Case $Down
            If UD($LV, 1) = -1 Then MsgBox(64, "Bottom", "Item is at the bottom of the ListView", 1)
    EndSwitch
Wend

Func UD($iCID, $iFlag)
    Local $iCnt, $sCur,$iCur, $sNxt, $iNew = -1
    $iCur = _GUICtrlListView_GetNextItem($iCID)
    $sCur = _GUICtrlListView_GetItemText($iCID, $iCur)
    $iCnt = _GUICtrlListView_GetItemCount($iCID)
    If $iFlag And $iCur < ($iCnt - 1) Then
        $iNew = $iCur + 1
    ElseIf Not $iFlag And $iCur > 0 Then
        $iNew = $iCur - 1
    EndIf
    If $iNew = -1 Then Return $iNew
    $sNxt = _GUICtrlListView_GetItemText($iCID, $iNew)
    _GUICtrlListView_SetItemText($iCID, $iNew, $sCur)
    _GUICtrlListView_SetItemText($iCID, $iCur, $sNxt)
    _GUICtrlListView_SetItemSelected($iCID, $iNew, 1)       
EndFunc

Share this post


Link to post
Share on other sites

Nice idea, but it's done already before: _GUICtrlListView_MoveItems - UDF.

P.S

Updated with new version wich preserves the checkbox states and images list of the items.


 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites

smashly,

I agree with your tightening suggestion for stand alone code - but as I explained, the code was extracted from a larger script and I need the array in matching order for other parts of the code!

MrCreatoR,

I shall examine your UDF closely and steal (sorry, plagiarise!) what I can from it. :-)

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

Hello Melba23,

thanks again for the great help. I've tried adding a function to delete an item from the list, but unfortunately it doesn't work the way it is supposed to. As I am an AutoIt beginner I've added some comments for myself to clarify what is going on in the script, so sorry if it got a bit longer :(

Have a look at my Delete_item function: if I try deleting items from the end of the list it seems to work, but if I start somewhere in the middle it results in some mess ^_^ I guess I don't get the point of the $hStart_ID dummy, maybe you can explain it in short? As far as I understand the overall number of items in the list is kept in $aList_Array[0] as an integer. But how does GUICtrlRead($hListView) - $hStart_ID relate to the current position ($hStart_ID seems to equal 8 all the time)?

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

Global $aList_Array[1]
; Fill the array with some test data
For $i = 1 To 5
    _ArrayAdd($aList_Array, $i & "-----" & $i & "-----" & $i)
    $aList_Array[0] += 1 
    ConsoleWrite ( "array elements " & $aList_Array[$i] &@CRLF)
    ; ==> the first item in the array holds information on the overall item count (eg. a $aList_Array[0]=23 means there are 23 items in the list after the for loop)
Next
ConsoleWrite ( "array counter " & $aList_Array[0] &@CRLF)

; Create dialog
Local $hList_Win = GUICreate("UP/Down ListView", 500, 500, Default, Default, BitOr($WS_POPUPWINDOW, $WS_CAPTION))
    ; $WS_POPUPWINDOW > enables the WS_BORDER, WS_POPUP, and WS_SYSMENU styles
    ; $WS_CAPTION > window has a title bar
    GUISetBkColor(0xCECECE)
    
; Create list box with no col headers and permit only 1 selection
Local $hListView = GUICtrlCreateListView ("_", 10, 10, 480, 390, $LVS_NOCOLUMNHEADER + $LVS_SINGLESEL, $LVS_EX_FULLROWSELECT )
; Set up alt line colouring
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_LV_ALTERNATE)
; Set col width
_GUICtrlListView_SetColumnWidth(-1, 0, $LVSCW_AUTOSIZE_USEHEADER)

; Create buttons
Local $delete_item_Button     = GUICtrlCreateButton("Delete Item",   170, 410, 80, 30)
Local $hList_Move_Up_Button   = GUICtrlCreateButton("Move Up",   270, 410, 80, 30)
Local $hList_Move_Down_Button = GUICtrlCreateButton("Move Down", 270, 450, 80, 30)
Local $hList_Cancel_Button    = GUICtrlCreateButton("Cancel",    370, 410, 80, 30)

; Fill listbox, list handles indexed on this dummy
Local $hStart_ID = GUICtrlCreateDummy()        
ConsoleWrite ("hStart_ID is " &$hStart_ID &@CRLF )

; Fill listbox
; Add placeholders for the list items
    For $i = 1 To $aList_Array[0]
        GUICtrlCreateListViewItem($aList_Array[$i], $hListView)
            GUICtrlSetBkColor(-1, 0xCCFFCC)
    Next

; Show dialog
GUISetState(@SW_SHOW, $hList_Win)

While 1

    Local $aMsg = GUIGetMsg(1) 
    ;==> in the MessageLoop mode the (1) param returns an array, where $aMsg[0] contains the event
    ;and $aMsg[1] the window handle.
    
    If $aMsg[1] = $hList_Win Then ; check if the current window in focus corresponds to the GUI "$hList_Win" 
    
        Switch $aMsg[0] 
            Case $GUI_EVENT_CLOSE, $hList_Cancel_Button ; close app
                Exit
            Case $hList_Move_Up_Button
                List_Item_Up($aList_Array, $hListView, $hStart_ID) ;call the function for item up
            Case $hList_Move_Down_Button
                List_Item_Down($aList_Array, $hListView, $hStart_ID) ;call the function for item down
            Case $delete_item_Button 
                Delete_Item($aList_Array, $hListView, $hStart_ID) ;call the function for item down
        EndSwitch
        
    EndIf
            
Wend

; -------

Func List_Item_Up(ByRef $aList_Array, $hListView, $hStart_ID)
;==> ByRef passes the $aList_Array as reference to this function, so modifications to the array will
; be reflected in the Main function (in this case GUI declaration), where the array was initialized.

If $aList_Array[0] < 2 Then Return

; Get value of listview selection via handle count
$iList_Index = GUICtrlRead($hListView) - $hStart_ID
; If already at top or no selection or out of range
If $iList_Index < 2 Or $iList_Index > $aList_Array[0] Then Return

; Swap array elements
_ArraySwap($aList_Array[$iList_Index],  $aList_Array[$iList_Index - 1])

; Rewrite list items
For $i = 1 To $aList_Array[0]
    GUICtrlSetData($hStart_ID + $i, $aList_Array[$i])
Next

; Unselect all items to force selection before next action
;_GUICtrlListView_SetItemSelected ($hListView, -1, False)
_GUICtrlListView_SetItemSelected ($hListView, $iList_Index, True, True)

EndFunc

; -------

Func List_Item_Down(ByRef $aList_Array, $hListView, $hStart_ID)

If $aList_Array[0] < 2 Then Return

; Get value of listview selection via handle count
$iList_Index = GUICtrlRead($hListView) - $hStart_ID
; If already at bottom or no selection or out of range
If $iList_Index < 1 Or $iList_Index > $aList_Array[0] - 1 Then Return

; Swap array elements
_ArraySwap($aList_Array[$iList_Index], $aList_Array[$iList_Index + 1])

; Rewrite list items
For $i = 1 To $aList_Array[0]
    GUICtrlSetData($hStart_ID + $i, $aList_Array[$i])
Next

; Unselect all items to force selection before next action
_GUICtrlListView_SetItemSelected ($hListView, -1, False)

EndFunc

Func Delete_Item(ByRef $aList_Array, $hListView,$hStart_ID)
    ; Get value of listview selection via handle count
    $iList_Index = GUICtrlRead($hListView) - $hStart_ID
    ConsoleWrite ( "GUICtrlRead($hListView) returns   " & GUICtrlRead($hListView) &@CRLF)
    ConsoleWrite ( "iList_Index is now  " & $iList_Index &@CRLF) 
    
    ; If no selection or out of range
    If $iList_Index > $aList_Array[0] Then Return

    ; delete array element 
    _ArrayDelete($aList_Array,$iList_Index)
    
    if ($aList_Array[0] > 0) Then    ;trying to prevent the script from crashing when deleting last element :(
        $aList_Array[0] -= 1
    EndIf
    
    ;_GUICtrlListView_DeleteAllItems($hListView)
    _GUICtrlListView_DeleteItem($hListView, $iList_Index) ; >> will delete the particular item
    

    ;debug after delete
    For $i = 1 To $aList_Array[0]
    ConsoleWrite ( "array elements " & $aList_Array[$i] &@CRLF)
    Next
    ConsoleWrite ( "array counter " & $aList_Array[0] &@CRLF)
    ;populate the listview again
    #cs
    For $i = 1 To $aList_Array[0]
        _GUICtrlListView_AddItem ($hListView, $aList_Array[$i])
        ;GUICtrlCreateListViewItem($aList_Array[$i], $hListView)
        GUICtrlSetBkColor(-1, 0xCCFFCC)
    Next
    #ce
    
    ; Rewrite list items - does it stay the same?
    For $i = 1 To $aList_Array[0]
        GUICtrlSetData($hStart_ID + $i, $aList_Array[$i]) ;
        ;GUICtrlSetData($hListView, $aList_Array[$i]);
        ConsoleWrite ( "array counter " & $aList_Array[0] &@CRLF)
    Next
    
EndFuncoÝ÷ Ø l£^*.ºÇ­éZ²×è®Z(¥«­¢+Ù½ÈÀÌØí¤ôÄQ¼ÀÌØí1¥ÍÑ}ÉÉålÁt(U%
ÑɱMÑÑ ÀÌØí¡MÑÉÑ}%¬ÀÌØí¤°ÀÌØí1¥ÍÑ}ÉÉålÀÌØí¥t¤ì(U%
ÑɱMÑ   ­
½±½È ´Ä°Áá


¤)9áÐ

is that the place where the listview gets actually refreshed? Furthermore I can never delete the last element in the list ;)

Sorry for the mess in the code, and thanks again!

Best,

Gabriel

Share this post


Link to post
Share on other sites

GabrielSumner,

Sorry I do not have time to debug your code at the moment - I have to go out (well, it is Friday night!). However, I have added "Add" and "Delete" buttons to the code I posted earlier. Have a play with that to be going on with. :-)

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

Global $aList_Array[1]
For $i = 1 To 20
    _ArrayAdd($aList_Array, $i & "-----" & $i & "-----" & $i)
    $aList_Array[0] += 1
Next

; Create dialog
Local $hList_Win = GUICreate("UP/Down ListView", 500, 500, Default, Default, BitOr($WS_POPUPWINDOW, $WS_CAPTION))
    GUISetBkColor(0xCECECE)
    
; Create list box with no col headers and permit only 1 selection
Local $hListView = GUICtrlCreateListView ("_", 10, 10, 480, 390, $LVS_NOCOLUMNHEADER + $LVS_SINGLESEL, $LVS_EX_FULLROWSELECT )
; Set up alt line colouring
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_LV_ALTERNATE)
; Set col width
_GUICtrlListView_SetColumnWidth(-1, 0, $LVSCW_AUTOSIZE_USEHEADER)

; Create buttons
Local $hList_Add_Button    = GUICtrlCreateButton("Insert",  170, 410, 80, 30)
Local $hList_Delete_Button  = GUICtrlCreateButton("Delete", 170, 450, 80, 30)
Local $hList_Move_Up_Button   = GUICtrlCreateButton("Move Up",   270, 410, 80, 30)
Local $hList_Move_Down_Button = GUICtrlCreateButton("Move Down", 270, 450, 80, 30)
Local $hList_Cancel_Button  = GUICtrlCreateButton("Cancel", 370, 410, 80, 30)

; Fill listbox, list handles indexed on this dummy
Local $hStart_ID = GUICtrlCreateDummy()     

; Fill listbox
; Add placeholders for the list items
    For $i = 1 To 20
        GUICtrlCreateListViewItem($aList_Array[$i], $hListView)
            GUICtrlSetBkColor(-1, 0xCCFFCC)
    Next

; Show dialog
GUISetState(@SW_SHOW, $hList_Win)

While 1

    Local $aMsg = GUIGetMsg(1)
    
    If $aMsg[1] = $hList_Win Then
    
        Switch $aMsg[0]
            Case $GUI_EVENT_CLOSE, $hList_Cancel_Button
                Exit
            Case $hList_Add_Button
                List_Item_Insert($aList_Array, $hListView, $hStart_ID)
            Case $hList_Delete_Button
                List_Item_Delete($aList_Array, $hListView, $hStart_ID)
            Case $hList_Move_Up_Button
                List_Item_Up($aList_Array, $hListView, $hStart_ID)
            Case $hList_Move_Down_Button
                List_Item_Down($aList_Array, $hListView, $hStart_ID)
        EndSwitch
        
    EndIf
            
Wend

Func List_Item_Insert(ByRef $aList_Array, $hListView, $hStart_ID)

; If list is full, return
If $aList_Array[0] = 20 Then Return

; If list is empty, add first value
If $aList_Array[0] = 0 Then
    $iList_Index = 0
Else
; Get value of listview selection via handle count
    $iList_Index = GUICtrlRead($hListView) - $hStart_ID
; If no selection or out of range assume add to end
    If $iList_Index < 0 Or $iList_Index > $aList_Array[0] Then $iList_Index = $aList_Array[0]
EndIf

; Add tracks to the Playlist
Local $sSelection = InputBox("", "Enter a a value")
If @error Then Return

; Insert in array
_ArrayInsert($aList_Array, $iList_Index + 1, $sSelection)
$aList_Array[0] += 1
; Rewrite list items
For $i = 1 To $aList_Array[0]
    GUICtrlSetData($hStart_ID + $i, $aList_Array[$i])
Next

; Set highlight to inserted item
GUICtrlSetState($hListView, $GUI_FOCUS)
_GUICtrlListView_SetItemSelected ($hListView, $iList_Index)
_GUICtrlListView_EnsureVisible ($hListView, $iList_Index)

EndFunc

; -------

Func List_Item_Delete(ByRef $aList_Array, $hListView, $hStart_ID)

; If list is empty, return
If $aList_Array[0] = 0 Then Return

; Get value of listview selection via handle count
$iList_Index = GUICtrlRead($hListView) - $hStart_ID
; If no selection or out of range
If $iList_Index < 1 Or $iList_Index > $aList_Array[0] Then Return

; Delete from array
_ArrayDelete($aList_Array, $iList_Index)
$aList_Array[0] -= 1

; Rewrite list items
For $i = 1 To $aList_Array[0]
    GUICtrlSetData($hStart_ID + $i, $aList_Array[$i])
Next
; Blank last line
GUICtrlSetData($hStart_ID + $i, "")

; Unselect all items to force selection before next action
_GUICtrlListView_SetItemSelected ($hListView, -1, False)

EndFunc

; -------

Func List_Item_Up(ByRef $aList_Array, $hListView, $hStart_ID)

If $aList_Array[0] < 2 Then Return

; Get value of listview selection via handle count
$iList_Index = GUICtrlRead($hListView) - $hStart_ID
; If already at top or no selection or out of range
If $iList_Index < 2 Or $iList_Index > $aList_Array[0] Then Return

; Swap array elements
_ArraySwap($aList_Array[$iList_Index],  $aList_Array[$iList_Index - 1])

; Rewrite list items
For $i = 1 To $aList_Array[0]
    GUICtrlSetData($hStart_ID + $i, $aList_Array[$i])
Next

; Unselect all items to force selection before next action
_GUICtrlListView_SetItemSelected ($hListView, -1, False)

EndFunc

; -------

Func List_Item_Down(ByRef $aList_Array, $hListView, $hStart_ID)

If $aList_Array[0] < 2 Then Return

; Get value of listview selection via handle count
$iList_Index = GUICtrlRead($hListView) - $hStart_ID
; If already at bottom or no selection or out of range
If $iList_Index < 1 Or $iList_Index > $aList_Array[0] - 1 Then Return

; Swap array elements
_ArraySwap($aList_Array[$iList_Index], $aList_Array[$iList_Index + 1])

; Rewrite list items
For $i = 1 To $aList_Array[0]
    GUICtrlSetData($hStart_ID + $i, $aList_Array[$i])
Next

; Unselect all items to force selection before next action
_GUICtrlListView_SetItemSelected ($hListView, -1, False)

EndFunc

As before, the code is taken and modified from a larger script and so it is not as tight as it might be (for example, the array is used elsewhere and I am just taking advantage of it here to poulate the ListView).

The Dummy acts as a placeholder. The controls are created in order, so the Dummy is always just ahead of the first ListView item. That way you can get the array index by subtracting the Dummy's ControlID from the ListView item ControlID ($iList_Index = GUICtrlRead($hListView) - $hStart_ID).

I hope that is clearer.

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
Sign in to follow this  
Followers 0