Getting Control Properties from EventHandler

kentheprogger

Newcomer
Joined
Apr 2, 2006
Messages
24
I am designing a program that grabs PictureBox information from an XML file and puts them on the form. I want to have an EventHandler that handles every PictureBox I create, which I have done. The problem is that in order to identify which PictureBox has been clicked I need to get the name.

I would think that this would be extremely easy but I have not found a way to do it.

Here is some sample code:

Code:
 Public Sub createButtons()

        Dim i As Integer

        For i = 0 To UBound(buttons)
            If buttons(i).Name <> "" Then
                PBWEB = New PictureBox
                With PBWEB
                    .Size = buttons(i).Size
                    .Location = buttons(i).Location
                    .Name = buttons(i).Name
                    .Image = Image.FromFile(Application.StartupPath & buttons(i).upPath)
                End With
                AddHandler PBWEB.MouseDown, AddressOf buttons_MouseDown
                Me.Controls.Add(PBWEB)
            End If
        Next

End Sub

Public Sub buttons_MouseDown(ByVal sender As Object, ByVal e As System.windows.Forms.MouseEventArgs) Handles PBWEB.MouseDown
        Dim i As Integer

        For i = 0 To UBound(buttons)
            'If buttons(i).Name = (?Button's Name that was Pushed?) Then
            '   Change Graphic to Down Path
            'End If
        Next
End Sub

I have a lot of buttons and I really would not like to create hundreds of different handlers. I also would not like to use Control Arrays, as VB.Net has stepped away from it but not completely.

Any help would be greatly appreciated.

(EDIT: PBWEB.Name only provides the name of the last created control.)
 
Visual Basic:
Public Sub buttons_MouseDown(ByVal sender As Object, ByVal e As System.windows.Forms.MouseEventArgs) Handles PBWEB.MouseDown
        Dim i As Integer

dim b as button = DirectCast(sender, Button)

'b will be the button that was pressed,
'b.Name is the name of the button that was pressed etc.

'instead of looping you could just do
b.Image = <whatever>
        For i = 0 To UBound(buttons)
            'If buttons(i).Name = (?Button's Name that was Pushed?) Then
            '   Change Graphic to Down Path
            'End If
        Next
End Sub
 
Back
Top