How to determine if listbox item is visible?

R

Robin Tucker

Hi there,

How can I determine if a list box item is visible or not (I mean visible
within the client context of the listbox).

Background -->

I am trying to implement an "image cache". My list box items are all owner
draw and display various images. Of course, when I have 1,000 items in the
list box, I don't want to go to the database and fetch from 1 to 1000 (too
slow), just to display items 300 to 320 in the list box. So what I'm doing
is requesting the image from the database thread in the OnDrawItem event and
then, when the thread executes a delegate to state that the item has been
fetched from the database, I'm "refreshing" the item on screen.

When the user scrolls the list box, I need to cancel requests for items that
are no longer visible (the database fetch thread no longer needs to fetch
them into the cache). I can do this by testing each request in the request
queue to see if its still visible in the listbox client area. Something
tells me GetItemRectangle is the key here, but I'm not sure.
 
R

Robin Tucker

Ok, I solved this using a different technique:

Public Function CanFetch(ByVal theKey As Integer) As Boolean

' Work out the bottom point to check

Dim bottom As Integer = (ListBox.Height \ ListBox.ItemHeight) *
ListBox.ItemHeight

' Now find the first and last indices of items in the list box

Dim theFirst As Integer = ListBox.IndexFromPoint(New Point(0, 0))
Dim theLast As Integer = ListBox.IndexFromPoint(New
Point(ListBox.Width - 1, bottom - 1))

' Minus one means there is nothing there, or rather, that we have
reached the end. So set the index
' to the end

If theLast = -1 Then
theLast = ListBox.Items.Count - 1
End If

' Is it in our list?

If Hash.ContainsKey(theKey) Then

Dim theItem As Integer = ListBox.Items.IndexOf(Hash.Items(theKey))

' Ok, if its within the bounds, its visible, otherwise, do not fetch
it.

If theItem >= theFirst And theItem <= theLast Then
Return True
End If
End If

Return False

End Function
 

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