Listview Visible Row Count

  • Thread starter Thread starter Henry
  • Start date Start date
H

Henry

Does anyone know how I can determine how many visible rows there are in
a vb.net Listview? There is no listview "visible row count" property
like there is for a datagrid.

Tks in advance for any feedback.
 
I was aware of the listview "item count" propery to obtain the total
number of items.

However, what I need to figure out is how may rows in the listview
display area are advanced or vice versa when the user presses the "page
down" or the "page up " keys. I will take the number and then
increment/decrement my record position/counter that I display for the
user.

Any other ideas?
 
You could probably use the Control.ClientSize Property in conjunction with
the protected property Control.FontHeight and do some fun mathmatics there
(not really diffucult.

RowsVisible = Control.ClientSize.Height / Control.FontHeight

I would round it and you should have a good idea of how much space is being
used... I think you can get the height of the columns as well for good
measure...
 
Also noted.. You can use Control.CreateGraphics method and use embedded
methods in that such as MEasureString I suppose...

And add padding as neede.d

-CJ
 
Tks for the suggestion. I'll work the idea and see what I can come up
with.

In the mean time any other suggestions would also be welcomed.
 
For anyone who is interested below is a solution:

Dim lvCount
lvCount = ListView_GetVisibleCount(ListView1.Handle)

Private Const LVM_FIRST = &H1000
Private Const LVM_GETCOUNTPERPAGE As Long = (LVM_FIRST + 40)
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA"
(ByVal hwnd As IntPtr, ByVal wMsg As Long, ByVal wParam As Long, ByVal
lParam As Long) As Long

Private Function ListView_GetVisibleCount(ByVal hwndlv As IntPtr) As
Long
ListView_GetVisibleCount = SendMessage(hwndlv,LVM_GETCOUNTPERPAGE, 0&,
0&)
End Function
 
* Henry said:
For anyone who is interested below is a solution:

Dim lvCount
lvCount = ListView_GetVisibleCount(ListView1.Handle)

Private Const LVM_FIRST = &H1000
Private Const LVM_GETCOUNTPERPAGE As Long = (LVM_FIRST + 40)
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA"
(ByVal hwnd As IntPtr, ByVal wMsg As Long, ByVal wParam As Long, ByVal
lParam As Long) As Long

Private Function ListView_GetVisibleCount(ByVal hwndlv As IntPtr) As
Long
ListView_GetVisibleCount = SendMessage(hwndlv,LVM_GETCOUNTPERPAGE, 0&,
0&)
End Function

Replace all 'As Long' with 'As Int32', at least 'hwnd' should be an
'IntPtr'. Remove the '&' suffix from the '0&'.
 
Back
Top