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
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:
…Where obviously you would have to implement the
UpdatePositionandUpdateSizemethods 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
AnchorandDockproperties. In all my years, I don’t think I’ve ever heard of a legitimate reason for not using either these or the layout controlsTableLayoutPanel/FlowLayoutPanelor both. Those are the way to deal with layouts in WinForms; anything else is a clumsy hack.