Bensqu90
Bensqu90

Reputation: 11

Establishing a link between a picture box and label for a drag and drop system

This is an image to give a better idea of what's going on:

photo

(I have attempted to resolve this myself but I'm unsure even on the terms to search.)

I have a program where I am adding nodes (represented by a picturebox) onto a canvas, which have a label underneath to identify them. When a button is pressed a picture box and a label are created and I can move them around the canvas together.

My problem arises after I have placed many nodes onto the canvas: if I move previously placed picture boxes, the most recently created label will then be assigned to the older picture box.

Private Sub PictureMouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
   If e.Button = Windows.Forms.MouseButtons.Left Then
       sender.top = MousePosition.Y - coord.Y
       sender.left = MousePosition.X - coord.X
       lblLocation.Left = MousePosition.X - coord.X
       lblLocation.Top = MousePosition.Y - coord.Y + 50
   End If
End Sub

I believe the problem does not occur for the picture boxes because I am using the 'sender' however I don't think I can use this for the label as they are not the control calling the event.

Is there any way to 'link' the label to the text box so they move together? Thanks

Upvotes: 0

Views: 114

Answers (1)

Idle_Mind
Idle_Mind

Reputation: 39132

Agreed, a UserControl would make more sense as they are both supposed to always be together as a "unit".

The .Tag property suggestion would be a quick, or temporary solution. You'd just assign the label to the Tag property when you create them both:

Dim pb As New PictureBox
Dim lbl As New Label
' ... other initialization code ...
pb.Tag = lbl

Then you can retrieve it in the MouseMove event:

Dim pb As PictureBox = DirectCast(sender, PictureBox)
Dim lbl As Label = DirectCast(pb.Tag, Label)

' ... do something with "pb" and "lbl" ...

Upvotes: 1

Related Questions