Robin
Robin

Reputation: 1967

Remove images from Listview Columns

For the columns in a ListView I am using my own Images to show if the sorting is ascending or descending. When I set the column.ImageKey property the column's text is moved to the right to make room for an image. The problem I'm having is how to remove the Image when I don't need it anymore. By setting the column.ImageKey = "" and column.ImageIndex = -1 I remove the Image, but the text isn't on it's starting position. So basically I just reset the Image Objects bitmap but I don't remove the Image Object itself. So I'm wondering how to remove/disable the Image Object.

Upvotes: 1

Views: 2401

Answers (2)

Dev Ops
Dev Ops

Reputation: 96

With a slight change in Sergey Berezovskiy answer, when you have different Text Alignments per column, you can also do;

column.ImageIndex = -1;
column.TextAlign = column.TextAlign;

Upvotes: 2

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236208

To remove image from column you need to restore default text alignment of your column after you removed image:

column.ImageIndex = -1;
column.TextAlign = HorizontalAlignment.Center;

Why it helped? Because when you are assigning image index >= 0 to column header, internally following method is called

ListView.SetColumnInfo(0x10, column);

Which changes text alignment of column

lParam.iImage = column.ActualImageIndex_Internal;
lParam.fmt |= 0x800;
lParam.fmt |= column.TextAlign;
UnsafeNativeMethods.SendMessage(new HandleRef(this, Handle), 
     NativeMethods.LVM_SETCOLUMN, column.Index, lParam);

But it does not change alignment back when you are setting image index to -1.

Upvotes: 7

Related Questions