我正在使用flowlayout面板在寡妇窗体上动态创建图片框。我的实际问题是,当用户使用点击事件点击流布局面板中的项目时,我希望在图片框中看到特定的图像。
我已经尝试了一些代码,但是每次我都失败了..请帮助。请告诉我我丢失的代码和一些细节,以便我能理解它。
Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
' Style Explorer - Thubnail Viewer
' FlowLayoutPanel1.SuspendLayout()
Dim OpenFileDialog1 As New OpenFileDialog
OpenFileDialog1.Filter = "Images (*.BMP;*.JPG;*.GIF,*.PNG,*.TIFF)|*.BMP;*.JPG;*.GIF;*.PNG;*.TIFF|" + "All files (*.*)|*.*"
OpenFileDialog1.Multiselect = True
OpenFileDialog1.Title = "Select Photos"
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
For Each file As String In OpenFileDialog1.FileNames
Dim imageControl As New PictureBox()
imageControl.Name = "pic" & i.ToString()
imageControl.Height = 100
imageControl.Width = 100
Dim myCallback As New Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback)
Dim myBitmap As New Bitmap(file)
Dim myThumbnail As Image = myBitmap.GetThumbnailImage(96, 96, myCallback, IntPtr.Zero)
imageControl.Image = myThumbnail
FlowLayoutPanel1.Controls.Add(imageControl)
Next
End If
End Sub
Public Function ThumbnailCallback() As Boolean
Return False
End Function
答案 0 :(得分:0)
在将图片框添加到FlowLayoutPanel的循环中,您可以将文件名添加到dinamically创建的PictureBox的Tag属性中,并为click事件添加事件处理程序
For Each file As String In OpenFileDialog1.FileNames
Dim imageControl As New PictureBox()
imageControl.Name = "pic" & i.ToString()
imageControl.Height = 100
imageControl.Width = 100
' Save the file from which you create the thumbnail
imageControl.Tag = file
' Set an event handler for the clickevent on the thumbnail
AddHandler imageControl.Click, AddressOf picClicked
.....
Next
现在编写响应click事件的事件处理程序
Private Sub picClicked(sender As Object, e As EventArgs)
Dim pic = DirectCast(sender, System.Windows.Forms.Control)
' The name of the file is in the Tag property....
Console.WriteLine(pic.Tag.ToString())
' Now its your job to display the image knowing the filename
End Sub