|
-
Jun 20th, 2012, 02:01 PM
#1
Thread Starter
Addicted Member
When Controls overlap
Alright my goal is to test to see if the user has dragged a label over a button. I have add event handlers for MouseMove and ButtonEnter, the trick is that i dont want the buttonEnter sub to fire unless the user is actually dragging a label.
vb Code:
Dim dragging As Boolean = False
Public Sub Label_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Static lOffset As Point
lbl = DirectCast(sender, Label)
If e.Button = MouseButtons.None Then
lOffset = New Point(e.Location)
dragging = False
ElseIf e.Button = MouseButtons.Left Then
lbl.Location = lbl.Location + e.Location - lOffset
dragging = True
End If
End Sub
Private Sub Button_MouseEnter(sender As System.Object, e As System.EventArgs)
If dragging = True Then
MsgBox("This is a Test")
End If
End Sub
I thought would work but because the user is dragging the label the mouse never actually enters the button field.
I also tried doing lbl.location = button.location... but unless the label and the buttons are the exactly the same size in exactly the same location then the event wont trigger.
Is there an easy solution to this?
-
Jun 20th, 2012, 03:29 PM
#2
Re: When Controls overlap
Is this drag and drop dragging, or are you doing your own? Drag and Drop dragging would be if you set AllowDrop to true for both controls. I ask because you get different events in each case.
Take a look at getting a Rectangle for each control (they have a couple). Rectangles have some method such as Intersects that will tell you if one is in the area of the other.
My usual boring signature: Nothing
 
-
Jun 20th, 2012, 03:33 PM
#3
Re: When Controls overlap
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
Last edited by Inferrd; Jun 20th, 2012 at 03:37 PM.
-
Jun 20th, 2012, 04:56 PM
#4
Thread Starter
Addicted Member
Re: When Controls overlap
You guys are good, I was missing the "bounds.IntersectsWith." I knew you guys would know : )
This is the code that Im using:
vb Code:
For Each but As Button In Me.Controls.OfType(Of Button)()
If lbl.Bounds.IntersectsWith(but.Bounds) Then
MsgBox("This is a Test")
End If
Next
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|