Microsoft Communities

Welcome to WindowsClient.net | Sign in | Join

Here are some frequently asked questions about Windows Forms and their answers.

Windows Forms FAQs

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

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 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 );
  }
}

[Visual Basic]

Imports System.Windows.Forms

Public Class ButtonWithConstrainedSize
  Inherits Button

  Private Shared FixedHeight As Integer = 50
  Private Shared FixedWidth As Integer = 50

  Protected Overrides Sub SetBoundsCore(ByVal x As Integer, ByVal y As Integer, _
      ByVal width As Integer, ByVal height As Integer, _
      ByVal specified As BoundsSpecified)
    MyBase.SetBoundsCore(x, y, FixedWidth, FixedHeight, specified)
  End Sub
End Class

George Shepherd, Syncfusion, and Stuart Celarier, Fern Creek



Page view counter