Vb.net - scroll to bottom of listbox

keith1001
keith1001 used Ask the Experts™
on
Once a list box items list is bigger than display, it gets pushed to bottom and last item added is not visible.  You have to scroll the listbox manually.

How do I make the listbox to scroll to bottom of list with code?

And is there a way when adding items to the listbox, it pushes the previous item down and put current item at top?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
To add to the top of the list, use the insert method. For example,

ListBox1.Items.Insert(0, "My Item")

That will insert the item at the top.
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
Add to and scroll to the bottom:
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Static cnt As Integer = 0
        cnt = cnt + 1
        ListBox1.Items.Add("Item #" & cnt)
        ListBox1.SelectedIndex = ListBox1.Items.Count - 1
    End Sub

Open in new window


Add to and scroll to the top:
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Static top As Integer = 0
        top = top + 1
        ListBox1.Items.Insert(0, "Top #" & top)
        ListBox1.SelectedIndex = 0
    End Sub

Open in new window

For the second half, it's a bit more complex. You can either select an element further down on the list and it should scroll or you can simulate a scroll. I haven't done that, but there's a blog post about how to do it in C# at http://umaranis.wordpress.com/2013/10/13/programmatically-scroll-listbox-in-net-windows-forms-c/ You need to use the SendMessage API call (http://msdn.microsoft.com/en-us/library/windows/desktop/ms644950(v=vs.85).aspx)

Author

Commented:
Thanks guys for quick response and answers!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial