Reputation: 2277
I have noticed an issue with having a large number of items in a listbox winforms control.
In this simple test case example, I have 120,000 strings added to a listbox. I originally encountered this using a datatable bound to the listbox.DataSource, but it is reproducible this way as well:
listBox.BeginUpdate();
for(int x = 0; x < 120000; x++)
{
listBox.Items.Add(x);
}
listBox.EndUpdate();
When I click and drag the scroll button from the top to the bottom, after I release the mouse, it moves the scroll button to the middle of the scroll bar.
This is a screenshot of where the button lands when I let it go from exactly at / near the bottom (I was precise and did not have the mouse outside of bounds of the scroll bar).
If I now click on the scroll bar button and move it 3/4/ the way down the length of the control, it will now pop back up to around 1/4th the way down the control length.
Has anyone else seen this and is there a known workaround? (I admittedly haven't looked into this next point) does this control have 'virtual modde' dynamic loading options like datagridview which might incidentally get rid of this strange hiccup?
Upvotes: 2
Views: 2332
Reputation: 1
I noticed that the list box build in vertical scroll bar only can return the index from scroll position up to maximum 65535. If the items added more than this value then will have the scrolling issue. However, Vertical scroll bar control does not have this issue. It can scroll up to maximum of Int32.Max. I just used the vertical scroll bar control instead of the default build in ListBox scroll bar
I add in another vertical scroll bar (vScrollBar1) and place it on right side of the list box to cover its original's list box vertical scroll bar.
Then Add below code for the vertical scroll bar that I create
private void vScrollBar1_Scroll(object sender, ScrollEventArgs e) { listbox.TopIndex =
e.NewValue; }
private void vScrollBar1_MouseEnter(object sender, EventArgs e)
{
vScrollBar1.Maximum = listbox.Items.Count;
}
Upvotes: 0
Reputation: 1636
IMHO, you are solving the wrong problem. What is the use case for having such a large number of items in your listbox. The general usage for a list box is for the user to select from the listed population. Does your application really expect users to select specific items from such a large list of choices?
You would do your users (and your performance) a favor by providing a better way to narrow down the list for selection.
Upvotes: 0
Reputation: 18863
For example if you want to do this when you first load the form setup the property and its virtual size use a ListView instead of a ListBox
private void Form1_Load(object sender, EventArgs e)
{
listView1.VirtualMode = true;
listView1.VirtualListSize = 12000;
}
Upvotes: 1