Search code examples
vb.netwinformslistview

Stop Right Click Selecting Items In ListView


I'm new to coding so please be gentle with me. I've tried researching the topic but can not seem to find an answer that will put me onto the right path.

I have a program that has a ListView and in the ListView each item holds a tag for a file path to an image. when I select each item I read the tag and then display the image using the image path. The problem I have is that when I right click on an item it selects the item. I would like to only allow right mouse click for a context menu strip option & have the left mouse click be the selector of item(s).

I've tried quite a few things but I am a bit puzzled on how to implement this?

Here is my code for selecting the new image.

Public Sub LoadSelectedImageFromList()
    If ScanBtnPressed = False Then
        If ImagesListViewBx.SelectedItems.Count > 0 Then
            Dim SelectedFilePath As String = ImagesListViewBx.SelectedItems(0).Tag.ToString().Trim
            Dim OpenFileName As New ImGearLocalFile(SelectedFilePath)

            Using fileContent As FileStream = OpenFileName.OpenToRead()
                Dim page As ImGearPage = ImGearFileFormats.LoadPage(fileContent, 0)
                MainImageView.Page = page
                ' Reset Display settings
                MainImageView.Display = New ImGearPageDisplay(MainImageView.Page)
                MainImageView.Update()
            End Using
            ImageLoaded = True
            ImagesListViewBx.SelectedItems(0).Selected = True
            ImagesListViewBx.Focus()
        End If
    End If

End Sub


Public Sub ImagesListViewBx_ItemSelectionChanged(sender As Object, e As ListViewItemSelectionChangedEventArgs) Handles ImagesListViewBx.ItemSelectionChanged
    ' nScreenTracer Is to Keep Item Selected If User Clicks On Blank Part Of ListView.
    ' If e.IsSelected Then nScreenTracer = e.Item.Index

    ' Read Selected Item Tag To Get Destination Of File So We Can Then Use It To Load Image To Screen.

    LoadSelectedImageFromList()


End Sub

I have tried telling the mouse up/down & click events what button is being pressed by e.buttons and a couple of other ways which i have deleted now so i cant give you that code but nothing i have tried has worked.


Solution

  • I have found a way to achieve what I wanted. This does however show a reselection of the current selected item in the list view but I could not find another way around it.

    It also stops the items from deselecting when clicking on a blank part of the list view.

    If there is a better or more cleaner way of implementing this then please let me know.

    ' List View.
    Private nScreenTracer As Integer
    Private nSelectedScreen As Integer
    Private rightClicked As Boolean = False
    Private lviListIndex As Integer() = Nothing
    
    
    
    Public Sub ImagesListViewBx_ItemSelectionChanged(sender As Object, e As ListViewItemSelectionChangedEventArgs) Handles ImagesListViewBx.ItemSelectionChanged
        ' nScreenTracer Is to Keep Item Selected If User Clicks On Blank Part Of ListView.
        If e.IsSelected Then nScreenTracer = e.Item.Index
    
        If rightClicked Then
            ImagesListViewBx.SelectedIndices.Clear()
        End If
        LoadSelectedImageFromList()
    
    End Sub
    
    Private Sub ImagesListViewBx_MouseDown(sender As Object, e As MouseEventArgs) Handles ImagesListViewBx.MouseDown
        ScanBtnPressed = False
    
        If e.Button = System.Windows.Forms.MouseButtons.Right Then
            rightClicked = True
            lviListIndex = New Integer(ImagesListViewBx.SelectedItems.Count - 1) {}
            ImagesListViewBx.SelectedIndices.CopyTo(lviListIndex, 0)
        Else
            rightClicked = False
        End If
    
        If e.Button = MouseButtons.Left Then
            If ImagesListViewBx.Items.Count > 0 Then
                nScreenTracer = -1
            End If
        End If
    End Sub
    
    Public Sub ImagesListViewBx_MouseUp(sender As Object, e As MouseEventArgs) Handles ImagesListViewBx.MouseUp
    
        If rightClicked Then
            ContextMenuStrip = LVContextMenuStrip
            RemoveHandler ImagesListViewBx.ItemSelectionChanged, (AddressOf ImagesListViewBx_ItemSelectionChanged)
            If lviListIndex IsNot Nothing Then
    
                For Each index As Integer In lviListIndex
                    ImagesListViewBx.SelectedIndices.Add(index)
                Next
            End If
            lviListIndex = Nothing
            AddHandler ImagesListViewBx.ItemSelectionChanged, (AddressOf ImagesListViewBx_ItemSelectionChanged)
        End If
    
        rightClicked = False
    
        If e.Button = MouseButtons.Left Then
            If nScreenTracer = -1 Then
                ImagesListViewBx.SelectedIndices.Add(nSelectedScreen)
            Else
                nSelectedScreen = nScreenTracer
            End If
        End If
     
    End Sub