Ownerdrawn ListBox produces flawed graphics

D

Danny van Kasteel

Hi,

I'm trying to make an ownerdrawn ListBox that can display a custom graphic
and 2 lines of text per item.

I have tried to set the following properties on a standard ListBox control:

DrawMode = OwnerDrawFixed
ItemHeight = 32

In the DrawItem Event for the ListBox, I have placed the following code for
testing purposes:

Private Sub ListBox1_DrawItem(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
e.DrawBackground()
e.Graphics.FillRectangle(System.Drawing.Brushes.PowderBlue, e.Bounds)
Console.WriteLine("OnDrawItem")
e.Graphics.DrawRectangle(System.Drawing.Pens.DarkBlue, e.Bounds)
e.DrawFocusRectangle()

End Sub

This code should draw a powder blue background, with a dark blue outline for
each item in the ListBox (I know the outline will lack the right and bottom
borders due to some unfortunate usage of the e.Bounds rectangle, this is for
demonstration purposes only :blush:)

At first all Items draw correctly, but after selecting another item, or
resizing the control or scrolling, the backgrounds (powder blue) all still
draw, and text (omitted in example code) would also draw correctly, but the
dark blue outline rectangles get clipped randomly it seems...

Am I doing something wrong? I doubt it, since I have seen examples by
Microsoft demonstrating how to create an ownerdrawn ListBox with this
problem occurring as well.

Does anyone know how to work around this issue, or have any idea what is
causing this?

TIA,

Danny van Kasteel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top