It might seem like a silly & simple question, and yet, I've been unable to find a satisfying answer. Basically, I have a TListview (style = vsReport) with data. Sometimes, I have to update it, and therefore, I have to clear the listview and fill it again with the updated data.
However, when I do that, the scrollbar position is reseted to 0. I would like to be able to get the scrollbar position before the clearing and set it back to what it was before. If the updated data has the exact same amount of rows as the old data, I need the scrollbar to be at the exact same position as before; if not, I just need it to be more-or-less at the same place as before.
Seems easy, right? Yet, all I've found are hacks or tweaks with TopItem and MakeVisible. Is there any appropriate method to do that?
Thanks!
Save the top item before clearing,
After updating, scroll the listview so that the saved top item's 'y' position will be 0 (+ header height):
Actually, since you're re-populating the listview, you have to devise a mechanism to find which item you want to be at the top instead of saving a reference to it.
If you don't like modifying scroll position through 'top item', since functions like
SetScrollInfo
,SetScrollPos
won't update the client area of the control, you can useGetScrollInfo
to get the 'nPos' of aTScrollInfo
before clearing the list, and then send that manyWM_VSCROLL
messages with 'SB_LINEDOWN` after populating.Save scroll position:
After populating, scroll (assuming scroll position is 0):
or,