OwnerDrawn ListBox失去焦点时为空

本文关键字:焦点 ListBox 失去 OwnerDrawn | 更新日期: 2023-09-27 17:58:55

我订阅了ListBox.DrawItem事件,当它有焦点时,它画得很好,但当我导航离开时,它什么也没画。

private void lbHeader_DrawItem(object sender, DrawItemEventArgs e)
{
    e.Graphics.FillRectangle(SystemBrushes.Window, e.Bounds);
    e.Graphics.FillRectangle(SystemBrushes.GradientActiveCaption, e.Bounds);
    int left = e.Bounds.Left + ImageList.ImageSize.Width;
    for (int i = 0; i < Columns.Count; i++)
    {
        Rectangle textRect = Rectangle.FromLTRB(
            left, e.Bounds.Top, e.Bounds.Right, e.Bounds.Bottom);
       TextRenderer.DrawText(e.Graphics, "Some Text", e.Font, textRect,
           FontColor, TextFormatFlags.Left | TextFormatFlags.VerticalCenter);
       left += Columns[i].Width;
    }
}

OwnerDrawn ListBox失去焦点时为空

检查此

   private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
    {
        if (e.Index > 0)
        {
            e.DrawBackground();
            e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), this.Font, Brushes.Black, e.Bounds);
        }
    }