The problem: I have a list box, into which I am placing objects by looping through the collection and calling Listbox.Items.Add. When I have one object only, it is not displayed in the box and when I try to add another item I get a System.OutOfMemoryException with the message that there are too many items in the list box.
The solution: It turned out, after much investigation that this problem was being caused by the fact that I was calling BeginUpdate before adding the single item to the box and EndUpdate afterwards. These are intended to avoid lots of re-draws when adding lots of items to a list box but apparently if you call them when only adding one thing, something somewhere breaks. Even adding 2 things, you're OK, but one thing, no. So all I did was add conditional code to not call them if I had one item to put in the listbox and bingo.
The solution: It turned out, after much investigation that this problem was being caused by the fact that I was calling BeginUpdate before adding the single item to the box and EndUpdate afterwards. These are intended to avoid lots of re-draws when adding lots of items to a list box but apparently if you call them when only adding one thing, something somewhere breaks. Even adding 2 things, you're OK, but one thing, no. So all I did was add conditional code to not call them if I had one item to put in the listbox and bingo.
"Too many items in List Box" error - caused by ToString override returning nothing
Date: December 21st, 2005 04:38 pm (UTC)From: (Anonymous)The classes I was adding had an override for the ToString property, overridden via a delegate.
One instance of the class returned nothing for the overridden ToString property.
I loaded up the checked list box during form load - no error.
The checked list box was on the second tab page of a tab control & when I clicked on that page hey presto, the error message was displayed ( ie when the overridden ToString sub returned nothing to the checked list box, when the checked list box was trying to display the text for each item in the list, which it does by calling the object's ToString method by default)
This list is still the only resource on the subject, and I found it via Google ;-)
Regards,
Graeme
ps VB.NET 2003, .NET Framework 1.1 SP1, XP PRo
Re: "Too many items in List Box" error - caused by ToString override returning nothing
Date: December 22nd, 2005 05:27 pm (UTC)From: