How to scroll listview items programmatically
Asked Answered
S

4

9

I have a listview control on my WinForms application.

here, on click of separate button, i do change couple of listview items backcolor and reload the whole grid as there are certain changes into database so, reloading from database on each click of button.

Now, problem is, once the grid is reloaded then lastly added items are scrolled so, need to scroll all items and find so, it makes hard to end user.

Is there any way to ,scroll the lastly added items or updated items into listview automatically (I mean, programmatically so, it could be view to user directly without being manually scrolled).

Scudder answered 4/8, 2014 at 9:56 Comment(3)
Could you not simply order the items so that the last added items are at the top of the listbox? That way there would be no need to scroll to the bottom to see them. That would seem the simplest way forward.Kipkipling
If the added item is always last in the ListView, you can use this answer: #2014787Hydrochloride
If it's not last(e.g. updated item), then you can store the item's index in your button click and scroll to it, by doing listView1.Items[itemIndex].EnsureVisible();Hydrochloride
T
10

listView1.EnsureVisible(X); where X is the item index.

This snippet can be used to scroll the ListView automatically to a particular index in the listView.

Consider the code: with this you can automatically scroll to the index 8 on button click

 private void button2_Click(object sender, EventArgs e)
 {
     listView1.EnsureVisible(8);
 }
Thoreau answered 4/8, 2014 at 10:41 Comment(3)
It is nice and simple but does not seem to work reliably.Benbena
@NeilDunlop, you probably tried it on the form creation and it is definitely not working. But if you move this implementation to the load event or to another part of code which is not part of the initialization, it should work fineWieldy
@Wieldy Thanks. I'll check what I was doing last year but, if I understand your suggestion correctly, I don't think that it would solve the problem I had. I'm not creating forms on the fly. All my forms are created using the VSTO Forms designer and then used...Benbena
W
1

Despite @user3711357 correct answer, I spent too much time trying to understand why it is not working for me. I found that trying to call EnsureVisible in the constructor of the form will not work.

public class MyForm
{
    public MyForm()
    {
        InitializeComponent();
        listView1.EnsureVisible(8);  // will not work !!!
    }

    private void MyForm_Load(object sender, EventArgs e)
    {
        listView1.EnsureVisible(8);  // Works fine
    }
}
Wieldy answered 12/7, 2018 at 7:31 Comment(0)
A
1

Can send messages directly.

public partial class Form1 : Form
{
    [DllImport("User32.dll", CharSet = CharSet.Auto)]
    public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);

    public Form1()
    {
        InitializeComponent();

        c_scroll.ScrollSlide += C_scroll_ScrollSlide;

    }

    private void C_vScrollBar_Scroll(object sender, ScrollEventArgs e)
    {
        const int LVM_SCROLL = (0x1000 + 20);
        SendMessage(c_listView_show.Handle, LVM_SCROLL, 0, e.NewValue - e.OldValue);

    }


}
Affirmation answered 19/2, 2019 at 1:55 Comment(0)
G
0

Before you refresh the list, store the currently focussed or selected item (depending on how your interaction code works) into a variable, then you can restore the selected item afterwards. For example;

Dim selectedObjectName = listview.SelectedItems(0).Name
...
' refresh your list
...
Dim vItem as ListViewItem
If listview.SelectedItem.ContainsKey(selectedObjectName) Then 
    vItem = listview.Items(selectedObjectName)
Else
    vItem = listview.Items(0)
End If
vItem.Selected = True
vItem.Focus
Gamekeeper answered 4/8, 2014 at 10:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.