tinyjo: (webdesigner - chez geek)
Emptied of expectation. Relax. ([personal profile] tinyjo) wrote2005-03-30 03:40 pm

"Too many items in List Box"

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.

[identity profile] celestialweasel.livejournal.com 2005-03-30 03:02 pm (UTC)(link)
But do you think Google will ever scan your LJ :-)

[identity profile] tinyjo.livejournal.com 2005-03-30 03:06 pm (UTC)(link)
It'll find it eventually :)

[identity profile] shepline.livejournal.com 2005-03-30 09:27 pm (UTC)(link)
Google's found my lj, and has found Jo's via mine *grins*

[identity profile] celestialweasel.livejournal.com 2005-03-30 10:05 pm (UTC)(link)
Jolly good. It seems to have found mine. If I search for "Freedom Zone C" (in quotes) bizarrely mine is the only hit.

(Anonymous) 2005-10-17 04:37 pm (UTC)(link)
Hi,

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

[identity profile] tinyjo.livejournal.com 2005-10-17 04:44 pm (UTC)(link)
Hey, any time! I'm sorry my solution didn't help you out - good luck with it...

Too many items in List Box - caused by ToString() override returning nothing

(Anonymous) 2005-12-21 04:29 pm (UTC)(link)
I had this "Too many items in List Box" error when I was adding a set of classes to a checked list box items collection one at a time in a for loop.

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

(Anonymous) 2005-12-21 04:38 pm (UTC)(link)
I had this "Too many items in List Box" error when I was adding a set of classes to a checked list box items collection one at a time in a for loop.

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

[identity profile] tinyjo.livejournal.com 2005-12-22 05:27 pm (UTC)(link)
I'm glad it found it's way on there. Your solution/diagnosis sounds interesting. Were you only having the error when you had one item in the list box, or was it happening all the time?

[identity profile] programishka.livejournal.com 2007-01-04 07:04 pm (UTC)(link)
you might by surpiced, but I was looking for a soluton of such problem and found this post thru google...

[livejournal.com profile] tinyjo thanks for solution :-)

[identity profile] tinyjo.livejournal.com 2007-01-04 07:08 pm (UTC)(link)
You're welcome!

Re: Too many items in List Box - caused by ToString() override returning nothing

(Anonymous) 2007-05-02 01:26 pm (UTC)(link)
Thnx,

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

(Anonymous) 2008-08-16 10:03 am (UTC)(link)
I'm new here, just wanted to say hello and introduce myself.

Re: Hello

[identity profile] tinyjo.livejournal.com 2008-08-18 08:11 am (UTC)(link)
Err, hi! Pleased to meet you :)