覆盖OnDrawItem(CheckedListBox)

时间:2014-12-25 15:00:15

标签: c# override checkboxlist ondrawitem

我的自定义CheckedListBox类有问题。我有使用自定义ComboBox类的代码覆盖OnDrawItem(包括图片)。我的目标是更改外观,以便项目显示在CheckedListBox类中(也可以使用覆盖,包括图片)而不是自定义ComboBox。问题是图片显示不正确:直到我点击项目并按住鼠标或滚动列表框才会显示它们...此外,即使我在代码中取消选中它们,检查项目也会保持检查状态运行时(只有在视觉上,逻辑上它们都可以)。知道发生了什么事吗?

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

}

0 个答案:

没有答案