0

I have a problem with customized CheckedListBox class. I've got the code that uses custom ComboBox class with override OnDrawItem (to include pictures). My goal is to change appearance so that the items appear in CheckedListBox class (also with override, to include pictures) instead of custom ComboBox. The problem is that the pictures are not showing correctly: they don't show at all until I click on the item and hold the mouse, or scroll the listbox... Also, checked items stay checked even after I uncheck them in code during runtime (only visually, logically they are OK). Any idea what's going on?

public class CategoryCheckBoxListImagified : System.Windows.Forms.CheckedListBox
{
   // other stuff....
   // ...

    protected override void OnDrawItem(DrawItemEventArgs e)
    {
        if (this.Items.Count == 0) return;

        if (DisplayMode == DisplayModeEnum.TextOnly || DisplayMode == DisplayModeEnum.TextAndImages)
            base.OnDrawItem(e);

        Category category = (Category)this.Items[e.Index];

        if (DisplayMode == DisplayModeEnum.ImagesOnly || DisplayMode == DisplayModeEnum.TextAndImages)
        {
            string imagePath = Path.Combine(IMAGE_FOLDER, category.ImageName ?? "");
            Image image = null;
            if (File.Exists(imagePath))
            {
                image = Bitmap.FromFile(imagePath);
            }
            else
            {
                imagePath = Path.Combine(IMAGE_FOLDER, IMAGE_NO_IMAGE);
                image = Bitmap.FromFile(imagePath);
            }

            // e.Bounds contain the area for the whole item. Text is 16 pixels high.
            Rectangle drawImage = new Rectangle(20, e.Bounds.Top + 12, 64, 64);
            e.Graphics.DrawImage(image, drawImage);
        }
    }

}

c4551u5
  • 111
  • 1
  • 6
  • 1
    you are probably at war with the standard ListBox rendering methods. Notice that ListBox has a DrawMode while CLB does not. I did something similar but inherited from listBox to take control of things from the start. – Ňɏssa Pøngjǣrdenlarp Dec 25 '14 at 15:06
  • thx for the tip, but I managed to figure it out - at least the part of the problem with the incorrect display of pictures: the error was in line `Rectangle drawImage = new Rectangle(20, e.Bounds.Top + 12, 64, 64);` it should be `Rectangle drawImage = new Rectangle(e.Bounds.Left+20, e.Bounds.Top + 12, 64, 64);` – c4551u5 Dec 26 '14 at 03:01

0 Answers0