adding event handlers

Ezguy

Freshman
Joined
Jul 24, 2003
Messages
41
hi,

I am creating picture boxes dynamically using the following code.

----------------------------
Dim picture As New PictureBox()
picture.Size = New Size(100, 100)
picture.Left = 100
Panel1.Controls.Add(picture)
----------------------------

For example I am creating 10 instances of the picture box control I want to know how to find when a picture box is clicked

can anyone tell me how to a event handlers to it.

thanks
 
here's an example of making 3 basic picturebox's and handling them :
Visual Basic:
Private WithEvents picBox As PictureBox


    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim pics(2) As PictureBox
        Dim x As Integer
        Dim i As Integer = 20
        For x = 0 To UBound(pics)
            pics(x) = New PictureBox()
            With pics(x)
                .Tag = x
                .Location = New System.Drawing.Point(400, i)
                .BackColor = Color.Red
                .Visible = True
                Controls.Add(pics(x))
                AddHandler pics(x).Click, AddressOf picBox_Click
            End With
            i += 60
        Next
    End Sub

    Private Sub picBox_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles picBox.Click
        MessageBox.Show(sender.tag)
    End Sub
 
Back
Top