How to clear last wxListBox result? Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
FlyingIsFun1217
Super wx Problem Solver
Super wx Problem Solver
Posts: 497
Joined: Mon Nov 06, 2006 9:58 pm

How to clear last wxListBox result?

Post by FlyingIsFun1217 » Sat Feb 18, 2012 1:13 am

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

Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Re: How to clear last wxListBox result?

Post by Auria » Sat Feb 18, 2012 1:31 am

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
"Keyboard not detected. Press F1 to continue"
-- Windows

FlyingIsFun1217
Super wx Problem Solver
Super wx Problem Solver
Posts: 497
Joined: Mon Nov 06, 2006 9:58 pm

Re: How to clear last wxListBox result?

Post by FlyingIsFun1217 » Sat Feb 18, 2012 2:17 am

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

Post Reply