How can I get the height of a ListView item?

13,020

Solution 1

You can use the GetItemRect() method:

int itemHeight = yourListView.GetItemRect(itemIndex).Height;

Solution 2

I am not 100% sure but this might help:

int itemHeight = listView.Items[itemIndex].GetBounds(ItemBoundsPortion.Entire).Height;

Solution 3

I had the same question however there is one issue - until the listview is drawn the values aren't set. And you may want to be able to set the sizes before you add any items (if for example I want to dry a listview that can display 5 entries but will start off empty).

Therefore my workaround was to run the following code, which forces the control to be rendered but without displaying it, in the application's initialisation section and save the values as global variables for later use. And, to get around listviews with different font sizes, to only store the difference between the height and the font height:

Dim lvwTemp As New ListView
lvwTemp.View = View.Details
lvwTemp.Columns.Add("test")
lvwTemp.Items.Add("test")
Dim zTempBitmap As New Bitmap(100, 100)
lvwTemp.DrawToBitmap(zTempBitmap, New Rectangle(0, 0, 100, 100))
zTempBitmap.Dispose()
gintListviewHeaderHeightMinusFontSize = lvwTemp.Items(0).GetBounds(ItemBoundsPortion.Entire).Top - lvwTemp.Font.Height
gintListviewItemHeightMinusFontSize = lvwTemp.Items(0).GetBounds(ItemBoundsPortion.Entire).Height - lvwTemp.Font.Height
Share:
13,020
treetey
Author by

treetey

Developer of SpeedRunner file manger. http://speedrunner.belintesa.com/

Updated on July 12, 2022

Comments

  • treetey
    treetey almost 2 years

    I have ListView and I need to determine item height.

  • Rick Morgan
    Rick Morgan over 7 years
    This allows you to get column header height and item height very easily - nice!