How to proportionally resize a control with the mouse in the WinForm control designer?

I tried to resize the mouse control by holding the left + Shift button, hoping that the width and height would be adjusted proportionally (for example, in Photoshop). Does not work.

I Googled to find out how to do this, sure enough that I will have an answer in one minute. To my surprise, I could not find anything.

Do I have to understand that Visual Studio, even before the 2013 version, lacks this very basic design feature ?! Or am I just missing him?

Please note that this is not only for a specific control; it is a design tool that I would like to use on anything that can be "drawn" on a form / user control.

+4
source share
1 answer

You can always expand the control you want to maintain the ratio of:

public class Panelx : Panel {

    private int _width;
    private int _height;
    private double _proportion;
    private bool _changingSize;

    [DefaultValue(false)]
    public bool MaintainRatio { get; set; }

    public Panelx() {
        MaintainRatio = false;
        _width = this.Width;
        _height = this.Height;
        _proportion = (double)_height / (double)_width;
        _changingSize = false;
    }

    protected override void OnResize(EventArgs eventargs) {
        if (MaintainRatio == true) {
            if (_changingSize == false) {
                _changingSize = true;
                try {
                    if (this.Width != _width) {
                        this.Height = (int)(this.Width * _proportion);
                        _width = this.Width;
                    };
                    if (this.Height != _height) {
                        this.Width = (int)(this.Height / _proportion);
                        _height = this.Height;
                    };
                }
                finally {
                    _changingSize = false;
                }
            }
        }
        base.OnResize(eventargs);
    }

}

Then all you have to do is set the MaintainRatio property to true so that it changes accordingly.

This solution can be quite complicated if you need to work with many different controls.

+1
source

All Articles