I'm sure I came across a more straight forward way of doing this, but I can't remember.....
Anyway, the following should do the job. Test if the rectangle occupied by the Label intersects with the rectangle occupied by the Button.
There are a couple of (minor) changes to your original code.
The first code triggers as soon as the Label overlaps the Button, but the Mouse Cursor may be off the Button.
The second code waits until the Mouse Cursor is also over the Button.
NOTE that (in this version) neither will work if the label and the Button have different parents (i.e. they both have to be on the Form itself or on the same Panel/GroupBox etc.)
EDIT note that I put the Handles Label1.MouseMove clause back in. Don't know if you need it. (and Shaggy's typing is getting quicker)
vb.net Code:
Dim dragging As Boolean = False
Public Sub Label_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseMove
Static lOffset As Point
Dim lbl As Label = DirectCast(sender, Label)
If e.Button = MouseButtons.None Then
lOffset = e.Location
dragging = False
ElseIf e.Button = MouseButtons.Left Then
dragging = True
lbl.Location = lbl.Location + New Size(e.Location) - New Size(lOffset)
If dragging AndAlso lbl.Bounds.IntersectsWith(Button1.Bounds) Then
MsgBox("This is a Test")
End If
End If
End Sub
vb.net Code:
Dim dragging As Boolean = False
Public Sub Label_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseMove
Static lOffset As Point
Dim lbl As Label = DirectCast(sender, Label)
If e.Button = MouseButtons.None Then
lOffset = e.Location
dragging = False
ElseIf e.Button = MouseButtons.Left Then
dragging = True
lbl.Location = lbl.Location + New Size(e.Location) - New Size(lOffset)
If dragging AndAlso lbl.Bounds.IntersectsWith(Button1.Bounds) Then
If Button1.ClientRectangle.Contains(Button1.PointToClient(MousePosition)) Then
MsgBox("This is a Test")
End If
End If
End If
End Sub