0

I have searched for an answer to my question since last thursday. A lot of answers about my exact same question have been answerd in vb.net. However, I am working on Visual Basic 2008 and those two language seems to have differences that are for me difficult to understand. So here is my issue.

I need to create several picture box and I have created them dynamicly as several sites recommanded. That part works fine. Issue begins when I want to click on them. I read enough to understant that it is not because I have created the object that I have created the method attached to them. Then I create the method. Still no problem except when I am running the code each button does the same thing because they are all attached to the same method. I came to a solution: I need to transfer with the method an argument to tell wich Picturebox I am clicking on, but because I am using addressof I can't. I know few sites that have talked about the same issues and solved it using a lamda expression. If someone could give me the code I should use I would be really thankful.

Here is my code:

For i = 0 To 7
     'couleur is the name I give to my picturebox object and objet () is the sub in   which    I  created my object
    couleur(i) = objet()
Next

For x = 0 To 7
   ' initiasation of location, etc.
Next


   '     This is the issue !!! I do not know how to say this line into vb8
   ' I want to pass in argument  X to know on which object I have cliked on and then use a seled case to make separated command afterward.  
For x = 0 To 7
      AddHandler couleur(i).Click, Function(senderobj, args) couleur_click(x)
Next


End Sub

Sub couleur_click(ByVal i As Integer)

' select case doing seperated things depending on the x receive in argument

End Sub

Thank all of you for help, sorry for my language it is not my first language.

Simon MᶜKenzie
  • 8,344
  • 13
  • 50
  • 77

2 Answers2

1

Why don't you change couleur_click to take the sender as a parameter? You will then know the source of the click, from which you can find the index of the PictureBox in your couleur array:

' ...
For x = 0 To 7
      AddHandler couleur(i).Click, AddressOf couleur_click
Next
' ...

Sub couleur_click(sender As Object, e As EventArgs)
   Dim pictureBoxSource As PictureBox = sender

   ' Find the index of the source in the base collection
   Dim index = Array.IndexOf(couleur, pictureBoxSource)

    Select Case index
        ' ...
    End Select
End Sub
Simon MᶜKenzie
  • 8,344
  • 13
  • 50
  • 77
0

Set the tag property of each PictureBox, then in the click event handler you can do a select case on the tag.

You can't add parameters to the built in event handlers.

Paul Anderson
  • 1,120
  • 3
  • 11
  • 22
  • I am novice and the researshes I just made did now allow me to understant how I must use tag to solve my problem. Could you explain what tag is and how I must use it in my situation ? thank you for your help – Pascal Levesque May 08 '13 at 01:54
  • 1
    @PascalLevesque, he means that the `PictureBox` class has a [`Tag`](http://msdn.microsoft.com/en-us/library/system.windows.forms.control.tag.aspx) property, in which you could place your index integer. Your event handler could then extract the integer from the tag, assuming you have access to the `sender`, as in my example. – Simon MᶜKenzie May 14 '13 at 06:02