Scrolling ListBox contents?

  • Thread starter Thread starter Ray Mitchell
  • Start date Start date
R

Ray Mitchell

Hello,

I have a simple application in which I am using a ListBox to display text
strings for logging purposes. When the text area becomes full the new lines
are added to the end and the vertical scroll bar becomes visible, however,
it does not automatically scroll up to show the most recent lines. What do
I need to do to get it to scroll all the way up each time a new line is
added, even though I might have previously manually pulled the scroll bar to
another position?

Thanks,
Ray Mitchell
 
I came accross this problem my self. I found that listBox.TopIndex sets
the top line in the listBox. I added a global:
int nbrListBoxItems;

and in the menu initilization added:
// TODO: Add any constructor code after InitializeComponent
//
nbrListBoxItems = testListBox.ClientSize.Height /
testListBox.ItemHeight;
this determines the number of lines displayed.

Then when I added an entry I added the code:
if (testListBox.Items.Count > nbrListBoxItems)
testListBox.TopIndex = testListBox.Items.Count - nbrListBoxItems +1;

This works. I have been looking for a way to use a listBox event along
with this code so I don't have to added it after every add. I tried
using SizeChanged, but the code there never changes the listBox. I know
it must be a context thing, but I haven't figured it out yet. Anyone
have an answer?
Bill
 
Back
Top