Listview - 多列 - 更改整行的选择颜色
Listview - Multi column - Change selection color for the whole row
我想更改 ListView 中的选择颜色,而不是默认颜色(蓝色)。我无法根据自己的需要调整找到的任何代码。
这是最接近的代码。
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
部分。它不适用于多列列表视图。这是结果。
选择前:
...之后:
是否可以保留其他列的值并仍然选择整行?
谢谢。
使用 OwnerDraw Listview
要记住的一点是,如果控件是详细信息 View
:DrawColumnHeader
和 DrawSubItem
。
DrawItem
将在控件使用不同的 View
并且没有要绘制的子项时使用。
由于SubItems(0)
与Item.Text
相同,您可以使用DrawSubItem
绘制项目和子项目文本。我不知道该片段位于何处,但这会起作用:
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
将有多少子项就被调用多少次)。
请注意,我还将 Font
包装在 Using
块中以处理它。使用 LimeGreen,结果:
我想更改 ListView 中的选择颜色,而不是默认颜色(蓝色)。我无法根据自己的需要调整找到的任何代码。
这是最接近的代码。
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
部分。它不适用于多列列表视图。这是结果。
选择前:
...之后:
是否可以保留其他列的值并仍然选择整行?
谢谢。
使用 OwnerDraw Listview
要记住的一点是,如果控件是详细信息 View
:DrawColumnHeader
和 DrawSubItem
。
DrawItem
将在控件使用不同的 View
并且没有要绘制的子项时使用。
由于SubItems(0)
与Item.Text
相同,您可以使用DrawSubItem
绘制项目和子项目文本。我不知道该片段位于何处,但这会起作用:
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
将有多少子项就被调用多少次)。
请注意,我还将 Font
包装在 Using
块中以处理它。使用 LimeGreen,结果: