我遇到了一个小问题。通过这段代码,我为每个" .jpg"创建了一个 new 表单。文件夹中的文件:
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
现在我在处理表单(p)事件时遇到了一些麻烦。我知道如何处理在代码中创建的表单,但如果创建了多个表单,则这不起作用。只是最后一个得到了事件。
AddHandler p.Click, AddressOf p_click()
简而言之:在代码中创建多个表单时,每个表单如何获取事件(p_click)?
答案 0 :(得分:2)
是的,我在p.Show()
之前调用AddHandler
那是正确的...然后你在处理程序中转换sender
参数来获取对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