Removing single list item from duplicates

Started by The creature, Thu 10/09/2020 13:35:38

Previous topic - Next topic

The creature

Hi

I'm having an issue with Listboxes. I have a listbox that can have multiples duplicate items. The issue is I want to remove a selected item from the listbox, but as expected it's removing all duplicates of that name.

Is there a way to just remove a single duplicate entry?

My code follows:

Code: ags
String EquipTool1 = ListBox_Toolbox.Items[ListBox_Toolbox.SelectedIndex];
    if (EquipTool1 == "Crowbar")
    {
      int i = 0;
        while (i < ListBox_Toolbox.ItemCount)
        {
          if (ListBox_Toolbox.Items[i] == "Crowbar")
          {
            bD0_Tool1.NormalGraphic=439;
            diver0_tool1=2; // Crowbar
            ListBox_Toolbox.RemoveItem(i);
            ListBox_Toolbox.InsertItemAt(0, "Unequip");
          }
        i++;
        }
      return;
    }


As described in my question, if there are multiple Crowbar items in the list it obviously removes all crowbar items.

Any help would be greatly appreciated in this matter.

C

morganw

Can't you just do something like this (untested) ?

Code: ags
String EquipTool1 = ListBox_Toolbox.Items[ListBox_Toolbox.SelectedIndex];
if (EquipTool1 == "Crowbar")
{
  bD0_Tool1.NormalGraphic=439;
  diver0_tool1=2; // Crowbar
  ListBox_Toolbox.RemoveItem(ListBox_Toolbox.SelectedIndex);
  ListBox_Toolbox.InsertItemAt(0, "Unequip");
  return;
}


You already have the index of what is selected, so I don't think you need to search for it.

The creature

#2
I tried that but was presented with this error:

ListboxRemove: invalid listindex specified.
I was curious if it was because it was removing an item that was currently selected so I tried adding: ListBox_Toolbox.SelectedIndex=-1; as that has sometimes caused issue in the past. But no avail. And it's just dawned on me that my unselecting it ListBox_Toolbox.RemoveItem(ListBox_Toolbox.SelectedIndex); wouldn't work anyway...but it didn't work before either.

Code: ags
   if (EquipTool1 == "Crowbar")
    {
      int i = 0;
        while (i < ListBox_Toolbox.ItemCount)
        {
          if (ListBox_Toolbox.Items[i] == "Crowbar")
          {
            bD0_Tool1.NormalGraphic=439;
            diver0_tool1=2; // Crowbar
            ListBox_Toolbox.SelectedIndex=-1;
            ListBox_Toolbox.RemoveItem(ListBox_Toolbox.SelectedIndex);
          }
        i++;
        }
      return;
    }


C

morganw

That is still in a while loop though. I'm suggesting that you remove the while loop.

The creature

Yeah that works great!

I'm now not even sure why I was having all that in a loop to be honest. works a charm.

Thank you!
C

SMF spam blocked by CleanTalk