ACAT.Extensions.Default.FunctionalAgents.LectureManager.LectureManagerMainForm.centerControls C# (CSharp) Method

centerControls() private method

Since we are resizing the form, the controls on the form are all anchored. We have to make sure they are all repositioned correctly. Use the center of gravity method to move all the controls relative to each other. The CG can move vertically, horizontally or in both directions
private centerControls ( List controls, MoveDirection direction ) : void
controls List list of controls in the form
direction MoveDirection which direction to move the CG
return void
        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;
                    }
                }
            }
        }