ObjectListView show icons
Asked Answered
G

1

4

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)
    {

    }

}

}

Gulgee answered 22/3, 2013 at 8:34 Comment(3)
Have you looked at the documentation on MSDN? They usually provide sufficient examples. Look here and here.Tails
ObjectListView is not actually a Microsoft product, and the problem seems to be with it rather than with the ImageList.Extramundane
thanks guys, problem is solved, my ShowImageOnSubItems is set to false, thank you very much form the answer belowGulgee
E
7

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?

Extramundane answered 22/3, 2013 at 8:53 Comment(2)
no, delete column is on the third column (requirement by the client, can't change it)Gulgee
hey thanks, i missed the ShowImagesOnSubItems, really a problem because there's no tutorial (or maybe I haven't looked hard enough) about it. Thanks a lot :)Gulgee

© 2022 - 2024 — McMap. All rights reserved.