tags:

views:

11

answers:

1

I have a ListBox that displays some words. Words are entered in TextBox, and when submitted on button click, they are added to ListBox. The problem is, if I add many words, scroll is always on top of ListBox, so I don't see last but first words added. Is there a way to dynamically move scroll to the end of ListBox every time word is added, so last added word is visible?

A: 

here you go, this should do nicely...

public static void ScrollToBottom(this ListBox listbox)
{
    if (listbox == null) throw new ArgumentNullException("listbox", "Argument listbox cannot be null");
    if (!listbox.IsInitialized) throw new InvalidOperationException("ListBox is in an invalid state: IsInitialized == false");

    if (listbox.Items.Count == 0)
        return;

    listbox.ScrollIntoView(listbox.Items[listbox.Items.Count - 1]);
}

Now, given any ListBox I can do this: ListBox lb = ...;

lb.ScrollToBottom();
Muad'Dib
It works perfectly! Thanks!
sokolovic
my pleasure, glad to help!
Muad'Dib