vb.net 在VB.NET中以编程方式选择并突出显示一行ListView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45693124/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 20:19:29  来源:igfitidea点击:

programmatically select and highlight a row of a ListView in VB.NET

vb.netlistview

提问by DJDave

I want to do something seemingly simple - programmatically select and highlighta row of a ListView in VB.NET.

我想做一些看似简单的事情 -在 VB.NET 中以编程方式选择并突出显示ListView 的一行。

VB.NET: How to dynamically select a list view item?

VB.NET:如何动态选择列表视图项?

tells me what should to be all that is needed, but it isn't. The row is selected, but not highlighted.

告诉我什么应该是所有需要的,但事实并非如此。该行被选中,但未突出显示。

http://vbcity.com/forums/t/28260.aspx

http://vbcity.com/forums/t/28260.aspx

tells me about the "HideSelection" property and the .Focus() method (also referenced at Select programmatically a row of a Listview), which sounded hopeful, but the best I can get is the faint highlight mentioned, I want the full monty. I tried a Noddy example with just a ListView, in Details mode, FullRowSelection = true, HideSelection = False, one columnheader defined and then

告诉我“HideSelection”属性和 .Focus() 方法(也在Select programmatically a row of a Listview 中引用),这听起来很有希望,但我能得到的最好的是提到的微弱亮点,我想要完整的蒙蒂。我尝试了一个只有 ListView 的 Noddy 示例,在详细信息模式下,FullRowSelection = true,HideSelection = False,定义了一个列标题,然后

    ListView1.Items.Add("Red")
    ListView1.Items.Add("Orange")
    ListView1.Items.Add("Yellow")
    ListView1.Items.Add("Green")

    ListView1.Items(2).Selected = True

I get this

我明白了

enter image description here

在此处输入图片说明

but I want this

但我想要这个

enter image description here

在此处输入图片说明

I can simulate highlighting by adding these lines

我可以通过添加这些行来模拟突出显示

ListView1.SelectedItems(0).BackColor = Color.CornflowerBlue
ListView1.SelectedItems(0).ForeColor = Color.White

but then how can I be sure to undo the artificial highlight if the row can be implicitly as well as explicitly deselected? Do I have to think of all the possible cases? That's too much work for what should be a simple operation. Plus, since I want to color-code my rows, there is an additional challenge that when I undo the highlight color, I have to figure out what color is appropriate at that point. Is there a better, simpler way?

但是,如果该行既可以隐式也可以显式取消选择,我怎么能确定取消人工突出显示呢?我是否必须考虑所有可能的情况?对于一个简单的操作来说,这实在是太多了。另外,由于我想对我的行进行颜色编码,还有一个额外的挑战是,当我撤消突出显示颜色时,我必须弄清楚此时哪种颜色是合适的。有没有更好、更简单的方法?

采纳答案by djv

You can access the Graphics object used to draw each item, and draw them yourself.

您可以访问用于绘制每个项目的 Graphics 对象,并自己绘制它们。

Make a new project with a Button and ListView. Paste the following code:

使用 Button 和 ListView 创建一个新项目。粘贴以下代码:

Form_Load to use multiple subitems

Form_Load 使用多个子项

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Me.ListView1.OwnerDraw = True ' or else can't handle DrawItem event
    ListView1.Columns.Add("ColumnHeader1")
    ListView1.Columns.Add("ColumnHeader2")
    ListView1.Columns.Add("ColumnHeader3")
    Me.ListView1.Items.Add("Red")
    Me.ListView1.Items.Add("Orange")
    Me.ListView1.Items.Add("Yellow")
    Me.ListView1.Items.Add("Green")
    ListView1.Items(0).SubItems.Add("Strawberry")
    ListView1.Items(0).SubItems.Add("Apple")
    ListView1.Items(1).SubItems.Add("Pepper")
    ListView1.Items(1).SubItems.Add("Apricot")
    ListView1.Items(2).SubItems.Add("Plum")
    ListView1.Items(2).SubItems.Add("Banana")
    ListView1.Items(3).SubItems.Add("Apple")
    ListView1.Items(3).SubItems.Add("Lime")
End Sub

Three handlers for the ListView's drawing related events. Code copied from this answer

ListView 的绘图相关事件的三个​​处理程序。从这个答案复制的代码

Private Sub listView1_DrawColumnHeader(sender As Object, e As DrawListViewColumnHeaderEventArgs) Handles ListView1.DrawColumnHeader
    e.DrawDefault = True
End Sub

Private Sub listView1_DrawSubItem(sender As Object, e As DrawListViewSubItemEventArgs) Handles ListView1.DrawSubItem
    Const TEXT_OFFSET As Integer = 1
    ' I don't know why the text is located at 1px to the right. Maybe it's only for me.
    Dim listView As ListView = DirectCast(sender, ListView)

    ' Check if e.Item is selected and the ListView has a focus.
    If Not listView.Focused AndAlso e.Item.Selected Then
        Dim rowBounds As Rectangle = e.SubItem.Bounds
        Dim labelBounds As Rectangle = e.Item.GetBounds(ItemBoundsPortion.Label)
        Dim leftMargin As Integer = labelBounds.Left - TEXT_OFFSET
        Dim bounds As New Rectangle(rowBounds.Left + leftMargin, rowBounds.Top, If(e.ColumnIndex = 0, labelBounds.Width, (rowBounds.Width - leftMargin - TEXT_OFFSET)), rowBounds.Height)
        Dim align As TextFormatFlags
        Select Case listView.Columns(e.ColumnIndex).TextAlign
            Case HorizontalAlignment.Right
                align = TextFormatFlags.Right
                Exit Select
            Case HorizontalAlignment.Center
                align = TextFormatFlags.HorizontalCenter
                Exit Select
            Case Else
                align = TextFormatFlags.Left
                Exit Select
        End Select
        TextRenderer.DrawText(e.Graphics, e.SubItem.Text, listView.Font, bounds, SystemColors.HighlightText, align Or TextFormatFlags.SingleLine Or TextFormatFlags.GlyphOverhangPadding Or TextFormatFlags.VerticalCenter Or TextFormatFlags.WordEllipsis)
    Else
        e.DrawDefault = True
    End If
End Sub

Private Sub listView1_DrawItem(sender As Object, e As DrawListViewItemEventArgs) Handles ListView1.DrawItem
    Dim listView As ListView = DirectCast(sender, ListView)

    ' Check if e.Item is selected and the ListView has a focus.
    If Not listView.Focused AndAlso e.Item.Selected Then
        Dim rowBounds As Rectangle = e.Bounds
        Dim leftMargin As Integer = e.Item.GetBounds(ItemBoundsPortion.Label).Left
        Dim bounds As New Rectangle(leftMargin, rowBounds.Top, rowBounds.Width - leftMargin, rowBounds.Height)
        e.Graphics.FillRectangle(SystemBrushes.Highlight, bounds)
    Else
        e.DrawDefault = True
    End If
End Sub

Button click handler to simulate item(2) selected

按钮单击处理程序以模拟选定的项目 (2)

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Me.ListView1.Items(2).Selected = True
End Sub

This will draw the background color regardless of focus. You have a lot of control over other colors and fonts going this route too.

无论焦点如何,这都会绘制背景颜色。您也可以对沿着这条路线的其他颜色和字体进行很多控制。

Here, the button has been clicked, to select item 2, while the button still has focus, and item 2 is selected.

在这里,按钮已经被点击,选择了项目 2,而按钮仍然有焦点,项目 2 被选中。

enter image description here

在此处输入图片说明