CraigF
CraigF

Reputation: 35

How do you make a Windows.Forms ListView item scroll into view?

I use a ListView to show a list of items. When a new item is added, or the user moves one up or down (via buttons), I want the list to scroll to show/keep the item visible to the user. How can I do this?

Most articles I've found point to the ScrollIntoView() method or give complex code solutions. E.g. this question and answer uses ScrollIntoView() in WPF, but that isn't available for a ListView in Windows.Forms.

Upvotes: 0

Views: 89

Answers (1)

CraigF
CraigF

Reputation: 35

Use ListView.EnsureVisible(index), which takes the index of the item you want to be visible. This will scroll the given item into view.

Another option is to use ListView.TopItem = itemToShow, but this will put it at the top of the list every time, whereas EnsureVisible() will scroll an item that is below the view up to the bottom position, which gives a clearer visual effect in your situation.

Upvotes: 0

Related Questions