添加单击事件以打开单个图片框图像

时间:2016-08-02 19:19:57

标签: vb.net picturebox flowlayout

我将图片框中的图片添加到流程布局面板。我尝试添加点击事件,以便在流程图窗口面板中单击图像时,它将打开原始图像。我的照片是.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

1 个答案:

答案 0 :(得分:0)

您需要使用"发件人"获取对单击的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