简体   繁体   English

如何为以编程方式创建的PictureBox Array创建事件处理程序?

[英]How do I create an event handler for a programmatically created PictureBox Array?

I have created a two-dimensional array of PictureBoxes, and I want to add a DragDrop event to all the elements of the array. 我创建了一个PictureBoxes的二维数组,并且想向该数组的所有元素添加一个DragDrop事件。

 For x As Integer = 1 To 16
        For y As Integer = 1 To 4
            p(x, y) = New PictureBox()
            p(x, y).Image = My.Resources.Kästchen
            p(x, y).Location = New Point(pMain.Left + x * 48, pMain.Top + y * 48)
            p(x, y).Size = New Size(48, 48)
            p(x, y).Name =  "p"+str(x)+str(y)
            AddHandler p(x, y).DragDrop, AddressOf p(x,y)_DragDrop

            p(x, y).Visible = True
            Me.Controls.Add(p(x, y))


        Next
    Next

I know that there's a similar answer here , but I wasn't able to adapt it to arrays. 我知道,有一个类似的答案在这里 ,但我没能使其适应阵列。 How do I add the DragDrop Event for all the PictureBoxes, which are created during runtime? 如何为在运行时创建的所有PictureBox添加DragDrop事件?

You can't have a sub named p(x,y)_DragDrop . 您不能有一个名为p(x,y)_DragDrop的子p(x,y)_DragDrop You need to create a sub with the signature (sender As Object, e As DragEventArgs) , and use sender to identify the picturebox. 您需要创建一个带有签名的子(sender As Object, e As DragEventArgs) ,并使用sender来标识图片框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM