Page 1 of 1

How to clear last wxListBox result?

Posted: Sat Feb 18, 2012 1:13 am
by FlyingIsFun1217
Hello again, full of questions tonight:

I have a wxListBox, of which I am allowing the user to delete items through a button. When I do deletion, I check which array item is selected, remove that result, and everything is fine... up until the user attempts to delete the last result. Since I'm essentially removing the selected item from the wxArrayString, I'm guessing the problem is coming from attempting to redraw the list with no items in that array.

Code: Select all

for(int i = 0; i <= (SymbolListStrings.GetCount() - 1); i++)
	{
		if(SymbolList -> IsSelected(i))
		{
			if(SymbolListStrings.GetCount() > 1)
			{
				SymbolListStrings.RemoveAt(i, 1);
			}
			
			else if(SymbolListStrings.GetCount() == 1)
			{
				SymbolListStrings.Clear();
				SymbolListStrings.Add(wxT(""));
			}
			
			SymbolList -> Set(SymbolListStrings);
		}
	}
SymbolListStrings is the wxArrayString, and SymbolList is the wxListBox. It works fine the way I have it in the above code, except that I don't want to have an item in the wxListBox that is empty. Is there any way to reset the wxListBox without any items in it?

FlyingIsFun1217

Re: How to clear last wxListBox result?

Posted: Sat Feb 18, 2012 1:31 am
by Auria
Sorry, your code doesn't make much sense.

Here is a quick trace.

Code: Select all

item count = 10.
i = 0.

remove item 0.
i++.

item count = 9.
i = 1.

remove item 1.
i++.

item count = 8.
i = 2.

remove item 2.
i++.

item count = 7.
i = 3.

remove item 3.
i++.

item count = 6.
i = 4.

remove item 4.
i++.

item count = 5.
i = 5.

remove item 5.
huh? can't remove item at index 5 (the 6th item in the list starting from 0), the list has only 5 elements. so the loop exits before we're done.
Another weird thing is that you call SymbolList -> Set on every loop iteration, this is most likely not necessary and slows things down uselessly.

I suggest you just create a new wxArrayString, iterate over the old one, and add the ones that are not selected to the new list. Then call SymbolList -> Set once at the end when you're done

Re: How to clear last wxListBox result?

Posted: Sat Feb 18, 2012 2:17 am
by FlyingIsFun1217
This is what I have now (and had before for that matter):

Code: Select all

for(int i = 0; i <= (SymbolListStrings.GetCount() - 1); i++)
	{
		std::cout << i << std::endl;
		
		if(SymbolList -> IsSelected(i))
		{
			SymbolListStrings.RemoveAt(i, 1);
			SymbolList -> Set(SymbolListStrings);
			break;
		}
	}
I simply cannot see how this would do what you are saying. The output to terminal even shows it iterating through the SymbolListStrings array, and stopping at the item that was selected. And I definitely can't say I see why creating a possibly infinite amount of wxArrayStrings when there's a funtion to remove items from the array. Maybe I've got my blinders on here, but I just can't see it.

FlyingIsFun1217

---------------EDIT---------------
Problem solved. In the translation of GetCount() - 1, 0-1=4294967295, so the type wasn't staying as an integer. Fixed it by type conversion, so that -1 is the output.

FlyingIsFun1217