Question Listview Scroll Position (or TopItem)

Elwood472

New member
Joined
Oct 6, 2014
Messages
2
Programming Experience
5-10
I've been scouring the internet for the past couple of days trying to find a solution to this problem, but nothing I've tried has worked.

In a nutshell, after the listview refreshes its items, I want the scrolled position to remain where it was, rather than going all the way back to the top. I can get this to work if I do not ShowGroups, but that is a feature I want to use.

My program is basically a file explorer with tons of extra features. It features a TreeView which displays the folders and a ListView which displays files and subfolders. After the user renames a file or performs some action that refreshes the tree, I try to retain the same view, so they do not have to scroll back to where they were.

First I tried getting and setting the scrollbar position and this DOES set the scrollbar position to where it was, but it does not actually scroll the list to where it should be. I see the same thing as if I didn't set the scrollbar. I read somewhere that suggested I use the Invalidate method to redraw the form, but this doesn't do anything.

Next I tried getting and setting the TopIndex. When I try getting the TopIndex, the value is 0 which is wrong, so setting it doesn't set it to the right place. I tried manually setting the topindex to an arbitraty number, and that works.

I found some code online that gets the real TopIndex and that works, however when I set it, it doesn't show the correct area. I believe this is because I am using groups. I group my files and folders separately and my files have subgroups as well (Word, Excel, etc.) My TopIndex gets the index that's correct if they were not grouped. However, when I set the topindex, it just counts down the line.

To further explain that, the list below represents my files in native form with it's index

folder 1 (0)
folder 2 (1)
a.doc (2)
b.xls (3)
c.doc (4)
d.pdf (5)
e.exe (6)

When I group, it would appear like this:

FOLDER GROUP
folder 1 (0)
folder 2 (1)

WORD GROUP
a.doc (2)
c.doc (4)

EXCEL GROUP
b.xls (3)

FILES GROUP
d.pdf (5)
e.exe (6)

When I grab the top item for b.xls, the index is 3, but when I set it, it makes c.doc (the third item in the grouped list).

I tried setting the TopItem to a ListViewItem but this fails too. I tried using EnsureVisible and FindItemByText, but they all fail.

Here is my code to get the "Real" top item
VB.NET:
    Public Function RealtopItem() As ListViewItem

        Dim i As Integer = 0

        While (i < lv1.Items.Count AndAlso Not lv1.ClientRectangle.Contains(lv1.Items(i).Bounds))
            i = i + 1
        End While

        Return lv1.Items(i)

    End Function

Here is the code to call the refresh:

VB.NET:
        mMyItem = RealtopItem()


        ExpTree1.RefreshTree()   'This in turn updates the list view


        lv1.TopItem = mMyItem


Again, TopItem works great if I don't use groups.


Thanks for your time and assistance!
 
I did not solve the problem with the top item, but figured out a way to not have to use it.

When a user renamed a file, the code would rebuild the list view. I no longer rebuild it, but instead change the text of the item to match the new file name. This eliminates the need to retain the scrolled position or top item.

I discovered this as I worked on a solution to reselect selected items after updating.
 
Back
Top