Search code examples
c#class-design

prevent height sizing at design time


I'm working on a custom user control. How can I prevent the HEIGHT ONLY of the control from being modified during the design-time interface.


Solution

  • You can override the SetBoundsCore method and disallow changes to height by changing the height value before calling the base class implementation.

    private const int FixedHeightIWantToKeep = 100;
    
    protected override void SetBoundsCore(
        int x,
        int y,
        int width,
        int height,
        BoundsSpecified specified)
    {
        // Fixes height at 100 (or whatever fixed height is set to).
        height = this.FixedHeightIWantToKeep;
        base.SetBoundsCore(x, y, width, height, specified);
    }