I need to order windows in “rectangle” form. It’s mean that when I have six windows it orders it in rectangle 2×3, when I have 5 windows it orders it 2×3 but without last window, when I have 9 windows it orders in 3×3. But I’ve got some troubles with coordinates – child windows are out of bounds of mdiparent window.(img)
I used the same algorithm as I used in my mdi application on java
for(int i=0;i<a;i++)
for(int j=0;j<b;j++)
try{
indfr.get(counter).setLocation(i*theDesktop.getWidth()/a,j*theDesktop.getHeight()/b);
indfr.get(counter).setSize(theDesktop.getWidth()/a,theDesktop.getHeight()/b);
counter++;
}catch (IndexOutOfBoundsException exc){ break;}
where indfr - arralist of JInternalFrames,and theDesktop - JDesktopPane
Algorithm in c#
for (int i = 0; i < a; i++)
for (int j = 0; j < b; j++)
try
{
list[counter].SetDesktopLocation(i*list[counter].MdiParent.Width/a, j*list[counter].MdiParent.Height/b);
list[counter].Size = new Size(list[counter].MdiParent.Width/a, list[counter].MdiParent.Height/b);
counter++;
}
catch (IndexOutOfRangeException)
{
break;
}
where list – Form[] list = this.MdiChildern;
What is wrong with coordinates?
(P.S it is not whole algorithm but it is the main loop where windows are ordering)

The problem lines are those inside of your
tryblock:You’re checking
Form.WidthandForm.Height, which return the total size of the form on the screen, including all of the borders. You can only place child windows inside the client area of the parent form, so you need to query theClientSizeproperty instead. That is defined as the size of the form, minus the borders and title bar; in other words, the area of the form in which children can be placed.Re-write your
tryblock to this, instead:And then get rid of that silly empty
catchblock. If all you’re doing when an exception is thrown isbreaking, then there’s no point in catching the exception. It will bubble up to the next exception handler, all the way to the global one if necessary. Only catch exceptions that you know specifically how to handle. You shouldn’t be getting anIndexOutOfRangeException, and if you do, that’s a bug in your code—you want to know about it so that you can fix it. That means not swallowing the exception.In that case, there’s an easier method than writing a bunch of
forloops and manually setting the size and position of your children. Instead, just call theForm.LayoutMdimethod on your parent MDI form, and specify one of theMdiLayoutenumeration values. In this case, you probably want eitherMdiLayout.TileHorizontalorMdiLayout.TileVertical.WinForms will take care of arranging your child windows precisely how you want them automatically.