Search code examples
c#objectlistview

C# adding multiple images to same column in objectlistview


I try to add image to my first column(it might be change to another column later, it's in first column for now), so far I have do

if (item.Index == 0)
{
    item.ImageGetter = delegate (object RowObj)
    {
        return ((RowObjectModel)RowObj).ImageToShow.ToString();
    };
}

this part at start, I use a custom headerstyle and apply it on constructor while I do that I also do ImageGetter part. I also set my SmallImageList like this

ImageList IList = new ImageList();
IList.Images.Add("MyIcon", Properties.Resources.MyIcon);
mainForm.objListview.SmallImageList = IList;

I have 2 problems with this code, first I can't set my image. It's not showing on my listview. What I do to achieve that is this :

(objListview.GetItem(z).RowObject as RowObjectModel).ImageToShow = ThumbnailImages.MyIcon;

my enum is like this :

public enum ThumbnailImages
{
    NULL = 0,
    MyIcon = 1,
    MyIcon2 = 2,
    MyIcon3 = 3,
    MyIcon4 = 4,
    MyIcon5 = 5
}

Second problem is I have literally no clue on how I can add a second image in the same column of same row. I'm not even sure if this is possible.. But I have to do it somehow so I'm open to any ideas.

EDIT : Okay I found the solution to my first problem. I was not using the UpdateObject/UpdateObjects method. I marked all my items with proper images they should show and use this method and everything worked. Now all I need is to find a way to show 2 images at the same time in 1 cell.

EDIT 2 : About my second problem I found this class --> ImagesRenderer

http://objectlistview.sourceforge.net/cs/ownerDraw.html#imagesrenderer

But I could not found any working solution so far and I don't have any clue on how this is working?


Solution

  • Now all I need is to find a way to show 2 images at the same time in 1 cell.

    You can work around the requirement to show 2 images by showing 1 image that contains both. The example below does that. One note is that you should probably build a cache up front of all the combinations so you can return them at high speed rather than building them in the ImageGetter delegate

    enter image description here

    olv.ShowGroups = false;
    
    //make 2 images
    var img1 = new Bitmap(10, 10);
    var g = Graphics.FromImage(img1);
    g.FillRectangle(new SolidBrush(Color.Pink),2,2,8,8 );
    
    var img2 = new Bitmap(10, 10);
    var g2 = Graphics.FromImage(img2);
    g2.FillRectangle(new SolidBrush(Color.Blue),2,2,8,8 );
    
    var col1 = new OLVColumn();
    col1.AspectName = "ToString";
    
    col1.ImageGetter += delegate(object rowObject)
    {
        if(rowObject == "1")
            return img1;
    
        if(rowObject == "2")
            return img2;
    
        if (rowObject == "3")
        {
            var comboImg = new Bitmap(img1.Width + img2.Width, img1.Height + img2.Height);
            using (var g3 = Graphics.FromImage(comboImg))
            {
                g3.DrawImage(img1,new Point(0,0));
                g3.DrawImage(img2,new Point(img1.Width,0));
            }
    
            return comboImg;
        }
    
    
        return null;
    };
    
    olv.Columns.Add(col1);
    
    olv.AddObject("1");
    olv.AddObject("2");
    olv.AddObject("3");
    
    }