treetey
treetey

Reputation: 839

How can I get the height of a ListView item?

I have ListView and I need to determine item height.

Upvotes: 4

Views: 14856

Answers (3)

Gwynge
Gwynge

Reputation: 160

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

Upvotes: 1

Renatas M.
Renatas M.

Reputation: 11820

I am not 100% sure but this might help:

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

Upvotes: 2

Frédéric Hamidi
Frédéric Hamidi

Reputation: 263047

You can use the GetItemRect() method:

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

Upvotes: 13

Related Questions