简体   繁体   中英

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

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).

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.

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

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'

For example, you could set the Name property

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM