Results 1 to 4 of 4

Thread: When Controls overlap

  1. #1
    Addicted Member
    Join Date
    Sep 11
    Location
    Seattle
    Posts
    191

    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:
    1. Dim dragging As Boolean = False
    2.  
    3.    Public Sub Label_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
    4.         Static lOffset As Point
    5.         lbl = DirectCast(sender, Label)
    6.         If e.Button = MouseButtons.None Then
    7.             lOffset = New Point(e.Location)
    8.             dragging = False
    9.         ElseIf e.Button = MouseButtons.Left Then
    10.             lbl.Location = lbl.Location + e.Location - lOffset
    11.             dragging = True
    12.         End If
    13.     End Sub
    14.  
    15.     Private Sub Button_MouseEnter(sender As System.Object, e As System.EventArgs)
    16.         If dragging = True Then
    17.             MsgBox("This is a Test")
    18.         End If
    19.     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?

  2. #2
    Loquacious User Shaggy Hiker's Avatar
    Join Date
    Aug 02
    Location
    Idaho
    Posts
    20,390

    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

  3. #3
    Hyperactive Member
    Join Date
    Jul 11
    Location
    UK
    Posts
    436

    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:
    1. Dim dragging As Boolean = False
    2. Public Sub Label_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseMove
    3.  
    4.     Static lOffset As Point
    5.     Dim lbl As Label = DirectCast(sender, Label)
    6.     If e.Button = MouseButtons.None Then
    7.         lOffset = e.Location
    8.         dragging = False
    9.     ElseIf e.Button = MouseButtons.Left Then
    10.         dragging = True
    11.         lbl.Location = lbl.Location + New Size(e.Location) - New Size(lOffset)
    12.  
    13.         If dragging AndAlso lbl.Bounds.IntersectsWith(Button1.Bounds) Then
    14.             MsgBox("This is a Test")
    15.         End If
    16.  
    17.     End If
    18. End Sub



    vb.net Code:
    1. Dim dragging As Boolean = False
    2. Public Sub Label_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseMove
    3.  
    4.     Static lOffset As Point
    5.     Dim lbl As Label = DirectCast(sender, Label)
    6.     If e.Button = MouseButtons.None Then
    7.         lOffset = e.Location
    8.         dragging = False
    9.     ElseIf e.Button = MouseButtons.Left Then
    10.         dragging = True
    11.         lbl.Location = lbl.Location + New Size(e.Location) - New Size(lOffset)
    12.  
    13.         If dragging AndAlso lbl.Bounds.IntersectsWith(Button1.Bounds) Then
    14.             If Button1.ClientRectangle.Contains(Button1.PointToClient(MousePosition)) Then
    15.                  MsgBox("This is a Test")
    16.             End If
    17.         End If
    18.  
    19.         End If
    20. End Sub
    Last edited by Inferrd; Jun 20th, 2012 at 03:37 PM.

  4. #4
    Addicted Member
    Join Date
    Sep 11
    Location
    Seattle
    Posts
    191

    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:
    1. For Each but As Button In Me.Controls.OfType(Of Button)()
    2.                 If lbl.Bounds.IntersectsWith(but.Bounds) Then
    3.                     MsgBox("This is a Test")
    4.                 End If
    5.             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
  •