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