By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,282 Members | 1,562 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,282 IT Pros & Developers. It's quick & easy.

How to get/set scrollbar positions in ListView?

P: n/a
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
Nov 20 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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
------------------

"Frank Rizzo" <no**@none.com> wrote in message
news:eE**************@TK2MSFTNGP10.phx.gbl...
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

Nov 20 '05 #2

P: n/a
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.
Ken Tucker [MVP] wrote:
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
------------------

"Frank Rizzo" <no**@none.com> wrote in message
news:eE**************@TK2MSFTNGP10.phx.gbl...
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


Nov 20 '05 #3

P: n/a
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

"Frank Rizzo" <no**@none.com> wrote in message
news:eB**************@TK2MSFTNGP12.phx.gbl...
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.
Ken Tucker [MVP] wrote:
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
------------------

"Frank Rizzo" <no**@none.com> wrote in message
news:eE**************@TK2MSFTNGP10.phx.gbl...
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 theposition 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 theHorizontal scrollbar and I don't know how to do this.

Any idea on how to do this?

Thanks,
Frank


Nov 20 '05 #4

P: n/a
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.

yEaH rIgHt wrote:
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

"Frank Rizzo" <no**@none.com> wrote in message
news:eB**************@TK2MSFTNGP12.phx.gbl...
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.
Ken Tucker [MVP] wrote:
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(s i)
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
------------------

"Frank Rizzo" <no**@none.com> wrote in message
news:eE**************@TK2MSFTNGP10.phx.gbl...
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


Nov 20 '05 #5

P: n/a
Hi,

Try invalidating the listview to get it to repaint.

Ken
------------
"Frank Rizzo" <no**@none.com> wrote in message
news:uL**************@TK2MSFTNGP12.phx.gbl...
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.

yEaH rIgHt wrote:
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

"Frank Rizzo" <no**@none.com> wrote in message
news:eB**************@TK2MSFTNGP12.phx.gbl...
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.
Ken Tucker [MVP] wrote:

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
------------------

"Frank Rizzo" <no**@none.com> wrote in message
news:eE**************@TK2MSFTNGP10.phx.gbl.. .
>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


Nov 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.