Windows Forms listbox is overflowing when there ar

2019-07-01 19:47发布

问题:

I am building an indexer application based on a suffix tree, that enables me to index whole documents and search strings, substrings, etc, extremely fast.

What I'm doing is entering a string in a textbox, pressing a button, and running a function that queries the suffixtree and adds the positions of all occurences of the string to a listbox. So the listbox has a lot of integers inside it.

Normally, this works fine. For instance, searching for "wizard" or "Gandalf", or "hobbit" (I am indexing FOTR as a test :D) works fine and dandy. However, I'm trying it out now with the letter e.

The problem is that the positions of the letter e do get listed inside the listbox. All 88974 of them. However, when I'm scrolling down by dragging the scroller button, the listbox appears to circularly go back to the top when I'm at around item 60000 or so. The problem is made wierder by the fact that I've tried scrolling with page down and everything works ok.

I know that scrolling through 88974 (based on listbox.Items.Count) items is a bit extreme, but logically, there is no reason why there should be such a problem.

Thanks for reading this far!

EDIT : To all those who answered, thanks for answering. I've finally implemented this with a listview, and it's much faster, and can hold many items quite well. Thanks!

回答1:

Probably ListBox still uses the 16 bit range/position messages for scrollbars. You could try to reproduce it in Win32 if it's a problem of the underlying control.

You may want to try a list view control instead of the list box. I'd expect it to not run into this limitation, and its virtual mode should be more efficient, too.



回答2:

It is a Vista specific bug. It goes bonkers when you scroll past 65536 + number of visible items. The bug did not get fixed in SP1. No problems in XP. But yes, that's a bug that rarely gets put to the test.



回答3:

Sounds like you're hitting an oddity around the 16-bit (2^16=65536) point. My guess is that it's a bug in the scrolling logic for the control that causes a hiccup around that point. I suppose they figure nobody would put more than 65000 items in a listbox :)



回答4:

As others said, it seems likely that you are running into a limitation on the maximum number of items that can be stored in a ListBox. You're probably best off switching to using a DataGridView in virtual mode with that many items.

Implementing DataGridView is a little bit too long for an SO answer but here's a link to a tutorial:

http://msdn.microsoft.com/en-us/library/2b177d6d.aspx