By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,506 Members | 1,876 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,506 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.SystemBru shes.Highlight,
theBounds)
Else
theGraphics.FillRectangle(System.Drawing.SystemBru shes.Window,
theBounds)
End If
Nov 20 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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

-----------------------

"Robin Tucker" <id*************************@reallyidont.com> wrote in
message news:bv*******************@news.demon.co.uk...
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.SystemBru shes.Highlight,
theBounds)
Else
theGraphics.FillRectangle(System.Drawing.SystemBru shes.Window,
theBounds)
End If

Nov 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.