3

I am adding items to a c# list box and want to always scroll it to the last item added to the list so it is visible. The list will usually exceed the space available so the vertical scroll bar will display and as users can move this I need to force it jump to the end again with a new item. The only useful way I have found is to use the TopIndex property with the rows in box - number of row that can be displayed. I have this working OK with the code below unless one of the lines was too long in which case the horizontal scroll bar uses up the room for about the last 2 items. If I could figure out if the horizontal bar was displayed I could change the number of rows in the computation to account for it.

        LB1.Items.Add(strText);
        LB1.TopIndex = Math.Max(0,lbXmlMsg.Items.Count - 10); // 10 rows visible

This seems like a lot of work to just make sure that a new item is visible. Am I missing something ovvious here ?

John James
  • 51
  • 3
  • Your not missing anything, its just the problem with adding the scroll bar when needed, I'm sure I've seen plenty of topics on this, just not sure of any right now – Sayse Jul 28 '13 at 21:22

3 Answers3

1

You can check if the horizontal scrollbar is visible by calling

LB1.HorizontalScrollbar

As per the OP's comment, the above does not work.

I would also say that hard coded numbers are bad practice, you can do something like this: How to scroll to bottom of ListBox?

And then there is this as a way to autoscroll http://www.csharp-examples.net/autoscroll/

I haven't tried any of these but they should work.

If you swap to a ListView this has an EnsureVisible function that does exactly the job you want.

Community
  • 1
  • 1
Jezza32
  • 11
  • 3
  • The HorizintalScrollbar attribute is always true to allow it to be created if necessary so dioesnt help. The idea of the ListView is a good one but it doesnt seem like just a replace of the type is enough. I will try it out later. Great help guys - 2 useful replies in an hour. Most impessed – John James Jul 28 '13 at 22:30
1

I'm not sure how to easily detect whether the horizontal scrollbar is showing but here's an extension method which will let you scroll to the last item and not raise the SelectedIndexChanged event.

public static void ScrollToLastItem(this ListBox lb, EventHandler eventHandler)
{
    lb.SelectedIndexChanged -= eventHandler;
    lb.SelectedIndex = lb.Items.Count - 1;
    lb.SelectedIndex = -1;
    lb.SelectedIndexChanged += eventHandler;
}

You can use it like this (passing in your SelectedIndexChanged event handler).

myListBox.ScrollToLastItem(myListBox_SelectedIndexChanged);

It can be easily modified to scroll to any item index if you don't want it to go just to the final item.

keyboardP
  • 68,824
  • 13
  • 156
  • 205
0

how about after adding the item, you do the following

listBox1.SelectedIndex = listBox1.Items.Count - 1;

it'll ensure the last item is visible.

user734028
  • 1,021
  • 1
  • 9
  • 21
  • 1
    That was a good suggestion. However I dont want it selected but adding LB1.SelectedIndex = -1 afterwards solves that issue – John James Jul 28 '13 at 22:13