C# 覆盖OnDrawItem(选中列表框)

C# 覆盖OnDrawItem(选中列表框),c#,overriding,checkboxlist,ondrawitem,C#,Overriding,Checkboxlist,Ondrawitem,我对定制的CheckedListBox类有问题。我得到了使用自定义ComboBox类覆盖OnDrawItem(包括图片)的代码。我的目标是更改外观,使项目显示在CheckedListBox类中(也带有覆盖,以包括图片),而不是自定义组合框。问题是图片显示不正确:在我单击项目并按住鼠标或滚动列表框之前,图片根本不会显示。。。此外,即使在运行时我在代码中取消选中选中项,选中项仍保持选中状态(仅在视觉上,逻辑上它们是OK的)。知道发生了什么吗 public class CategoryCheckBox

我对定制的CheckedListBox类有问题。我得到了使用自定义ComboBox类覆盖OnDrawItem(包括图片)的代码。我的目标是更改外观,使项目显示在CheckedListBox类中(也带有覆盖,以包括图片),而不是自定义组合框。问题是图片显示不正确:在我单击项目并按住鼠标或滚动列表框之前,图片根本不会显示。。。此外,即使在运行时我在代码中取消选中选中项,选中项仍保持选中状态(仅在视觉上,逻辑上它们是OK的)。知道发生了什么吗

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);
        }
    }

}

您可能正在与标准的列表框渲染方法进行斗争。请注意,ListBox有一个DrawMode,而CLB没有。我做了一些类似的事情,但从listBox继承下来,从start.thx开始控制事情,但我设法解决了它——至少部分问题是图片显示不正确:错误在line
Rectangle drawImage=new Rectangle(20,e.Bounds.Top+12,64,64)它应该是
rectangledrawimage=新矩形(e.Bounds.Left+20,e.Bounds.Top+12,64,64)