views:

108

answers:

1

Hi, I have a requirement to resize all the controls on a form when my form resizes. Unfortunately, due to some issues I can not use any container control or make use of anchor/dock property. I need to do everything using code. I am able to resize the controls as per the ratio the screen is resized. While performing this resizing, I need to calculate the new position of all the controls along with there new size. The issue is while calculating these new points I am getting the new values in decimal and if I round them then it is causing the problems and calculating wrong size and position each time I resize my form. I saw some third party controls for the same task but they all calculate the position exactly.

Please help me resolve this issue. Thanks in advance, Ashish Sharma

A: 

Is this C#/Windows Forms?

Manual scaling is a pain in the neck. Realistically the only way to completely prevent any major rounding errors over the long-term lifetime of a form is to store the original locations and sizes of every control that you reposition/resize, and make all your reposition/resize code relative to the original position as opposed to the current position.

You'll end up with some fugliness like this:

public class Form1
{
    private Size originalFormSize;

    private List<Control> controlsToResize = new List<Control>();
    private List<Point> originalLocations;
    private List<Size> originalSizes;

    public Form1()
    {
        InitializeComponent();
        SaveOriginalSizes();
    }

    private void SaveOriginalSizes()
    {
        originalFormSize = Size;
        controlsToResize.Add(panel1);
        controlsToResize.Add(panel2);
        ...
        originalLocations = new List<Point>(controlsToResize.Count);
        originalSizes = new List<Size>(controlsToResize.Count);
        foreach (Control c in controlsToResize)
        {
            originalLocations.Add(c.Location);
            originalSizes.Add(c.Size);
        }
    }

    protected override void OnResize(EventArgs e)
    {
        base.OnResize(e);
        float scaleX = (float)originalFormSize.Width / Size.Width;
        float scaleY = (float)originalFormSize.Height / Size.Height;
        for (int i = 0; i < controlsToResize.Count; i++)
        {
            Control c = controlsToResize[i];
            UpdatePosition(c, originalLocations[i], scaleX, scaleY);
            UpdateSize(c, originalSizes[i], scaleX, scaleY);
        }
    }
}

...Where obviously you would have to implement the UpdatePosition and UpdateSize methods on your own - I'm assuming you already have some sort of implementation.

Honestly, it's pretty god-awful. I would strongly recommend that instead of trying to do any of this, you revisit whatever requirements/design constraints are preventing you from using the Anchor and Dock properties. In all my years, I don't think I've ever heard of a legitimate reason for not using either these or the layout controls TableLayoutPanel/FlowLayoutPanel or both. Those are the way to deal with layouts in WinForms; anything else is a clumsy hack.

Aaronaught
Hi Aaronaught,Thanks for your reply.I can understand that using anchor and dock is the easiest way out there but can't use the same :(Anyways, I will try to save the initial positions of the controls and will calculate as per those positions. I will let you know the results once I implement that.