【问题标题】:VB.Net Listview GroupsVB.Net 列表视图组
【发布时间】:2014-05-31 12:19:18
【问题描述】:

我是新来的,提出了我的第一个问题: 我有一些组的列表视图,每个组都有一些项目。 我在 Listview 中添加了一个 ContextMenuStrip,当我右键单击所选项目时,我想获取它所属的组的名称。 有人可以帮我怎么做吗?

If LV1.SelectedItems(0).Text = LV1.Groups(0).ToString Then
    MsgBox("Hi")
End If

【问题讨论】:

  • 显示您到目前为止尝试过的代码,以便我们有一个起点来提供帮助。否则就像您要求我们为您编写代码一样。
  • 我试过这样:puu.sh/98Txl/3eec32d3ea.png 但这有点蹩脚,我不喜欢创建一个无休止的长 if / elseif 语句,有没有更好的解决方案,比如循环之类的?
  • 所有代码都应该贴在这里;编辑您的帖子。没有人喜欢去 youtube 或 facebook 或 imgshack 看 code。该代码与您的问题不匹配-您说您想在右键单击时查看该组,该代码显示已选中,这不是一回事。你知道群会在View = Detail时显示,对吧?
  • 这是在Contextmenustripitem的点击事件中。它是相关部分,应确定所选项目所属的组。
  • LV1.SelectedItems 与鼠标所在/所在的项目不同;它们是两种不同的东西。你想用组名做什么?把它变成一个变量?

标签: vb.net listview organization items


【解决方案1】:

获取鼠标所在项目的 LV 组名称:

Private thisGroupName As String = ""

Private Sub MouseDown(sender, e As MouseEventArgs)...
    If e.Button = MouseButtons.Right Then
        thisGroupName = GetLVGroupAt(e.X, e.Y)
    End If
End Sub

Private Function GetLVGroupAt(X As Integer, Y as Integer) As String
    Dim theGrp As String = ""

    Dim ht As ListViewHitTestInfo = myLV.HitTest(X, Y)
    ' the mouse might be down over a NON item area, like a blank "row"
    ' AND if the items does not belong to a Group, 'Group' will
    ' be Nothing:
    If (ht.Item IsNot Nothing) AndAlso (ht.Item IsNot Nothing) Then
         theGrp = ht.Item.Group.Name
    End If

    Return theGrp
End Function

评估组名称留给消费代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-19
    • 1970-01-01
    • 2019-09-07
    • 2014-04-03
    • 1970-01-01
    相关资源
    最近更新 更多