How to auto scroll down in WinForms ListView control when update new item?
Asked Answered
D

6

38

How to auto scroll down in ListView control when update new item?

I have tried

listView1.Focus();
listView1.Items[listView1.Items.Count - 1].Selected = true;

but this not working.

Debauchery answered 6/1, 2010 at 16:9 Comment(1)
WinForms? WPF? Please specify...Roughneck
B
94

Try

listView1.Items[listView1.Items.Count - 1].EnsureVisible();
Balkhash answered 6/1, 2010 at 16:13 Comment(7)
Might I add that this is a winforms only solution, EnsureVisible doesn't exist on the WPF ListView. Only saying because I hit this page from a google search for "WPF ListView auto scroll to bottom".Oblation
Sounds fair enough. Do you have a link to a solution for WPF to help anybody else reaching this answer for the same reason?Balkhash
The problem seems to arise from not being able to access the built-in ScrollViewer on the WPF ListView through a property, I am just testing a solution now and will write it up on my blog, link to follow.Oblation
Finally got around to writing that blog post, here's a solution for WPF for info. dutton.me.uk/2013/08/13/…Oblation
@Oblation ironically the more times W P F is mentioned in the comments, the more likely Google is to list this question incorrectly. The search engine isn't terribly context-aware.Contextual
Very true @MarkRansomOblation
For some reason, EnsureVisible did not work for me because ListView control was in an other tab. So i used this instead: listview1.TopItem = listView1.Items[listView1.Items.Count - 1]Siegbahn
S
2

Another possible solution:

listview1.TopItem = listView1.Items[listView1.Items.Count - 1];

Siegbahn answered 22/7, 2021 at 10:9 Comment(0)
J
1

old fashioned solution:

 ListView1.SetFocus;
 PostMessage(ListView1.Handle, WM_KEYDOWN, VK_DOWN, 0);
Joeannjoed answered 13/8, 2020 at 10:55 Comment(0)
K
0

where x is an int, of the item in the list you want to see

listView1.Items[x].Focus();
listView1.Items[x].Selected = true;
listView1.EnsureVisible(x);
Kennykeno answered 11/3, 2017 at 20:35 Comment(0)
M
-2

maybe

listView1.Items[listView1.Items.Count - 1].Selected = false;
listView1.Items[listView1.Items.Count - 1].Selected = true;
Maxon answered 6/1, 2010 at 16:13 Comment(0)
S
-2

Codesleuth's answer of calling EnsureVisible() on the last item in the list only worked for me when called during the forms OnShown() event.

I tried doing it in the constructor, where I was populating my ListView, but nothing happened. Doing it during OnShown() worked a treat, however.

Hope this helps.

Sideman answered 9/9, 2014 at 9:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.