Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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:

 


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.)

  • Administrators
Posted
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 = 
       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

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...