Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
TitleDisplay popup menus when the user right clicks over a ListView on or off of an item
KeywordsListView, popup, context menu
CategoriesControls, Software Engineering
 
In the MouseDown event handler, use the HitTest method to see if the mouse is over an item. Select or deselact the ListView control's currently selected item as appropriate.

In the MouseUp event handler, use HitTest to see what item is under the mouse and display an appropriate popup menu.

 
' Select the item right clicked.
Private Sub ListView1_MouseDown(Button As Integer, Shift As _
    Integer, x As Single, y As Single)
Dim item As ListItem

    If Button = vbRightButton Then
        Set item = ListView1.HitTest(x, y)
        If item Is Nothing Then
            Set item = ListView1.SelectedItem
            If Not item Is Nothing Then
                item.Selected = False
            End If
        Else
            Set ListView1.SelectedItem = item
        End If
    End If
End Sub

' Display the popup menu.
Private Sub ListView1_MouseUp(Button As Integer, Shift As _
    Integer, x As Single, y As Single)
Dim item As ListItem

    If Button = vbRightButton Then
        Set item = ListView1.HitTest(x, y)
        If item Is Nothing Then
            PopupMenu mnuPopupOffItem
        Else
            mnuPopupOnItemItemSelected.Caption = item.Text
            PopupMenu mnuPopupOnItem
        End If
    End If
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated