Emptied of expectation. Relax. (tinyjo) wrote,
Emptied of expectation. Relax.
tinyjo

  • Mood:

"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.
Subscribe
  • Post a new comment

    Error

    Comments allowed for friends only

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

  • 14 comments