Removing a specific entry by name from a listbox

Started by JD, Thu 13/04/2006 21:27:39

Previous topic - Next topic

JD

In my game I use a listbox to keep track of quests the main character is on. Adding new quests to the listbox works and when you click on a quest it shows a description of the quest in a text box next to it. So far so good. But what if I want to remove a specific entry from the listbox, for when the character completes a quest. I looked up the RemoveItem command, but I saw this only lets you remove entries by number. Like the first one in the listbox, or the third one. In my case the game never knows in which order the player picks up the quests, so this won't be of much help I guess. Is it possible to remove entries using something similar to RemoveItem("name of entry") for example? Perhaps by using.. magic?

Thanks in advance,

-Def

GarageGothic

#1
Not having tested this, it may require a few changes to work:

Code: ags

function RemoveTextItem(String nameofentry) {
   int index;
   while (index < lstQuests.ItemCount) {
      if (lstQuests.Items[index].CompareTo(nameofentry) == 0) lstQuests.RemoveItem(index);
      else index++;
      }
   }

DoorKnobHandle

GarageGothic was faster, but I am still posting because my function does support different GUIs to be used and uses a more OOP-approach... ;)

Assuming you use AGS 2.71:

Code: ags

function RemoveListboxItemByName ( GUIControl* listbox, String name )
{
	int i = 0;
	while ( i < listbox.AsListBox.ItemCount )
	{
		if ( listbox.AsListBox.Items[i] == name )
			listbox.AsListBox.RemoveItem ( i );

		i++;
	}
}


This is absolutely untested and from the top of my head, but it should work. This function simply loops through all items in your listbox and then checks if the name of the item that you want to delete equals the one that we're currently looking at. If they equal, the function removes that entry.

JD

Thanks both of you, thats a logical way to do it! I tried GarageGothic's code and it was flawless. I also tried dkh's code and it worked too. Solved!

Thanks again.

SMF spam blocked by CleanTalk