0

自上周四以来,我一直在寻找我的问题的答案。vb.net 已经回答了很多关于我完全相同的问题的答案。但是,我正在使用 Visual Basic 2008,这两种语言似乎有我难以理解的差异。所以这是我的问题。

我需要创建几个图片框,并且我已经按照几个站点的推荐动态创建了它们。那部分工作正常。当我想点击它们时,问题就开始了。我读得足够明白,这并不是因为我创建了对象,而是创建了附加到它们的方法。然后我创建方法。仍然没有问题,除非我运行代码时每个按钮都执行相同的操作,因为它们都附加到相同的方法。我找到了一个解决方案:我需要用方法传递一个参数来告诉我正在点击的图片框,但是因为我使用的是 addressof 我不能。我知道很少有网站讨论过相同的问题并使用 lamda 表达式解决了它。如果有人能给我我应该使用的代码,我将非常感激。

这是我的代码:

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

谢谢大家的帮助,对不起我的语言它不是我的第一语言。

4

2 回答 2

1

为什么不改成couleur_click将 sender 作为参数呢?然后您将知道点击的来源,您可以从中找到数组PictureBox中的索引:couleur

' ...
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
于 2013-05-14T05:56:14.380 回答
0

设置每个 PictureBox 的 tag 属性,然后在 click 事件处理程序中,您可以对 tag 进行选择案例。

您不能将参数添加到内置事件处理程序。

于 2013-05-08T01:30:25.487 回答