Listview - Multi column - Change selection color for the whole row

爷,独闯天下 提交于 2019-12-18 09:37:50

问题


I would like to change selection color in a ListView, from a default (blue). I can not adapt any code that I found to my needs.

Here is the code that is closest.

  If e.Item.Selected = True Then
        e.Graphics.FillRectangle(New SolidBrush(Color.Gray), e.Bounds)
        TextRenderer.DrawText(e.Graphics, e.Item.Text, New Font(ListView2.Font, Nothing), New Point(e.Bounds.Left + 3, e.Bounds.Top + 2), Color.White)
    Else
        e.DrawDefault = True
    End If

The main problem is e.Item.Text part. It doesn't work for multi column listview. Here is the result.

Before selection :

...and after :

Is it possible to preserve values from other columns and still have full row selection?

Thanks.


回答1:


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:



来源:https://stackoverflow.com/questions/29804280/listview-multi-column-change-selection-color-for-the-whole-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!