Get visible portion of ListView?

S

Sin Jeong-hun

Is it possible to get current visible rect when there is a scrollbar on
the ListBox?

Say, if the ListBox is in Details mode, and there are 1000 lines. The
user has scrolled down to 500th line and currently 500~540th lines are
visible. Is there any way to get this number (500~540?) or virtual
Rectable of visible area compared to the whole ListView rectable?
 
D

Dave Sexton

Hi,

In 2.0 (I'm not sure about 1.*) there is a method called GetItemRect. I haven't tried myself, but maybe you can use the TopItem
property, GetItemRect and the Height property of the ListView to determine the number of lines that are visible. Here's some code
(hasn't been tested):

int firstVisibleIndex = listView.TopItem.Index;

// TODO: verify heightOfFirstItem is not zero
int heightOfFirstItem = listView.GetItemRect(firstVisibleIndex, ItemBoundsPortion.Entire).Height;

// assuming each item has the same height (I think this is true for list view and details view)
int nVisibleLines = (int) Math.Ceiling(listView.Height / heightOfFirstItem);

int lastVisibleIndexInDetailsMode = firstVisibleIndex + nVisibleLines;

// NOTE: This will not work in list mode without accounting for multiple items per line.

Please let me know if that works for you.
 
C

Claes Bergefall

P/Invoke LVM_GETCOUNTPERPAGE and use the TopItem property. From those two
you can figure out which items are currently visible (at least I think this
is what you asked for).

/claes
 

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