Force a ListBox to Scroll

Problem

You want to scroll a list box programmatically so that certain items in the list are visible.

Solution

Set the ListBox.TopIndex property, which sets the first visible list item.

Discussion

In some cases, you might have a list box that stores a significant amount of information or one that you add information to periodically. It’s often the case that the most recent information, which is added at the end of the list, is more important than the information at the top of the list. One solution is to scroll the list box so that recently added items are visible.

The following code example is for a form with two buttons (shown in Figure 11-6). One button adds a batch of 10 items to the form, while the other button adds 10 ...

Get Microsoft® Visual Basic® .NET Programmer's Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.