Stop Right Click Selecting Items In ListView

80 Views Asked by At

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.

2

There are 2 best solutions below

0
Mike Couples On BEST ANSWER

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
3
Albert D. Kallal On

Is this for desktop (winforms), or is this for web based, say webforms?

For desktop, if I drop in a listbox, and a Picture box.

And then this code behind:

Private Sub ListBoxPicture_Load(sender As Object, e As EventArgs) Handles Me.Load

    Dim sPicturesDir = "c:\test\balls"
    Dim f As New DirectoryInfo(sPicturesDir)
    Dim fFiles = f.GetFiles

    ListBox1.DataSource = fFiles

End Sub

Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged

    Dim fInfo As FileInfo = ListBox1.SelectedItem
    PictureBox1.Image = Image.FromFile(fInfo.FullName)

End Sub

The result is now this:

enter image description here

And in above, when I try to right click on the listbox, it does not select.

And I can add a mouse down event to the Listbox.

So, say I drop in a ContentMenu strip, and thus add this code to the mouse down event of the listbox:

Private Sub ListBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles ListBox1.MouseDown

    If e.Button = MouseButtons.Right Then

        ContextMenuStrip1.Show(Cursor.Position.X, Cursor.Position.Y)

    End If

End Sub

The result again is that a right click on the ListBox does not select the item, and we get this effect:

enter image description here

Once again, I don't see any effect or case in which a simple right click results in the Listbox being selected when you right click.

This is why your question should include at least "some" code since I'm unable to reproduce your issue here.

If we build this as web based?

Then as web forms, I have this markup:

        <asp:ListBox ID="ListBox1" runat="server" style="float:left"
            DataTextField="Name" 
            Height="407px" Width="242px"
            AutoPostBack="true"
            OnSelectedIndexChanged="ListBox1_SelectedIndexChanged">
        </asp:ListBox>

        <asp:Image ID="Image1" runat="server" style="float:left;margin-left:40px" 
            Height="332px" Width="348px" />

And code behind? Well, we will assume that some pictures folder exists in the web site (project).

Hence this code behind:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If Not IsPostBack Then
        LoadData()
    End If

End Sub

Sub LoadData()

    Dim sPicturesDir = Server.MapPath("~/Content/Balls")
    Dim f As New DirectoryInfo(sPicturesDir)
    Dim fFiles = f.GetFiles

    ListBox1.DataSource = fFiles
    ListBox1.DataBind()

End Sub

Protected Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs)

    Dim sFile As String = $"~/Content/Balls/" & ListBox1.SelectedItem.Text

    Image1.ImageUrl = sFile

End Sub

And the result is this, and note again how right click does not select the item, but shows the browser right click context menu.

enter image description here

So, desktop, or web based, I don't see the effect of a right click selecting the listbox item.

However, hopefully the above code examples gives you some working code.