大家好,我在 VB 中制作了这个应用程序,它将文件中的图片框加载到 flowlayoutpanel 中,并为每个图片添加了一个点击处理程序,以便以更大的尺寸显示它们。但是,当它们被点击时,它们会从布局面板中删除,我不希望这样,也不明白为什么会发生这种情况。这是我的代码:
导入 System.IO 公共类 Form1
Private folderPath As String
Private pics() As PictureBox
Private Sub OpenToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles OpenToolStripMenuItem.Click
FolderBrowser.SelectedPath = Directory.GetCurrentDirectory
If FolderBrowser.ShowDialog() = DialogResult.Cancel Then
Return
End If
folderPath = FolderBrowser.SelectedPath()
Dim fileNames As String() = Directory.GetFiles(folderPath)
If fileNames.Length = 0 Then
MessageBox.Show("Unable to find any image files")
Return
End If
Me.Text = folderPath
ReDim pics(fileNames.Length - 1)
For i As Integer = 0 To fileNames.Length - 1
pics(i) = New PictureBox()
With pics(i)
.Size = New System.Drawing.Size(300, 200)
.SizeMode = PictureBoxSizeMode.Zoom
.Image = New Bitmap(fileNames(i))
FlowPanel.Controls.Add(pics(i))
AddHandler pics(i).Click, AddressOf pics_Click
End With
Next
End Sub
Private Sub pics_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim pic As PictureBox = DirectCast(sender, PictureBox)
With pic
.Size = New System.Drawing.Size(500, 500)
.SizeMode = PictureBoxSizeMode.Zoom
RemoveHandler pic.Click, AddressOf pics_Click
End With
Dim frm As New Form2
FlowPanel.Controls.Add(pic)
frm.FlowLayoutPanel1.Controls.Add(pic)
frm.ShowDialog()
End Sub
结束类