Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove icon space after null SmallImageList property of ListView

The problem is, once the SmallImageList is set to imgList1, it never "releases" the icon spacing, even when the SmallImageList is set to null. The item is always indented the same whether there is an icon or not.

any solution?

like image 332
silly_dg Avatar asked Oct 18 '25 13:10

silly_dg


2 Answers

This is an unusual thing to do, the .NET ListView wrapper wouldn't handle it. You could try recreating the native Windows control to reset it. Not sure it this will have side-effects, you'd have to try. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox, replacing your original.

using System;
using System.Windows.Forms;

class MyListView : ListView {
    public new ImageList SmallImageList {
        get { return base.SmallImageList; }
        set {
            base.SmallImageList = value;
            if (value == null && base.IsHandleCreated) this.RecreateHandle();
        }
    }
}
like image 103
Hans Passant Avatar answered Oct 20 '25 16:10

Hans Passant


Thank you to Hans for the solution. The code below follows his example, but calls the RecreateHandle method using reflection.

this.listView1.SmallImageList = null;
MethodInfo mInfo = this.listView1.GetType().GetMethod(
    "RecreateHandle", BindingFlags.Instance | BindingFlags.NonPublic);
mInfo.Invoke(this.listView1, null);

HTH!

like image 38
Phil Lambert Avatar answered Oct 20 '25 15:10

Phil Lambert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!