My owner draw list box contorl doesn't "invalidate" old selected items!

R

Robin Tucker

Hi ppl,

My owner draw list box controls do not "refresh" old selected items when a
new selection is made. This means that as you click to make selections, the
previously selected items stay highlighted along with the new ones too. It
draws correctly when I minimize the window and then maximise it again, but I
just don't seem to be getting a "DrawItem" event for switching of an item
from Selected to NotSelected! Any ideas???

Here is the code I use:

Private Sub ListBox_DrawItem(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.DrawItemEventArgs) Handles ListBox.DrawItem

' Guard

If e.Index < 0 Then
Exit Sub
End If

' Get the object we wish to draw

Dim theItem As DataThumbnailViewItem = CType(ListBox.Items()(e.Index),
DataThumbnailViewItem)

' and render it.

If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
theItem.Render(e.Graphics, e.Bounds, True)
Else
theItem.Render(e.Graphics, e.Bounds, False)
End If
End Sub

and the code in the DataThumbnailViewItem render is something like:

' If selected, draw box border in selected colour

If bIsSelected Then
theGraphics.FillRectangle(System.Drawing.SystemBrushes.Highlight,
theBounds)
Else
theGraphics.FillRectangle(System.Drawing.SystemBrushes.Window,
theBounds)
End If
 
K

Ken Tucker [MVP]

Hi,

Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As
System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem

Dim g As Graphics = e.Graphics

Dim s As String

Dim d As Date

Dim br As Brush = SystemBrushes.WindowText

Dim brBack As Brush

Dim rDraw As Rectangle

Dim bSelected As Boolean = CBool(e.State And DrawItemState.Selected)

rDraw = e.Bounds

rDraw.Inflate(-1, -1)

If bSelected Then

brBack = Brushes.LightBlue

g.FillRectangle(Brushes.LightBlue, rDraw)

g.DrawRectangle(Pens.Blue, rDraw)

Else

brBack = Brushes.White

g.FillRectangle(brBack, e.Bounds)

End If

br = Nothing

brBack = Nothing

rDraw = Nothing

Try

s = ListBox1.Items.Item(e.Index).ToString

Catch

s = ""

End Try

Dim x, y As Integer

x = e.Bounds.Left + 25

y = e.Bounds.Top + 1

Dim c As Color

Dim b As SolidBrush

c = Color.FromName("Red")

b = New SolidBrush(c)

g.FillRectangle(b, x - 20, y + 2, 8, 8)

g.DrawString(s, ListBox1.Font, Brushes.Black, x, y)

End Sub



Ken
 

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