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.
no subject
Date: March 30th, 2005 03:02 pm (UTC)From:no subject
Date: March 30th, 2005 03:06 pm (UTC)From:no subject
Date: March 30th, 2005 09:27 pm (UTC)From:no subject
Date: March 30th, 2005 10:05 pm (UTC)From:no subject
Date: October 17th, 2005 04:37 pm (UTC)From: (Anonymous)Well, I found your tip and it was exactly what I was looking for! Or so I thought, your solution doesn't work for my code but I'm still glad to have found someone else that has had this problem. I've Googled and searched MSDN but nothing. Your post is the only thing I've found so far...
So thanks for the tip!
//Girlie
no subject
Date: October 17th, 2005 04:44 pm (UTC)From:Too many items in List Box - caused by ToString() override returning nothing
Date: December 21st, 2005 04:29 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
"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:no subject
Date: January 4th, 2007 07:04 pm (UTC)From:no subject
Date: January 4th, 2007 07:08 pm (UTC)From:Re: Too many items in List Box - caused by ToString() override returning nothing
Date: May 2nd, 2007 01:26 pm (UTC)From: (Anonymous)I got the same problem in C# and thanks to your comment it got fixed. My class with an overridden ToString() returned nothing too.
Regards,
Samuel
Hello
Date: August 16th, 2008 10:03 am (UTC)From: (Anonymous)Re: Hello
Date: August 18th, 2008 08:11 am (UTC)From: