winforms 窗口的LViewItem,窗体用鼠标点击选择

slsn1g29  于 2023-05-29  发布在  其他
关注(0)|答案(1)|浏览(119)

在ListView中有3列。首先是ListViewItem,它有2个ListViewSubitems。下面是引用它的程序行。

Public Class Form1
    Friend Shared WithEvents LView As ListView = New ListView

    Shared Sub ShowTable(codes As Byte())
        LView.View = View.Details
        LView.Width = 800
        LView.Height = 300
        LView.FullRowSelect = False
        LView.GridLines = True
        LView.Columns.Add("Id1", 200)     'This is ListViewItem
        LView.Columns.Add("Id2", 230)     'This and the next line are the ListViewSubitems
        LView.Columns.Add("Id3", 345)

'Here is the loading of the items of the table
       
        DisplayForm.Controls.Add(LView)  'DisplayForm is the Form's name
        LView.Show()

    End Sub

出现表格后,要选择其中的一项,用鼠标点击它。这将调用MouseClick处理程序,代码如下

Private Shared Sub LView_MouseClick(sender As Object, e As MouseEventArgs) Handles LView.MouseClick

        If e.Button.Equals(MouseButtons.Right) Then
            DelSelected(e)   'This sub define the clicked line
        Else
            MessageBox.Show("This is left click", "Click message", MessageBoxButtons.OKCancel)
            Exit Sub
        End If
    End Sub
End Class

仅当单击ListViewItem时调用MouseClick事件。如果单击子项,则什么也不会发生,即使它们在ListView上。所需要的是,在单击整行时调用事件子,但对于任何隐含其值的活动,ListViewSubitems保持禁用,鼠标单击仅调用处理程序函数来定义单击的元素。因此,将FullRowSelect属性设置为True并不好。在这种情况下,整行都是彩色的,并且单击子项(而不是其本身)将返回父ListItem,因此无法定义单击了哪个子项。也许通过点击的坐标就可以定义它,用了很多计算,但是很消耗。

j2datikz

j2datikz1#

简短的回答是:ListView不能在任何地方单击,而只能在属性FullRowSelect=False的情况下单击“ListViewItem”-s。此外,ListViewSubitem不具有IndexSelectedFocused的性质。为了解决这个问题,我在处理ListView数据的类中应用了这个方法。
ShowTable子行中的第一个更改

LView.FullRowSelect = False
to
LView.FullRowSelect = True

在声明部分

Shared oldcolor1 As Color = Nothing, oldcolor2 As Color = Nothing
    Shared selitemindex As Integer = 0, selsubitemindex As Integer = 0
    Shared oldcolor1 As Color = Nothing, oldcolor2 As Color = Nothing
    Shared selitemindex As Integer = 0, selsubitemindex As Integer = 0

在Sub过程中

Private Shared Sub ItemSelected(e As MouseEventArgs)

        Dim coordx, coordy, buttpress As Integer
        Dim rowitem As ListViewItem
        Dim info As ListViewHitTestInfo
        If Not oldcolor1.IsEmpty Then
            With LView.Items(selitemindex).SubItems(selsubitemindex)
                .BackColor = oldcolor1
                .ForeColor = oldcolor2
            End With
        End If
        coordx = e.Location.X
        coordy = e.Location.Y
        rowitem = LView.GetItemAt(coordx, coordy)
        info = LView.HitTest(coordx, coordy)
        selitemindex = info.Item.Index
        selsubitemindex = info.Item.SubItems.IndexOf(info.SubItem)
        With LView.Items(selitemindex)
            .Focused = False
            .Selected = False
            oldcolor1 = .BackColor
            oldcolor2 = .ForeColor
            .UseItemStyleForSubItems = False
            .SubItems(selsubitemindex).BackColor = Color.DodgerBlue
            .SubItems(selsubitemindex).ForeColor = Color.White
        End With
    End Sub

只是为了扩展变量,可以使用ListView.FocusedItem属性来代替HitTest方法,当然代码必须进行调整。随你便必须知道ListViewSubItem在这种情况下既不是Focused也不是Selected。这部分代码基于链接表中的注解问题。关于这个问题,可以找到更多的信息。我就是这样熬过来的

相关问题