0

I have a form that I move labels around in with the mouse. What I'd like to do is make sure that I don't move one label into space used by another control.

Is there a better, more efficient method than what I'm using below:

for each c as control in me.controls
  if c.location.x > testcontrol.location.x and c.location.x < testcontrol.location.x + testcontrol.height and c.location.y > testcontrol.location.y and c.location.y < testcontrol.location.y + testcontrol.width then
     'testcontrol shares space with control c
     <do stuff>
   End if
next

I find the above overly clunky and hard to understand/read. Just hoping that there's some VB magic out there that will do this much more efficiently.

From my searches, I see C# has an 'intersectswith' function, but I can't seem to find similar for VB. This post shows vb using intersectwith, but I can't get that working for my stuff. Perhaps a different version of VB?

gCanuck
  • 39
  • 1
  • 6

1 Answers1

0

You forgot to add Bounds:

For Each c As Control In Me.Controls
    If c.Bounds.IntersectsWith(testcontrol.Bounds) Then
         'testcontrol shares space with control c
        <do stuff>
    End If
Next

IntersectsWith isn't a method of Control but of Rectangle.

MatSnow
  • 7,357
  • 3
  • 19
  • 31