I ran into a little problem. With this piece of code, I create a new form for every ".jpg" file in a folder:
Dim d As DirectoryInfo = New DirectoryInfo("path to folder")
For Each bild As FileInfo In d.GetFiles("*.jpg")
Dim p As New Form
p.Show()
Next
Now I have some trouble handling the forms (p) event. I know how to handle forms that were created in code but this doesn't work if more then one form were created. Just the last one gets the events.
AddHandler p.Click, AddressOf p_click()
In short: How can every form get the event (p_click) when multiple forms are created in code?
Yes I'm calling AddHandler right before p.Show()
That's correct then...you cast the sender
parameter in the handler to get a reference to the Form:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim d As DirectoryInfo = New DirectoryInfo(My.Computer.FileSystem.SpecialDirectories.MyPictures)
For Each bild As FileInfo In d.GetFiles("*.jpg")
Dim p As New Form
AddHandler p.Click, AddressOf p_Click
p.BackgroundImage = Image.FromFile(bild.FullName)
p.BackgroundImageLayout = ImageLayout.Zoom
p.Show()
Next
End Sub
Private Sub p_Click(sender As Object, e As EventArgs)
Dim frm As Form = DirectCast(sender, Form)
' ... do something with "frm" ...
frm.Close()
End Sub