How to get/set scrollbar positions in ListView?

F

Frank Rizzo

I reload the items in the listview on a regular schedule (i.e. clear the
listview, then load it up with fresh values).

I want add a convinience to the user whereby my application remembers
the position of the scrollbars after reloading the data. But I can’t
figure out how to retrieve the scrollbar position (both horizontal and
vertical). Neither can I figure out how to restore the state of the
scrollbars. I could theoretically figure out the first visible item
via the .TopItem property and run .EnsureVisible on it, however, I’d
still want to set the Horizontal scrollbar and I don’t know how to do this.

Any idea on how to do this?

Thanks,
Frank
 
K

Ken Tucker [MVP]

Hi,

Use the getscrollinfo and setscrollinfo api.

Declare Function GetScrollInfo Lib "user32" Alias "GetScrollInfo" _
(ByVal hWnd As IntPtr, ByVal n As Integer, ByRef lpScrollInfo
As SCROLLINFO) As Integer

Declare Function SetScrollInfo Lib "user32" Alias "SetScrollInfo" _
(ByVal hWnd As IntPtr, ByVal n As Integer, _
ByRef lpcScrollInfo As SCROLLINFO, ByVal bool As Boolean) As
Integer


Structure SCROLLINFO
Dim cbSize As Integer
Dim fMask As Integer
Dim nMin As Integer
Dim nMax As Integer
Dim nPage As Integer
Dim nPos As Integer
Dim nTrackPos As Integer
End Structure

Private Const SB_HORZ = 0
Private Const SB_VERT = 1

Private Const SIF_RANGE = &H1
Private Const SIF_PAGE = &H2
Private Const SIF_POS = &H4
Private Const SIF_ALL = (SIF_RANGE Or SIF_PAGE Or SIF_POS)

Dim si As SCROLLINFO
si.cbSize =
System.Runtime.InteropServices.Marshal.SizeOf(si)
si.fMask = SIF_ALL
Dim x As Integer = GetScrollInfo(listview1.handle, SB_VERT,
si)
si.nPos = si.nMax
x = SetScrollInfo(listview1.handle, SB_VERT, si, True)

Ken
 
F

Frank Rizzo

Thanks, Ken.
I've figured out how to get the scroll position.
And I can also set the position, however, the ListView is not aware
that I set the scroll position and doesn't move accordingly. What am I
missing here?

Is there a call to the comctl32.dll or something like that?

Thanks.
 
Y

yEaH rIgHt

Try this:

Option Explicit On
Option Strict On
Imports System.Runtime.InteropServices.Marshal

Public Class Form1
Inherits System.Windows.Forms.Form

Dim si As New SCROLLINFO()

Declare Function GetScrollInfo Lib "user32" Alias "GetScrollInfo" _
(ByVal hWnd As IntPtr, ByVal n As Integer, ByRef
lpScrollInfo As SCROLLINFO) As Integer

Declare Function SetScrollInfo Lib "user32" Alias "SetScrollInfo" _
(ByVal hWnd As IntPtr, ByVal n As Integer, _
ByRef lpcScrollInfo As SCROLLINFO, ByVal bool As Boolean) As Integer

Public Structure SCROLLINFO
Public cbSize As Integer
Public fMask As Integer
Public nMin As Integer
Public nMax As Integer
Public nPage As Integer
Public nPos As Integer
Public nTrackPos As Integer
End Structure

Private Const SB_HORZ As Integer = 0
Private Const SB_VERT As Integer = 1

Private Const SIF_RANGE As Integer = &H1
Private Const SIF_PAGE As Integer = &H2
Private Const SIF_POS As Integer = &H4
Private Const SIF_TRACKPOS As Integer = &H10
Private Const SIF_ALL As Integer = SIF_RANGE Or SIF_PAGE Or SIF_POS Or
SIF_TRACKPOS



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

si.cbSize = SizeOf(si)
si.fMask = SIF_ALL
GetScrollInfo(ListView1.Handle, SB_VERT, si)
si.nPos = si.nMax
SetScrollInfo(ListView1.Handle, SB_VERT, si, True)

End Sub

End Class
 
F

Frank Rizzo

This is exactly what I got and the the scrollbar does move to exactly
where I want it. The only hickup is that the ListView controls itself
doesn't reposition the columns and rows in response to the scrollbar move.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top