473,396 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to get/set scrollbar positions in ListView?

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
5 22417
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Kovan A. | last post by:
since i couldnt find anyway to scroll a listview having disabled the scrollbars, i am left with no choice but to make the current scrollers bigger any leads as to which beast i am up against for...
0
by: user | last post by:
Hello Where can i find which messages can i sent to ListView class ? I would like to send to it something like EM_SETSCROLLPOS to RichTextBox (i want to see always 'fresh data' in ListView so from...
0
by: Tim Bücker | last post by:
Hello. Is it really not possible to hide the scrollbars of a listview ( this.listview.Scrollable = false; ) but do some scrolling anyway ( for instance: PostMessage(this.listview.Handle,...
2
by: | last post by:
Hi, Trying to figure out how i can tell if the scrollbar is in the bottom of my listview, im using the listview to display log files and i track them and add new items to the bottom of the...
0
by: Jeff Stewart | last post by:
After I update a listview in my form, I want to automatically set its scrollbar to the same position it was at before the update began. How do you do that programmatically? -- Jeff S.
2
by: | last post by:
Hi All, How can I get to scroll bars of a listview, I am trying to sync two listviews so I need to overload one of the listview, How can I achieve this ? Thank You.
1
by: malpani.abhijit | last post by:
Hi, I am having a ListView component in my WinForm, when i add some images into it, it gives me a vertical scroll bar. What i want is, the horizontal scrollbar instead of vertical. Is there any...
0
by: Dave Kelly | last post by:
Using .NET 2 . I am trying to get a vertical scrollbar with the ListView control, when in the List view mode. The default behaviour when the number of items exceeds the vertical length is to...
1
by: Lubomir | last post by:
Hi, I have a ListView with one column and no header. How to display the horizontal scrollbar if one of the items is too long? Scrolable property is set to true. Thanks, Lubomir
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.