C# – Limit the size of a user control at design time


I have a namespace Company.UI.Forms where we have a form base class BaseForm that inherits from System.Windows.Forms.Form.

I need to limit the size of this form, so that if a concrete form, say ExampleForm, derives from BaseForm, it should have a fixed size in the VS designer view. The fixed size should be set or defined in some way (in the BaseForm ctor?) so that the derived ExampleForm and the designer picks it up.

Is that possible?

[Edit] The purpose is to use the base class as a template for a full screen form for a Windows CE device of known screen size, for which I want to be able to lay out child controls in the designer.

Best Solution

In standard WinForms you can override the SetBoundsCore method. This code will prevent the base classes to change it at design time... but remember it can be overriden by any inherited class anyway:

protected override void SetBoundsCore(int x, int y,
     int width, int height, BoundsSpecified specified)
  if (this.DesignMode)
    base.SetBoundsCore(x, y, 500, 490, specified);
    base.SetBoundsCore(x, y, width, height, specified);

I've put the if because you specified to be at design time, if you don't want a resize at runtime as well remove it.

Related Question