Solved

Vb.net - scroll to bottom of listbox

Posted on 2014-02-25
4
1,209 Views
Last Modified: 2014-02-25
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?
0
Comment
Question by:keith1001
  • 2
4 Comments
 
LVL 14

Accepted Solution

by:
quizwedge earned 250 total points
ID: 39887055
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.
0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 250 total points
ID: 39887069
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

0
 
LVL 14

Expert Comment

by:quizwedge
ID: 39887072
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)
0
 

Author Closing Comment

by:keith1001
ID: 39887166
Thanks guys for quick response and answers!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video discusses moving either the default database or any database to a new volume.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now