Reputation: 290
hello guys im making this application in VB that loads picture boxes from a file into a flowlayoutpanel and adds a click handler to each pic in order to display them in a bigger size. however when they are clicked are removed from the layoutpanel and i dont want that and dont understand why is happening. this is my code:
Imports System.IO Public Class 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
End Class
Upvotes: 0
Views: 475
Reputation: 942050
frm.FlowLayoutPanel1.Controls.Add(pic)
A control can have only one parent. So moving it to the new form will remove it from FlowPanel. If you want a copy then you'll have to create a new 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()
Upvotes: 1