我正在将picturebox中的图像添加到流程布局面板中。我正在尝试添加一个点击事件,这样当在流程布局面板中点击图像时,它将打开原始图像。我的照片是.jpg。这是我到目前为止得到的,但似乎不起作用。
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
您需要使用"sender"参数来获得对单击的PictureBox的引用:
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
如果只引用PictureBox(如我上面的示例所示),则只能引用图像本身。如果你想要文件的完整路径,那么你必须以某种方式将信息存储在PictureBox中;使用Tag属性是一种简单的方法:
Dim picture As New PictureBox
...
picture.Image = Image.FromFile(fi.FullName)
picture.Tag = fi.Fullname
现在,您可以在点击事件中检索该文件名,并对其执行操作:
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