ObjectListView show icons

2019-02-14 08:14发布

问题:

Trying to put icons in ObjectListview, here's my piece of code where icon should have been put:

objectListView1.SmallImageList = imageList1;

        deleteColumn.IsEditable = true;
        deleteColumn.ImageGetter = delegate
        {
            return 0;
        };
        deleteColumn.AspectGetter = delegate
        {
            return "Delete";
        };

imageList1 already have an image, this code should have put an icon next to "Delete", but it did not appear at all, looked through cookbooks and Google and I still have no idea. Can anyone help me?

this is the full form code in case needed:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
    public Form1()
    {
        InitializeComponent();
        objectListView1.AllowDrop = true;
        objectListView1.DragEnter += new DragEventHandler(objectListView1_DragEnter);
        objectListView1.DragDrop += new DragEventHandler(objectListView1_DragDrop);
        objectListView1.CellEditActivation = BrightIdeasSoftware.ObjectListView.CellEditActivateMode.SingleClick;
        objectListView1.CellEditStarting += deleteItems;
        objectListView1.SmallImageList = imageList1;

        deleteColumn.IsEditable = true;
        deleteColumn.ImageGetter = delegate
        {
            return 0;
        };
        deleteColumn.AspectGetter = delegate
        {
            return "Delete";
        };
    }

    private void objectListView1_DragEnter(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.FileDrop))
        {
            e.Effect = DragDropEffects.Copy;
        }
        else
        {
            e.Effect = DragDropEffects.None;
        }
    }

    private void objectListView1_DragDrop(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.FileDrop, true))
        {
            string[] droppedFiles = (string[]) e.Data.GetData(DataFormats.FileDrop);
            foreach (string path in droppedFiles)
            {
                if (File.Exists(path))
                {
                    FileObject fo = new FileObject(path, "added later"); 
                    objectListView1.AddObject(fo);
                }
            }
        }
    }

    private void deleteItems(object sender, BrightIdeasSoftware.CellEditEventArgs e)
    {
        if(e.Column == deleteColumn)
        {
            e.Cancel = true;
            objectListView1.RemoveObject(e.RowObject);
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

}

}

回答1:

In order for images to appear next to the text in a column, you must:

  1. Connect the ObjectListView to an ImageList (using the SmallImageList property);
  2. Install an ImageGetter delegate for the column that must show the images;
  3. Make sure that there are actually images in the ImageList.

With this done, images will appear (I just tested this).

There is one catch, though. From your question, I suspect that the "Delete" column may not be the first column in the ObjectListView. The above steps only allow you to show an image in the very first column. For subsequent columns, you will have to set the ShowImagesOnSubItems property to True. Could that be it?