Crono
Crono

Reputation: 10478

How to proportionally resize a control with the mouse within WinForm's control designer?

I was trying to resize a control with the mouse, holding down Left-button + Shift, hoping that both width and height would be adjusted proportionally (like in Photoshop). Didn't work.

I Googled to find out how to do that, sure enough that I'd have an answer within one minute. To my surprise I couldn't find anything.

Must I understand that Visual Studio, even as far as version 2013, lacks this very basic designing feature?! Or do I just keep missing it?

Please note that this isn't for a particular control only; it's a designing tool I would want to use on anything that can be "drawn" on a form / user control.

Upvotes: 4

Views: 1047

Answers (1)

Rick
Rick

Reputation: 841

You could always extend 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 need to do is set the MaintainRatio property to 'true' to have it resize appropriately.

This solution could prove quite arduous if you need it to work with many different controls, though.

Upvotes: 2

Related Questions