我在這個論壇和其他網站上搜索過無數個樣本,但我仍然堅持這個問題; 我想爲動態創建的PictureBox-es添加一個Click Handler,並在它上面添加一個參數,這樣我就知道哪個圖片框被點擊了)。如何將事件處理程序添加到VB.NET中的動態創建的控件?
這裏是我當前的代碼:
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
所以我試圖用AddHandler的第(I)。點擊,AddressOf testini但顯然,這並不讓我打電話給「testini」用參數來識別哪個圖片框被點擊。
有人能指出我正確的方向還是給一些建議?不勝感激。
你已經得到了標識圖片框的參數,它是* sender *參數。 –
我最近經常看到這個「發件人」的東西,但我沒有得到如何在我的情況下使用它。 'code' Public Sub testini(ByVal sender,ByVal EventArgs) '如何在此處使用它? End Sub –
'sender'是被點擊的'PictureBox'。在你的'testini'方法中,你可以說'Dim pbox As PictureBox = DirectCast(sender,PictureBox)'然後用用戶點擊的圖片框做事情(改變圖片,不管)。 – prprcupofcoffee