Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Change ListView Item's/Row's height

Tags:

I want to change the Item's/Row's height in listview.

I searched every where and I figured that in order to change the height I need to use LBS_OWNERDRAWFIXED or MeasureItem or something like that.

The problem is that I dont know exactly what to do and how to use it..
Can anyone help me with it?

Edit:
I cant use the ImageList hack because I am using the SmallImageList for real and I need different line height from the ImageList images size.

Thanks!

like image 682
Ron Avatar asked Jul 03 '11 15:07

Ron


2 Answers

For the people that are still struggling with this, here is the code I use:

private void SetHeight(ListView listView, int height) {     ImageList imgList = new ImageList();     imgList.ImageSize = new Size(1, height);     listView.SmallImageList = imgList; } 

To use this, just do:

SetHeight(lvConnections, 25); 
like image 154
Maarten Peels Avatar answered Sep 27 '22 19:09

Maarten Peels


It can be done using the SmallImageList trick -- you just have to be careful. ObjectListView -- an open source wrapper around a standard .NET ListView -- uses that trick to successfully implement a RowHeight property.

If you want 32 pixels for each row, allocate an ImageList that is 16x32 (width x height), and then position each of your images in the vertical middle of the 32-pixel height.

This screen shot shows 32-pixel rows and the word wrapping that is possible because of the extra space:

enter image description here

ObjectListView does all this work for you. In fact, if you are trying to do anything with a ListView, you should seriously looked at using an ObjectListView instead. It makes many difficult things (e.g. sorting by column type, custom tooltips) trivial, and several impossible things (e.g. overlays, groups on virtual lists) possible.

like image 20
Grammarian Avatar answered Sep 27 '22 19:09

Grammarian