I'm creating 50 picture boxes where all of them has a constant width and height of 50 and on run time, i randomly place them in random locations on the form but the problem is some picture boxes overlaps with each other... any idea on how to randomly place the picture boxes on the form with no overlapping?

that's the problem, how am i going to create an array of non overlapping locations? my pictureboxes are placed on an array of picture boxes, each box has there boundaries like for example,

pic box 1 (width = 50, height = 50) and placed at location 0, 0 there fore the bounds of picbox 1 is from 0, 0 (topleft) and 0,50 (topright) and 50, 0 (bottomleft) and 50,50(bottom right).

pic box 2 (width = 50, height = 50) and placed at location 1, 1 therefore the bounds of picbox2 is from 1,1(topleft) and 1, 51(topright) and 51,0 (bottomleft) and 51,51(bottom right)

Now look, the problem with picbox 2 is that it is located on the bounds of pic box 1.

Why not use location 51,0 for picbox 2? That way it will be placed just right of picbox 1 and not overlap.

Usually you do a forumla such as

int pic_width=50;
int pic_height=50;

for(w=0;w<10;w++)
{
  picturebox[i].top=0;
  picturebox[i].left=(pic_width+1)*i;
  picturebox[i].height=pic_height;
  picturebox[i].width=pic_height;
}

this means box1 ends up at 0,0 , box2 51,0, box3 102,0 etc so they dont overlap.

I want it random and not fixed. So What I want is a method that will just return a true or false if it overlaps with a picbox. True if it overlaps, if it's true then regenerate another random location. If false, then place the pic box on that location

There is a Rectangle.Contains method ,so you have to remember all the rectangles of the pics you already placed and see if the new loc is in or out. There is also the Regionclass to consider but I don't know if it has a Contains method or something simular.

Yeah I solved it now, each controls has a "Bounds" Property and this Bounds property has a "intersectsWith(another object's bounds)" which returns true if they collide with each other.

Thats a very tough way of doing it - while I showed you a "fixed" way of laying them out - its very simple to randomize a position using that formula.

I said i want it random... not fixed. besides, your suggestion does not meet my very goal.

Well, it does, because the forumla doesnt require you to use it in order, because its a formula. You can randomize it and slot items into place without overlap.

This article has been dead for over six months. Start a new discussion instead.