2
votes

Nutshell: How can I disable the auto-scroll back to the top of a ListBox when I call RemoveAt(0)?

Background:
I have a simple list box that I mash full of data at regular intervals.

Basically I'm using it is a status reporting tool.

Every iteration of my program causes 100 or so lines of data to be thrown into the listbox and the best way to dump piles of information in is by always adding it to the bottom.

I want newest results to be at the bottom, and the oldest results to be "pushed up". Because the size of this listbox becomes rather large after a short period of time I limit the size to 1000 items.

When the box gets more than 1000 items, I use RemoveAt(0) till I get 1000 items again.

To always display the most relevant information I scroll the listbox all the way down. The problem with this is that whenever I call RemoveAt(0), the box is auto-scrolled back up to the new item 0. Basically, every time I add items to a "full" list box, the effect is a quick scroll up 1000 items, then back down 1000 items, making for a clumsy looking display.

How can I disable the auto-scroll back to the top of a ListBox when I call RemoveAt(0)?

2
depends on your platform---winforms,silverlight,wpf,asp.net (etc)Muad'Dib
Microsoft Visual C# 2008 Express (winforms)Gorchestopher H
Not sure this will work, but try calling ListBox.BeginUpdate before you call RemoveAt(0). Then set the selected item to the last one in the list, and finally call EndUpdate. This should prevent the ListBox from updating until EndUpdate is called.Jim Mischel
@Jim That works and it works great! No flicker, nice and clean, this is what I'm implementing!Gorchestopher H

2 Answers

1
votes

This will only happen when there is no selected item. Or in other words, ListBox.SelectedIndex = -1. The simple workaround is to select an item. Like the last one.

0
votes

I can't answer your question. Even if I knew the technology, I might not have an answer that would fulfill your needs. Part of the issue is you are asking "help me solve this in the way I am trying to solve it" rather than "here is the business problem, what is the best solution".

My advice: Go back to the core problem, which sounds like "I need a way of updating a 'list' with new events while keeping the list to 1000 items or less". From this, you have a solution of a ListBox appended to bottom and cleared from top as your solution. And, I am not sure how to turn off scrolling in your solution, but I am not convinced it is the only way to solve the business problem.

Broken down, there are a few questions to answer:

  1. How do I bind/update the data?
  2. How do I keep the control from scrolling to a new position when I change the number of items bound?

As far as binding, the programatic addition is one option, whether you add from bottom and prune top or add to top and prune bottom. Another is to assemble the data and rebind. I would imagine there are others.

The scrolling problem is not really a scrolling problem as much as a "remember what I am looking at" problem or a "always display newest" problem. If always display newest, you need to figure out a way to make sure you are at the end when something is added or removed. If user can change position, you need to have a means of storing where he is and getting back to that position. You have added some complexity, as your list can be 1 to 1000 items long, so scroll position, except always at bottom, is dicey, unless you work in percentages. Regardless of whether turning off scrolling solves the immediate problem, if there is a business requirement to remember the user's position, you should have a check step after data is updated.

As a last suggestion, look at how other people have solved the problem of displaying streaming data. Following a well-known pattern is a good idea, as it gives the user a familiar interface. Often, with streaming data, the stream has a very small set of lines (enough to fill a screen, not 1000), but allows you to go back and look at previous "logged" events, if needed. Once again, I am not sure your solution requires a streaming type solution, but you still should go back to the core business problem and start from there.