How do I return an image of a property to a ListView?

Env .: VS 2008, .NET 2.0, WinForms

I have a listview in Tile mode. Some elements have an associated image. Some don't.

The content (list items) is updated frequently when the user clicks on some of the controls. When this happens, it sometimes appears that none of the new elements have images. In this case, I would like to get rid of the white space to the left of the elements reserved for images. I tried the following pseudocode to temporarily get rid of the image list

list.Items.Clear();
FillList();
list.LargeImageList= (none of the items has image) ? null : MyImageList;

      

But that doesn't work: the empty space is still there. I am also trying to redraw the control but to no avail.

alt text http://apptranslator.com/_so/so_list1.jpg alt text http://apptranslator.com/_so/so_list2.jpg alt text http://apptranslator.com/_so/so_list3.jpg

Left: list with images.

Medium: list without images. Space for images is visible.

Right: how I would like when there are no images.

EDIT: I did this test as well: Don't assign a list of images in the designer. If the first display contains no images, then I get the expected result. Then I click to display images (I get them). I click again to go back to the selection without images: the image space does not disappear.

Also, Hath, no, I don't use small or state images. Large images only.

What can I do? TIA.

0


source to share


3 answers


Are you sure? It works for me in this test case:

using System;
using System.Windows.Forms;
public class MainForm : Form
{
    private System.ComponentModel.IContainer components = null;
    private System.Windows.Forms.ListView listView;
    private System.Windows.Forms.ImageList emptySmallImageList;
    private System.Windows.Forms.ImageList largeImageList;
    private System.Windows.Forms.Button imageListSmallButton;
    private System.Windows.Forms.Button imageListLargeButton;

    public MainForm()
    {
        InitializeComponent();
    }

    private void OnImageListSmallButtonClick(object sender, EventArgs e)
    {
        this.listView.LargeImageList = emptySmallImageList;       
    }

    private void OnImageListLargeButtonClick(object sender, EventArgs e)
    {
        this.listView.LargeImageList = largeImageList;
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    private void InitializeComponent()
    {
        this.components = new System.ComponentModel.Container();
        System.Windows.Forms.ListViewItem listViewItem5 = new System.Windows.Forms.ListViewItem("fgsdfg");
        System.Windows.Forms.ListViewItem listViewItem6 = new System.Windows.Forms.ListViewItem("sdfgsdfg");
        System.Windows.Forms.ListViewItem listViewItem7 = new System.Windows.Forms.ListViewItem("sdfgsdfgsdfg");
        System.Windows.Forms.ListViewItem listViewItem8 = new System.Windows.Forms.ListViewItem("sdfgsdfg");
        this.listView = new System.Windows.Forms.ListView();
        this.largeImageList = new System.Windows.Forms.ImageList(this.components);
        this.emptySmallImageList = new System.Windows.Forms.ImageList(this.components);
        this.imageListSmallButton = new System.Windows.Forms.Button();
        this.imageListLargeButton = new System.Windows.Forms.Button();
        this.SuspendLayout();
        // 
        // listView
        // 
        this.listView.Dock = System.Windows.Forms.DockStyle.Fill;
        this.listView.Items.AddRange(new System.Windows.Forms.ListViewItem[] {
        listViewItem5,
        listViewItem6,
        listViewItem7,
        listViewItem8});
        this.listView.LargeImageList = this.largeImageList;
        this.listView.Location = new System.Drawing.Point(0, 0);
        this.listView.Name = "listView";
        this.listView.Size = new System.Drawing.Size(292, 266);
        this.listView.TabIndex = 0;
        this.listView.UseCompatibleStateImageBehavior = false;
        this.listView.View = System.Windows.Forms.View.Tile;
        // 
        // largeImageList
        // 
        this.largeImageList.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
        this.largeImageList.ImageSize = new System.Drawing.Size(32, 32);
        this.largeImageList.TransparentColor = System.Drawing.Color.Transparent;
        // 
        // emptySmallImageList
        // 
        this.emptySmallImageList.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
        this.emptySmallImageList.ImageSize = new System.Drawing.Size(1, 1);
        this.emptySmallImageList.TransparentColor = System.Drawing.Color.Transparent;
        // 
        // imageListSmallButton
        // 
        this.imageListSmallButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
        this.imageListSmallButton.Location = new System.Drawing.Point(175, 12);
        this.imageListSmallButton.Name = "imageListSmallButton";
        this.imageListSmallButton.Size = new System.Drawing.Size(95, 23);
        this.imageListSmallButton.TabIndex = 1;
        this.imageListSmallButton.Text = "ImageList 1x1";
        this.imageListSmallButton.UseVisualStyleBackColor = true;
        this.imageListSmallButton.Click += new System.EventHandler(this.OnImageListSmallButtonClick);
        // 
        // imageListLargeButton
        // 
        this.imageListLargeButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
        this.imageListLargeButton.Location = new System.Drawing.Point(175, 53);
        this.imageListLargeButton.Name = "imageListLargeButton";
        this.imageListLargeButton.Size = new System.Drawing.Size(95, 23);
        this.imageListLargeButton.TabIndex = 2;
        this.imageListLargeButton.Text = "ImageList 32x32";
        this.imageListLargeButton.UseVisualStyleBackColor = true;
        this.imageListLargeButton.Click += new System.EventHandler(this.OnImageListLargeButtonClick);
        // 
        // MainForm
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(292, 266);
        this.Controls.Add(this.imageListLargeButton);
        this.Controls.Add(this.imageListSmallButton);
        this.Controls.Add(this.listView);
        this.Name = "MainForm";
        this.Text = "Form1";
        this.ResumeLayout(false);

    }

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new MainForm());
    }
}

      


I have what you see now, I don't understand why this doesn't do the resizing ... I wonder if Doing SendMessage would do the trick, but I duno what the message would be.



better work arround, I can see if you do this:

imageList1.ImageSize = new Size(1,1);

      


I created a test one and it seemed to work if I didn't install StateImageList

. Do you also cleanse StateImageList

?

+1


source


If you want full control, use the listview.drawitem event.



0


source


I've had this exact problem too lately. Something I noticed is that no matter what the LargeImageList ImageSize is for (or even if LargeImageList is NULL!), The ListView will always act as if the LargeImageList.ImageSize is still specified by the last value.

In my program, I didn't use SmallImageList at all, so it was NULL all the time. In my constructor for the form, I just set this to a new ImageList () for the ListView and locked the size at (1,1). The problem is gone! It seemed like a stupid mistake.

    public frmMain()
    {
        InitializeComponent();
        this.Text = Program.AppName;

        lstSightings.SmallImageList = new ImageList();
        lstSightings.SmallImageList.ImageSize = new Size(1, 1);

        RefreshItems();
    }

      

0


source







All Articles