1. Need A Quick Fix
- Posted by Icy_Viking Nov 03, 2019
- 1466 views
Hey Guys,
Every time I try to remove a selected item from a listbox, my program crashes.
sequence nesgames = {"Contra","Ducktales","Super Mario Bros.","Super Mario Bros. 2","Super Mario Bros. 3", "Zelda 2: Link's Adventure"} procedure Add_Game(integer self,integer event, sequence parm) addItem(Game_List,nesgames) end procedure setHandler(Game_List_Add,w32HClick,routine_id("Add_Game")) procedure Remove_Game(integer self, integer event, sequence parm) for i = 0 to length(nesgames) do if i >= 0 then deleteItem(Game_List,nesgames[i]) --somewhere here causes it to crash end if end for end procedure setHandler(Game_List_Remove,w32HClick,routine_id("Remove_Game"))
2. Re: Need A Quick Fix
- Posted by Senator Nov 04, 2019
- 1376 views
Hey Guys,
Every time I try to remove a selected item from a listbox, my program crashes.
sequence nesgames = {"Contra","Ducktales","Super Mario Bros.","Super Mario Bros. 2","Super Mario Bros. 3", "Zelda 2: Link's Adventure"} procedure Add_Game(integer self,integer event, sequence parm) addItem(Game_List,nesgames) end procedure setHandler(Game_List_Add,w32HClick,routine_id("Add_Game")) procedure Remove_Game(integer self, integer event, sequence parm) for i = 0 to length(nesgames) do if i >= 0 then deleteItem(Game_List,nesgames[i]) --somewhere here causes it to crash end if end for end procedure setHandler(Game_List_Remove,w32HClick,routine_id("Remove_Game"))
I'll take a stab at this:
If i = 0 then there is no element in the sequence nesgames to delete from Game_List.
If your click/selection routine returns a value based on the first item in the selection list having an index value 0f 0, then you will need to add 1 to that value to reference the index the item in the nesgames sequence you want to delete. Not sure why you would need a loop or a special DeleteItem routine to do that though, it seems Game_List = remove_item(Game_List,nesgames[i+ClickValue]) should suffice.
I probably shouldn't have tried to offer a solution, since I do not know how the Windows GUI works.
3. Re: Need A Quick Fix
- Posted by irv Nov 04, 2019
- 1373 views
Did you want to remove all games? There's no need for a loop!
Here's the docs:
deleteItem() also works with list view and tree view controls. position should be the item id returned when the item was created. To delete all items in a list view or tree view, position should be -1.
deleteItem(Game_List,-1)
Did you intend to remove just one?
If so, note the docs - it does not expect the name of the item (e.g. "Ducktales"). You are passing a string, no surprise that it crashes.
It needs the item id of the selected item. Something like:
id = getIndex(Game_List) -- id of the selected item on the list if id > 0 then -- if no item is selected, this will be zero, so don't bother deleting deleteItem(Game_List,id) end if
Note: It has been 10 years since I did anything with Windows or Win32lib. Hope this is correct.
4. Re: Need A Quick Fix
- Posted by ghaberek (admin) Nov 04, 2019
- 1384 views
for i = 0 to length(nesgames) do if i >= 0 then deleteItem(Game_List,nesgames[i]) --somewhere here causes it to crash end if end for
You can't subscript a sequence with zero! We count from one around here!
As Irv pointed out, according to the Win32Lib docs, deleteItem expects an item position, not an item value
There are actually several ways to skin this cat...
Pass -1 to the position parameter of deleteItem(), as Irv pointed out:
deleteItem( list, -1 )
Call eraseItems() which does the same thing:
eraseItems( list )
Walk backwards through the list, deleting each item as you go:
for index = getCount( list ) to 1 by -1 do deleteItem( list, index ) end for
Keep deleting the first item until the list is empty:
while getCount( list ) do deleteItem( list, 1 ) end while
I'll also point out that if you run a for loop where stop < start, the loop will simply exit. This is useful since you don't have to check getCount() > 0 first, just run for i = 1 to getCount() do and it will just exit if getCount() returns zero.
-Greg
5. Re: Need A Quick Fix
- Posted by Icy_Viking Nov 04, 2019
- 1361 views
Thanks for all the help guys. It was a slight oversight on my part. I have it fixed.
procedure Remove_Game(integer self, integer event, sequence parm) deleteItem(Game_List,1) end procedure setHandler(Game_List_Remove,w32HClick,routine_id("Remove_Game"))
6. Re: Need A Quick Fix
- Posted by irv Nov 05, 2019
- 1289 views
Thanks for all the help guys. It was a slight oversight on my part. I have it fixed.
procedure Remove_Game(integer self, integer event, sequence parm) deleteItem(Game_List,1) end procedure setHandler(Game_List_Remove,w32HClick,routine_id("Remove_Game"))
Note: the code above will always remove the first item, if you want to remove the selected item, you need to get the selection by calling
getIndex(Game_List)
You could try doing it as follows, but should test to see what happens when no item is selected. (And what happens if multiple items can be selected)
deleteItem(Game_List,getItem(Game_List))
7. Re: Need A Quick Fix
- Posted by Icy_Viking Nov 06, 2019
- 1218 views
Well I was able to get it to delete one item at a time. However I must be doing something a little wrong since I can't get it to delete a selected item, or it crashes.
procedure Remove_Game(integer self, integer event, sequence parm) for i = 1 to length(Game_List) do getIndex(Game_List) deleteItem(Game_List,getItem(Game_List,i)) end for end procedure setHandler(Game_List_Remove,w32HClick,routine_id("Remove_Game"))
8. Re: Need A Quick Fix
- Posted by irv Nov 07, 2019
- 1177 views
Well I was able to get it to delete one item at a time. However I must be doing something a little wrong since I can't get it to delete a selected item, or it crashes.
procedure Remove_Game(integer self, integer event, sequence parm) for i = 1 to length(Game_List) do getIndex(Game_List) deleteItem(Game_List,getItem(Game_List,i)) end for end procedure setHandler(Game_List_Remove,w32HClick,routine_id("Remove_Game"))
getIndex() returns the index number(s) of the item(s) selected. You have to save that, and then delete the item or items, one at a time. Whether the index is an integer or a sequence of integers, depends upon which control is being used and whether single or multi-select is being done.
procedure Remove_Game(integer self, integer event, sequence parm) object index = getIndex(Game_List) ? index -- put this here for a peek at what is being returned if atom(index) and index > 0 then -- an item was selected, so... deleteItem(Game_List,index) else -- multiple items were selected, so... for i = 1 to length(index) do -- process each item deleteItem(Game_List,index[i]) end for end if end procedure setHandler(Game_List_Remove,w32HClick,routine_id("Remove_Game"))
From the Win32 docs:
getIndex ( list ) Get the index of the selected item(s), or cursor position in an edit control. Returns: INTEGER: Index of selected item, or zero if no item is selected. Category: List Control For ListView and TreeView, this returns a sequence of all selected items. For MleEdit, EditText and RichEdit, this returns the cursor position. For List, this returns the index of the currently selected item.
If you are sure you will never select more than one item at a time, you can do away with the for...loop.