Controls Overlapping
-
-
I'm creating a lot a picture boxes. Each picture box has a constant height and width of 50. So I randomly place this picture boxes on the form. My problem is that some picture boxes overlaps or on top of other picture box. How do I solve this problem?
You need to look at the existing picture boxes and if they overlap with your new picture box then move your new picture box. Detecting an overlap is fairly simple. Each control can give you its Bounds[^], which is a Rectangle[^] object. The Rectangle structure has an IntersectsWith[^] method which will return true if the rectangles intersect (overlap) with each other. For example:
bool isOverlapping = firstPictureBox.Bounds.IntersectsWith(secondPictureBox.Bounds);
Developer Day Scotland 2 - Free community conference Recent blog posts: *Throwing Exceptions *Training Developers * Method hiding or overriding - or the difference between new and virtual