Search code examples
.netvb.netlistviewlost-focus

Must Lost Focus When no Item is Selected in a List View


I believe this is quite easy but I'm currently having problems with this right now. I have a list view and every time I click on an item, I'm storing its name on a string variable. What happens is that when I click an item on the list view and then click inside the list view again without selecting any item, it doesn't lost focus and stores the name of the previous selected item on the string variable. What I want to happen is that every time I click an item and then click inside or outside the list view without selecting any item, it must lost focus and not store the name of the item on a string variable.

Private Sub lvReceivedFiles_MouseClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles lvReceivedFiles.MouseClick

    If e.Button = Windows.Forms.MouseButtons.Left Then

        If lvRecievedFiles.FocusedItem.Selected = True

              lvReceivedFiles.FullRowSelect = True

              'When an item is clicked in the list view, store its name.
              fName = "\" & Path.GetFileName(lvReceivedFiles.FocusedItem.Text)

        Else

              'This part here doesn't make any sense because it doesn't execute
              'every time I click in the list view without selecting any item.
              'ListView.FocusedItem.Selected is always true.

        End If

    End If

End Sub

Solution

  • You can try in MouseUp event.

    Private Sub lvlcheckin_MouseUp(sender As Object, e As MouseEventArgs) Handles lvlcheckin.MouseUp
        If e.Button = Windows.Forms.MouseButtons.Left Then
    
            If lvlcheckin.FocusedItem.Selected = True Then
                lvlcheckin.FullRowSelect = True
            Else
                lvlcheckin.FullRowSelect = False
            End If
        End If
    End Sub