列表视图 - 多塔 - 更改选择颜色整行 [英] Listview - Multi column - Change selection color for the whole row

查看:251
本文介绍了列表视图 - 多塔 - 更改选择颜色整行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想改变颜色的选择在ListView,从默认(蓝色)。我无法适应任何code,我发现我的需求。

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.

下面是code最接近。

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

主要的问题是 e.Item.Text 部分。它不为多列的列表视图工作。下面是结果。

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

选择之前:

Before selection :

...之后:

...and after :

是否有可能从其他列preserve值,并有仍然充满行选择?

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

感谢。

推荐答案

要记住用的OwnerDraw 列表视图的事情是,有 2的事件,以响应(或重写,如果你继承)如果控制详情查看 DrawColumnHeader DrawSubItem

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 将在控制使用不同的查看,也没有分项绘制使用。

DrawItem would be used when the control is using a different View and there are no SubItems to draw.

由于子项(0)相同 Item.Text ,您可以使用 DrawSubItem 来绘制项,子项文本。我不知道在哪里的片段的位置,但是这将工作:

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

这样看来,你可能会使用正确的事件,但通过使用 Item.Text ,而不是 e.SubItem.Text 项文本也将被绘制所有子项( DrawSubItem 将因为有子项称为很多次)。

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:

这篇关于列表视图 - 多塔 - 更改选择颜色整行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆