I have a tablelayoutpanel set to 1 row that is loaded with a picture box in each colum. How can I tell when one of the picture boxes has been clicked on?
Printable View
I have a tablelayoutpanel set to 1 row that is loaded with a picture box in each colum. How can I tell when one of the picture boxes has been clicked on?
the pictureboxes are the same in a tablelayoutpanel as they are on a form, so you can use the picturebox_click event
Ok that may help some but these picture boxes are created at runtime so how would I know how to build a click event then?
you need to use addhandler to specify a handler.
try this. it adds a picturebox to your form each time you click the button.
pictureboxes_click handles the click event of your dynamic pictureboxes.
vb Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Static index As Integer = 0 Dim pb As New PictureBox pb.BackColor = Color.Red pb.Left = pb.Width * index pb.Tag = index index += 1 Me.Controls.Add(pb) AddHandler pb.Click, AddressOf pictureboxes_click End Sub Private Sub pictureboxes_click(ByVal sender As Object, ByVal e As System.EventArgs) MsgBox(DirectCast(sender, PictureBox).Tag) End Sub
great example and gave me an idea. This is what i tried:
But i get an error saying:Code:Private Sub pbselect()
MsgBox("It worked George")
End Sub
Private Sub getpics()
Debug.WriteLine(totalpic)
For count As Integer = 0 To totalpic - 1
pb(count) = New PictureBox
Controls.Add(pb(count))
With pb(count)
.Image = pics(count)
.Width = 100
.Height = 75
.Visible = True
.SizeMode = PictureBoxSizeMode.StretchImage
AddHandler .Click, AddressOf pbselect
End With
Next
End Sub
does not have the same signiture as deligated
you should have a handler sub:
vb Code:
Private Sub pbselect(ByVal sender As Object, ByVal e As System.EventArgs) 'your code end sub
That worked GREAT thanks a bunch :)