idbirch Posted September 4, 2008 Share Posted September 4, 2008 Hi all, I'm trying to create a GUI application which lets users add and remove items from a list into a "basket". The GUI part works absolutely fine but the array in which I'm storing the final list of selected items is giving me a bit of grief. I'm using _ArrayAdd to add values and shockingly, _ArrayDelete to remove them. I need to be able to retrieve the number of items in the basket when the user clicks OK so I'm using _ArrayAdd and _ArrayDelete's return value to keep track of this and this is the problem: Adding to the array is fine, the value increments by 1 with every item added. If I then remove an item from the list, the value is not reduced, it remains the same. If I remove another item, the value then goes down by 1 and continues to do so with every subsequent removal, problem is of course, the value is +1 what it should be. Anyone know why this happens? I've written a very simple example which shows the problem: expandcollapse popup#include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <ListBoxConstants.au3> #include <WindowsConstants.au3> #include <Array.au3> #Include <GuiListBox.au3> Global $Array[1] Opt("GUIOnEventMode", 1) #Region ### START Koda GUI section ### Form= $Form1 = GUICreate("Form1", 229, 335, 193, 125) $List1 = GUICtrlCreateList("", 16, 16, 193, 240) $Button1 = GUICtrlCreateButton("Add", 32, 280, 51, 25, 0) GUICtrlSetOnEvent(-1, '_Add') $Button2 = GUICtrlCreateButton("Remove", 136, 280, 59, 25, 0) GUICtrlSetOnEvent(-1, '_Remove') GUISetState(@SW_SHOW) GUISetOnEvent($GUI_EVENT_CLOSE, "_Close") #EndRegion ### END Koda GUI section ### While 1 Sleep(100) WEnd Func _Add() $ValueToAdd = InputBox ( "Add to array", "Enter string to add to array", "" ) GUICtrlSetData ( $List1, $ValueToAdd ) $ArraySize = _ArrayAdd ( $Array, $ValueToAdd ) MsgBox (0, "Debug", "Array size is now " & $ArraySize ) EndFunc Func _Remove() $ValueToDelete = GuiCtrlRead ( $List1 ) $index = _GUICtrlListBox_GetCaretIndex ($List1) _GUICtrlListBox_DeleteString ( $List1, $index ) $ArraySize = _ArrayDelete ( $Array, $index ) MsgBox (0, "Debug", "Array size is now " & $ArraySize ) EndFunc Func _Close() Exit EndFunc Link to comment Share on other sites More sharing options...
PsaltyDS Posted September 4, 2008 Share Posted September 4, 2008 Hi all, I'm trying to create a GUI application which lets users add and remove items from a list into a "basket". The GUI part works absolutely fine but the array in which I'm storing the final list of selected items is giving me a bit of grief. I'm using _ArrayAdd to add values and shockingly, _ArrayDelete to remove them. I need to be able to retrieve the number of items in the basket when the user clicks OK so I'm using _ArrayAdd and _ArrayDelete's return value to keep track of this and this is the problem: Adding to the array is fine, the value increments by 1 with every item added. If I then remove an item from the list, the value is not reduced, it remains the same. If I remove another item, the value then goes down by 1 and continues to do so with every subsequent removal, problem is of course, the value is +1 what it should be. Anyone know why this happens? I've written a very simple example which shows the problem: The two functions do not return the same thing. The return value from _ArrayAdd() is the index of the added item, not the size of the array. Remember arrays are 0-based, so the index of the last item is one less than the size of the array, or count of total items. The return value from _ArrayDelete() is the new size of the array, not the index of the new last item. So the last item's index after the delete is one less than that. Demo: #include <Array.au3> Global $avArray[1] = [0] For $n = 1 To 5 $iRET = _ArrayAdd($avArray, $n) ConsoleWrite("Added, $iRET = " & $iRET & @LF) Next $avArray[0] = UBound($avArray) - 1 _ArrayDisplay($avArray, "$avArray") For $n = 5 To 1 Step -1 $iRET = _ArrayDelete($avArray, $n) ConsoleWrite("Deleted, $iRET = " & $iRET & @LF) Next $avArray[0] = UBound($avArray) - 1 _ArrayDisplay($avArray, "$avArray") Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law Link to comment Share on other sites More sharing options...
idbirch Posted September 5, 2008 Author Share Posted September 5, 2008 Ah. Oops. Another case of RTFM properly. I don't know where I got the idea that _ArrayAdd returned the new size, sorry about that. But thank you for your help and the code example. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now