我有一个社交媒体WinForm。我有一个基本上可以在单击按钮时创建新图片框的功能
Public Sub NewPost()
picture as new picturebox
picture.Width = 208
picture.Height = 264
picture.Image = Form2.PictureBox1.Image
picture.Location = New Point(258, 60)
End Sub
事实是,它仅生成1个新的图片框,因为每次我要添加图片框以及每次必须有一个新名称时都必须创建一个新变量。我知道我的问题有点令人困惑,但帮助会很好,谢谢
如果要为动态PictureBox捕获事件,则必须放弃该WithEvents
模型并转而使用AddHandler。
这是一个简单的示例,单击它时将显示PictureBox的名称。请注意,我没有设置a,Location
因为它们已添加到FlowLayoutPanel中,该FlowLayoutPanel会为您处理放置:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
NewPost()
End Sub
Public Sub NewPost()
Dim picture As New PictureBox
picture.Width = 208
picture.Height = 264
picture.BorderStyle = BorderStyle.FixedSingle
' ...etc...
Dim index As Integer = FlowLayoutPanel1.Controls.Count + 1
picture.Name = "pb" & index
AddHandler picture.Click, AddressOf picture_Click
FlowLayoutPanel1.Controls.Add(picture)
End Sub
Private Sub picture_Click(sender As Object, e As EventArgs)
Dim pb As PictureBox = DirectCast(sender, PictureBox)
Debug.Print(pb.Name)
End Sub
End Class
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句