Here is how i add and arrange the forms based on their Tag:
public static void AddForm(Form parentForm, Form childForm, int tag)
{
childForm.MdiParent = parentForm;
childForm.Tag = tag;
childForm.Show();
}
public static void ArrangeChildForms(Form parentForm)
{
int numberOfColumns = parentForm.MdiChildren.Length / 2 + parentForm.MdiChildren.Length % 2;
int numberOfChildForms = parentForm.MdiChildren.Length;
int availableWidth = parentForm.ClientSize.Width - 20;
int availableHeight = parentForm.ClientSize.Height;
int childFormWidth = availableWidth / numberOfColumns;
int childFormHeight = availableHeight / 2;
Form[] sortedChildren = parentForm.MdiChildren.OrderBy(child => (int)child.Tag).ToArray();
for (int i = 0; i < numberOfChildForms; i++)
{
Form child = sortedChildren[i];
int row = i / numberOfColumns;
int col = i % numberOfColumns;
child.Left = col * childFormWidth;
child.Top = row * childFormHeight;
child.Width = childFormWidth;
child.Height = childFormHeight;
}
}
Here is how i call the methods in container/parent form (called after parent form is fully loaded):
foreach (var (machine, i) in _lstMachine.Select((machine, i) => (machine, i)))
{
Form form = GetForm(machine.CommunicationType, machine.ID);
AddForm(this, form, i);
}
ArrangeChildForms(this);
The problem is it shows the entire process of adding child forms and arranging them, but I want to hide all the child forms until they are arranged to their place. I’ve tried tinkering around the visibility of the forms, move the childForm.Show()
from AddForm()
to ArrangeChildForms()
, use SuspendLayout()
in the container but it doesn’t work either. Hopefully I can get some insights about this problem.