0

I have created a two-dimensional array of PictureBoxes, and I want to add a DragDrop event to all the elements of the array.

 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?

1
  • Since the number of controls is hardcoded, why not put them on the form in the designer? You can still store a reference to them in an array for looping. Note that your app is likely leaking and array indices start at 0. Please read How to Ask and take the tour Commented Oct 12, 2017 at 15:29

1 Answer 1

1

You can't have a sub named 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.

Sign up to request clarification or add additional context in comments.

3 Comments

So I have to create a Sub for each picture box?
I came up with this, but it shows the error: "." expected Private Sub p11(p11 As Object, e As System.Windows.Forms.DragEventArgs) Handles p(1,1).DragDrop End Sub
@Cyrill : No, just create one event handler for all picture boxes: Private Sub PictureBoxes_DragDrop(sender As Object, e As DragEventArgs) (notice that there's no Handles ... on the end), then: AddHandler p(x, y).DragDrop, AddressOf PictureBoxes_DragDrop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.