private void centerControls(List<Control> controls, MoveDirection direction)
{
if (controls.Count > 0)
{
int sumX = 0;
int sumY = 0;
int offsetX = 0;
int offsetY = 0;
Point center;
foreach (Control control in controls)
{
center = new Point(control.Location.X + control.Width / 2, control.Location.Y + control.Height / 2);
sumX = sumX + center.X;
sumY = sumY + center.Y;
}
Point mean = new Point(sumX / controls.Count, sumY / controls.Count);
center = new Point(Width / 2, Height / 2);
offsetX = center.X - mean.X;
offsetY = center.Y - mean.Y;
foreach (Control control in controls)
{
switch (direction)
{
case MoveDirection.Vertical:
control.Location = new Point(control.Location.X + offsetX, control.Location.Y);
break;
case MoveDirection.Horizontal:
control.Location = new Point(control.Location.X, control.Location.Y + offsetY);
break;
case MoveDirection.Both:
control.Location = new Point(control.Location.X + offsetX, control.Location.Y + offsetY);
break;
}
}
}
}