How do I prevent a control from being resized manually by the user or through docking or anchoring?

Platform: WPF| Category: Layouts

The best way to ensure a particular height or width for a control is to override the control’s SetBoundsCore method.

This sample shows a Button which has a fixed height and width. The same technique can be used to set the upper and lower bounds on sizes.

[C#]

using System.Windows.Forms;

public class ButtonWithConstrainedSize : Button
{
  private static int fixedHeight = 50;
  private static int fixedWidth = 50;

  protected override void SetBoundsCore( int x, int y, int width, int height,
      BoundsSpecified specified )
  {
    base.SetBoundsCore( x, y, fixedWidth, fixedHeight, specified );
  }
}

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.