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:06 pm (UTC)From: