如何将事件处理程序添加到 VB.NET 中动态创建的控件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13614254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:21:57  来源:igfitidea点击:

How to add event handler to a dynamically created control in VB.NET?

vb.netvisual-studio-2008event-handling

提问by Altin

I have searched and seen countless samples here in this forum and in other sites but I'm still stuck with this problem; I want to add a Click Handler for dynamically created PictureBox-es and pas an argument on it so I know which one of picture boxes was clicked).

我在这个论坛和其他网站上搜索并看到了无数示例,但我仍然被这个问题所困扰;我想为动态创建的 PictureBox-es 添加一个 Click Handler 并在其上传递一个参数,以便我知道点击了哪一个图片框)。

Here is my current code:

这是我当前的代码:

Public Class frmMbarimAbonimi

Private Sub frmMbarimAbonimi_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'TODO: This line of code loads data into the 'FitnessdbDataSet.clients' table. You can move, or remove it, as needed.
    'Me.ClientsTableAdapter.Fill(Me.FitnessdbDataSet.clients)

    '===============
    Dim dt As DataTable = PaPaguar()
    Dim i As Integer = 0

    Dim gr(dt.Rows.Count) As GroupBox
    Dim pp(dt.Rows.Count) As PictureBox
    Dim lb(dt.Rows.Count) As Label


    For Each row As DataRow In dt.Rows

        gr(i) = New GroupBox
        gr(i).Width = 200
        gr(i).Height = 180

        pp(i) = New PictureBox
        pp(i).SizeMode = PictureBoxSizeMode.StretchImage

        lb(i) = New Label



        '-------------------------
        Try
            Using str As Stream = File.OpenRead("C:\Fotot\" + dt.Rows(i).Item("Foto"))
                pp(i).Image = Image.FromStream(str)
            End Using

            lb(i).Text = dt.Rows(i).Item("Emer")

        Catch ex As Exception

            MsgBox("Fotoja nuk mund te ngarkohet, ju lutem realizoheni nje foto tjeter!!!")

        End Try
        '-------------------------
        pp(i).Visible = True
        pp(i).Width = 200
        pp(i).Height = 150

        AddHandler pp(i).Click, AddressOf testini



        gr(i).Controls.Add(pp(i))

        lb(i).Visible = True
        lb(i).Width = 200
        lb(i).Height = 30
        lb(i).Left = pp(i).Left
        lb(i).Top = pp(i).Top + 150
        lb(i).BackColor = Color.WhiteSmoke
        lb(i).BringToFront()
        gr(i).Controls.Add(lb(i))

        flpanel.Controls.Add(gr(i))

        i = i + 1
    Next row
End Sub 
End Class

So I was trying to use AddHandler pp(i).Click, AddressOf testini but obviously this does not allow me to call "testini" with a parameter to identify which picture box was clicked.

所以我试图使用 AddHandler pp(i).Click, AddressOf testini 但显然这不允许我用参数调用“testini”来识别点击了哪个图片框。

Can someone point me in the right direction or give some advice? Greatly appreciated.

有人可以指出我正确的方向或提供一些建议吗?不胜感激。

回答by Steve

You need to add something to your created PictureBox to identify them in the event handler because you can't change the signature of the click event handler adding a 'parameter'

您需要向创建的 PictureBox 添加一些内容以在事件处理程序中识别它们,因为您无法通过添加“参数”来更改单击事件处理程序的签名

For example, you could set the Name property

例如,您可以设置 Name 属性

pp(i) = New GroupBox
pp(i).Name = "PictureBox" + i.ToString

then in the event handler you could recognize your picture box casting the sender object to a picturebox and grabbing the Name property.
Remember, sender is always the control that triggers the event. In your case is always one of your dinamically created PictureBoxes

然后在事件处理程序中,您可以识别您的图片框,将发送者对象投射到图片框并获取 Name 属性。
请记住,发送方始终是触发事件的控件。在您的情况下始终是您动态创建的图片框之一

Private Sub testini(sender As Object, e As System.EventArgs) 
     Dim pb As PictureBox = DirectCast(sender, PictureBox)
     Dim pbIdentity As String = pb.Name
     .....
End Sub