0

大家好,我在 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

结束类

4

1 回答 1

1
frm.FlowLayoutPanel1.Controls.Add(pic)

一个控件只能有一个父级。因此,将其移至新表单会将从 FlowPanel 中删除。如果你想要一个副本,那么你必须创建一个新的 PictureBox:

Dim pic As PictureBox = DirectCast(sender, PictureBox)
Dim newpic As PictureBox = new PictureBox()
With newpic
    .Size = New System.Drawing.Size(500, 500)
    .SizeMode = PictureBoxSizeMode.Zoom
    .Image = pic.Image
End With
Dim frm As New Form2
frm.FlowLayoutPanel1.Controls.Add(newpic)
frm.ShowDialog()
于 2013-06-19T23:35:33.523 回答