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:
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
Can't you just do something like this (untested) ?
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.
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.
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
That is still in a while loop though. I'm suggesting that you remove the while loop.
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