I'm adding images in a picturebox to a flowlayout panel. I'm trying to add a click event so that when the image is clicked on in the flowlayout panel, it will open the original image. My pictures are .jpg. Here's what i got so far but seems like it's not working.
For Each pic As FileInfo In New DirectoryInfo("picturepath").GetFiles("file.jpg")
Dim picture As New PictureBox
picture .Height = 113
picture .Width = 145
picture .BorderStyle = BorderStyle.Fixed3D
picture .SizeMode = PictureBoxSizeMode.Zoom
picture .Image = Image.FromFile(fi.FullName)
AddHandler picture.MouseClick, AddressOf pictureBox_MouseClick
flowlayoutpanel.Controls.Add(picture)
Next
Public Sub pictureBox_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
====>>> Not sure what goes here to get the correct path of that image since there could be more than one images.
End Sub
You need to use the "sender" parameter to get a reference to the PictureBox that was clicked:
Public Sub pictureBox_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Dim pb As PictureBox = DirectCast(sender, PictureBox)
' ... now do something with "pb" (and/or "pb.Image") ...
End Sub
With a reference to only the PictureBox (as in my example above), you would only have the image itself referenced. If you want the full path of the file then you'd have to store that information with the PictureBox somehow; using the Tag property is one simple way:
Dim picture As New PictureBox
...
picture.Image = Image.FromFile(fi.FullName)
picture.Tag = fi.Fullname
Now you can retrieve that filename in the click event and do something with it:
Public Sub pictureBox_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Dim pb As PictureBox = DirectCast(sender, PictureBox)
' ... now do something with "pb" (and/or "pb.Image") ...
Dim fileName As String = pb.Tag.ToString()
Process.Start(fileName)
End Sub