The thing to keep in mind with an OwnerDraw Listview
is that there are 2 events to respond to (or override if you are subclassing) if the control is Details View
: DrawColumnHeader
and DrawSubItem
.
DrawItem
would be used when the control is using a different View
and there are no SubItems to draw.
Since SubItems(0)
is the same as Item.Text
, you can use DrawSubItem
to draw the item and subitem text. I cannot tell where that snippet is located, but this will work:
Private Sub lv1_DrawSubItem(sender As Object,
e As DrawListViewSubItemEventArgs) Handles lv1.DrawSubItem
' use sender instead of a hardcodes control ref so
' you can paste this to another LV
Dim myLV As ListView = CType(sender, ListView)
If e.ItemIndex > 0 AndAlso e.Item.Selected Then
Using br As New SolidBrush(Color.Gray)
e.Graphics.FillRectangle(br, e.Bounds)
End Using
Using fnt As New Font(myLV .Font, Nothing)
' use e.SubItem.Text
TextRenderer.DrawText(e.Graphics, e.SubItem.Text,
fnt,
New Point(e.Bounds.Left + 3, e.Bounds.Top + 2),
Color.White)
End Using ' dispose!
Else
e.DrawDefault = True
End If
End Sub
It would appear that you may be using the correct event, but by usingItem.Text
rather than e.SubItem.Text
the Item text will be also be drawn for all SubItems (DrawSubItem
will be called as many times as there are subitems).
Note that I also wrapped the Font
in a Using
block to dispose of it. Using LimeGreen, the result:
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…