.NET ListView String Space

There seems to be no way to change the padding (or row height) for all rows in a .NET ListView. Does anyone have an elegant hacker?

+2
source share
2 answers

I know this post is quite old, however, if you have never found a better option, I have a blog post that can help, it involves using LVM_SETICONSPACING,

According to my blog,

First you need to add:

using System.Runtime.InteropServices; 

You will then need to import the DLL so that you can use SendMessage to change the ListView settings.

 [DllImport("user32.dll")] public static extern int SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam); 

Once this is complete, create the following two functions:

 public int MakeLong(short lowPart, short highPart) { return (int)(((ushort)lowPart) | (uint)(highPart << 16)); } public void ListViewItem_SetSpacing(ListView listview, short leftPadding, short topPadding) { const int LVM_FIRST = 0x1000; const int LVM_SETICONSPACING = LVM_FIRST + 53; SendMessage(listview.Handle, LVM_SETICONSPACING, IntPtr.Zero, (IntPtr)MakeLong(leftPadding, topPadding)); } 

Then, to use the function, just go to your ListView and set the values. In the example, 64 pixels is the image width, and 32 pixels is my horizontal spacing / padding, 100 pixels is the image height, and 16 pixels is my vertical spacing / padding, and a minimum of 4 pixels is required for both parameters.

 ListViewItem_SetSpacing(this.listView1, 64 + 32, 100 + 16); 
+8
source

The workaround is to use an ImageList that is as tall as you want the elements to be. Just fill the blank image with the background color. You can even make image 1 wide so as not to take up much horizontal space.

+3
source

All Articles