Question Double Click Listview selected item and show in picturebox

Dwhite

Member
Joined
Nov 10, 2020
Messages
17
Programming Experience
1-3
Evening all,

Hope all are well.

As the title says im having difficulty with selecting listview item and displaying in a picturebox. Basically i want to doubleclick an image in a listview and the item will show in a picturebox. If you are able to provide assistance please let me know. i keep receiving "System.ArgumentException: 'Parameter is not valid.'" I have added my codes below.
VB.NET:
     Private Sub ListView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.DoubleClick
        Dim list As New List(Of Bitmap)
        Dim gameObject As New Dictionary(Of Integer, Bitmap)

        ListView1.Items.Add(0)
        ListView1.Items.Add(1)
        ListView1.Items.Add(2)

        gameObject.Add(1, New Bitmap("Picture1.jpg"))
        gameObject.Add(2, New Bitmap("Picture2.jpg"))
        gameObject.Add(3, New Bitmap("Picture3.jpg"))
        Dim i As Integer
        i = CInt(ListView1.SelectedItems(0).Text.ToString)
        PictureBox1.Image = gameObject(i)
    End Sub
 
Last edited by a moderator:
That code doesn't make much sense as it is. You handle the DoubleClick event and THEN you create the items and the images. Surely you want to create the items and the images when the form loads, so that you've actually got something to double-click and display.

You should start by handling the Load event of the form and create the items and images there. I'd suggest assigning each Bitmap object to the Tag property of the corresponding ListViewItem. You can then easily get the image once you have the item.

You should then handle the MouseDoubleClick event of the ListView. That will give you the coordinates of the mouse so you can then call the HitTest method to determine if it was over an item and which item that was. Once you have the item, get the Image from the Tag property and display it in the PictureBox.
 
That code doesn't make much sense as it is. You handle the DoubleClick event and THEN you create the items and the images. Surely you want to create the items and the images when the form loads, so that you've actually got something to double-click and display.

You should start by handling the Load event of the form and create the items and images there. I'd suggest assigning each Bitmap object to the Tag property of the corresponding ListViewItem. You can then easily get the image once you have the item.

You should then handle the MouseDoubleClick event of the ListView. That will give you the coordinates of the mouse so you can then call the HitTest method to determine if it was over an item and which item that was. Once you have the item, get the Image from the Tag property and display it in the PictureBox.
Sorry. I tried the following and nothing happens. Any suggestions would be much appreciated.
VB.NET:
Private Sub ListView1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseDown
    Dim objDrawingPoint As Drawing.Point
    Dim objListViewItem As ListViewItem

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

    objDrawingPoint = ListView1.PointToClient(Cursor.Position)

    If Not IsNothing(objDrawingPoint) Then
        With objDrawingPoint
            objListViewItem = ListView1.GetItemAt(.X, .Y)
        End With

        If IsNothing(objListViewItem) Then
            PictureBox1.Image = Nothing
        Else
            PictureBox1.Image = ImageList1.Images(objListViewItem.ImageIndex)
        End If
    End If
End Sub
 
Last edited by a moderator:
You say that nothing happens but that's not true. Something happens, but it may just not be what you want or expect. You need to debug your code to find out what actually happens. Set a breakpoint and step through the code. I suspect that I know what the problem is but I'll let you debug first, because you should always do that first, before posting here.
 
Sorry. I tried the following and nothing happens. Any suggestions would be much appreciated.
VB.NET:
Private Sub ListView1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseDown
    Dim objDrawingPoint As Drawing.Point
    Dim objListViewItem As ListViewItem

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

    objDrawingPoint = ListView1.PointToClient(Cursor.Position)

    If Not IsNothing(objDrawingPoint) Then
        With objDrawingPoint
            objListViewItem = ListView1.GetItemAt(.X, .Y)
        End With

        If IsNothing(objListViewItem) Then
            PictureBox1.Image = Nothing
        Else
            PictureBox1.Image = ImageList1.Images(objListViewItem.ImageIndex)
        End If
    End If
End Sub
I got the application in break mode and received the following error:

System.InvalidCastException: 'Unable to cast object of type 'System.EventArgs' to type 'System.Windows.Forms.MouseEventArgs'.'

I changed the handles from mousedown to mouseclick and still receive the same error.
 
I got the application in break mode and received the following error:

System.InvalidCastException: 'Unable to cast object of type 'System.EventArgs' to type 'System.Windows.Forms.MouseEventArgs'.'

I changed the handles from mousedown to mouseclick and still receive the same error.

Also wanted to add the following to show how i populate images in the listview:

Private Sub populate()
Dim imgs As ImageList = New ImageList()
imgs.ImageSize = New Size(75, 75)
Dim itemsfolder As New List(Of ListViewItem)

'listview1.view is needed to show jpeg screenshot in listview********
ListView1.View = View.LargeIcon


Try
For Each fpath In Directory.GetFiles("\\FolderNameWhereImagesAreStored" & TextBox2.Text)
If fpath.Contains(TextBox2.Text) Then
imgs.Images.Add(Image.FromFile(fpath))
itemsfolder.Add(New ListViewItem(Path.GetFileName(fpath)) With {.ImageIndex = imgs.Images.Count - 1})

End If
Next



ListView1.Items.AddRange(itemsfolder.ToArray())

Catch ex As Exception

End Try
ListView1.LargeImageList = imgs
' ListView1.Items.Add("Name", 0)

End Sub
 
Have you finally solved that problem of yours? I do to have the same problem so I was hoping if u could share it
 
I just stumbled across this thread when searching for something else. I am not sure if you found a solution for your problem. Maybe this approach can help. You can get the value of any listview item or subitem by using something like this in the listview MouseDoubleClick event.

In the MouseDoubleClick event:

ViewListviewFieldValue(sender, e, ListView3_Term)

Public Sub ViewListviewFieldValue(sender As Object, e As MouseEventArgs, ByRef lv As ListView)
Try
Dim info As ListViewHitTestInfo = lv.HitTest(e.X, e.Y)
Dim usersSelection As String = String.Empty

usersSelection = info.SubItem.ToString
usersSelection = usersSelection.Substring(18)
usersSelection = usersSelection.Substring(0, usersSelection.Length - 1)

If Not String.IsNullOrEmpty(usersSelection) Then
Dim selectedFieldValue = InputBox("Value:", "Selected Item Value (View Only)", usersSelection)
End If
Catch ex As Exception
'Do nothing
End Try
End Sub

You can do anything you need with the usersSelection string. This is just a simple example.
 
Last edited:

Latest posts

Back
Top