Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Event handlers for List(Of ...) 1

Status
Not open for further replies.

SykoStokkrTim

Programmer
Jul 19, 2002
20
US
Hello! I'm sure (or at least I'm hoping) this will be simple but I just can't find anything on the subject. I am dynamically creating a grid of PictureBoxes and placing them into a List(Of PictureBox) structure or whatever it is considered. Well, creating the controls works just fine, but I simply cannot figure out how to make an event handler for what would ostensibly be PictureBox.Click, for all of the PictureBoxes. I would be very grateful for your help.

-SSTim
 
After creating each picture box, add a handler to the picture box's Click event. You'll need to provide a subroutine whose signature matches that needed by the Click event delegate. Here's a code example illustrating AddHandler and a way to determine which picture box actually was clicked from within a common event handler.

Code:
Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim pbx As New PictureBox
        pbx.Location = New Point(10, 10)
        pbx.BackColor = Color.Red
        pbx.Tag = "Red picture box"
        [b]AddHandler pbx.Click, AddressOf PictureBoxClickHandler[/b]
        Me.Controls.Add(pbx)

        pbx = New PictureBox
        pbx.Location = New Point(60, 60)
        pbx.BackColor = Color.Green
        pbx.Tag = "Green picture box"
        [b]AddHandler pbx.Click, AddressOf PictureBoxClickHandler[/b]
        Me.Controls.Add(pbx)

        pbx = New PictureBox
        pbx.Location = New Point(110, 110)
        pbx.BackColor = Color.Blue
        pbx.Tag = "Blue picture box"
        [b]AddHandler pbx.Click, AddressOf PictureBoxClickHandler[/b]
        Me.Controls.Add(pbx)
    End Sub

    Private Sub PictureBoxClickHandler(sender As System.Object, e As System.EventArgs)
        Dim pbx = CType(sender, PictureBox)
        Me.Text = pbx.Tag.ToString()
    End Sub

End Class
 
You are amazing. Thank you very much! This is exactly what I needed!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top