vb.net 如何在列表视图中使选定颜色成为背景颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24719006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make selected color the background color in listview
提问by Omer
How can I make the background color the selected color of a selected row in List View object using vb.net.
如何使用 vb.net 使背景颜色成为列表视图对象中选定行的选定颜色。
I did saw an example with the Ownerdraw set to true - but it messes up the column header. Although I was able to loop through the sub items to make the row selected color the back color
我确实看到了一个 Ownerdraw 设置为 true 的示例 - 但它弄乱了列标题。尽管我能够循环遍历子项以使所选行的颜色成为背景色
Here's the Code block :
这是代码块:
For i = 0 To LV_ProductsEdit.SelectedItems.Item(0).SubItems.Count - 1
LV_ProductsEdit.SelectedItems.Item(0).SubItems(i).BackColor = Color.Gold
Next
I have a similar loop that reverts it to the original transparent color when the new item is selected.
我有一个类似的循环,可以在选择新项目时将其恢复为原始透明颜色。
Any thoughts.
有什么想法吗。
Thanks
谢谢
Omer
欧玛
采纳答案by ??ssa P?ngj?rdenlarp
I am still not quite sure what the problem is. You dont need to loop thru SubItems though. Each item has a UseItemStyleForSubItemsproperty which tells the subitems to use the same Font, ForeColor and BackColor as the parent Item. This defaults to True, so setting the Item backcolor should be enough:
我仍然不太确定问题是什么。不过,您不需要循环遍历 SubItems。每个项目都有一个UseItemStyleForSubItems属性,它告诉子项目使用与父项目相同的字体、前景色和背景色。这默认为 True,因此设置 Item 背景色就足够了:
For Each lvi As ListViewItem In myLV.SelectedItems
lvi.BackColor = Color.Gold
Next
The original color is not Transparent, but should be SystemColors.Window.
原始颜色不是透明的,但应该是SystemColors.Window。
You can also reset HideSelectionto False. When the LV does not have the focus, the selected items will still be highlighted (grey) which seems to be what you were asking in the original question.
您也可以重置HideSelection为 False。当 LV 没有焦点时,所选项目仍将突出显示(灰色),这似乎是您在原始问题中提出的问题。
回答by wedsa5
If all you want to do is change the selection color from blue to something else, here's the code I found to work. First, set ownerdraw to true. Here, my listview is called ListViewQuote.
如果您只想将选择颜色从蓝色更改为其他颜色,这里是我发现可以工作的代码。首先,将 ownerdraw 设置为 true。在这里,我的列表视图称为 ListViewQuote。
Private Sub ListViewQuote_DrawColumnHeader(sender As Object, e As DrawListViewColumnHeaderEventArgs) Handles ListViewQuote.DrawColumnHeader
e.DrawDefault = True
End Sub
Private Sub ListViewQuote_DrawItem(sender As Object, e As DrawListViewItemEventArgs) Handles ListViewQuote.DrawItem
If e.Item.Selected = False Then
e.DrawDefault = True
End If
End Sub
Private Sub ListViewQuote_DrawSubItem(sender As Object, e As DrawListViewSubItemEventArgs) Handles ListViewQuote.DrawSubItem
If e.Item.Selected = True Then
e.Graphics.FillRectangle(New SolidBrush(FromArgb(255, 0, 0)), e.Bounds)
TextRenderer.DrawText(e.Graphics, e.SubItem.Text, New Font(Me.Font, Nothing), New Point(e.Bounds.Left + 3, e.Bounds.Top + 2), HighlightText)
Else
e.DrawDefault = True
End If
End Sub
To change the color from what I have, just change "New SolidBrush(FromArgb(255, 0, 0)" to the brush you want. The forecolor of the text is "HighlightText" which can also be changed to any color.
要更改我拥有的颜色,只需将“New SolidBrush(FromArgb(255, 0, 0)”更改为您想要的画笔。文本的前景色是“HighlightText”,它也可以更改为任何颜色。
This works for the Details view of a listview.
这适用于列表视图的详细信息视图。

