How do I prevent resizing of my Controls by the user, via Docking or anchoring or manual resizing during design-time?

Platform: WinForms| Category: Controls

The best place to ensure a particular height/width for you control is in the SetBoundsCore override of your Control, as follows:


		protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified)
		{
			int prefHeight = this.GetPreferredHeight();
			// Ensure that the height is atleast as big as prefHeight
			if(height < prefHeight)
				height = prefHeight;

			base.SetBoundsCore(x, y, width, height, specified);
		}

	Protected Overrides  Sub SetBoundsCore(ByVal x As Integer, ByVal y As Integer, ByVal width As Integer, ByVal height As Integer, ByVal specified As BoundsSpecified)
			Dim prefHeight As Integer =  Me.GetPreferredHeight() 
			’ Ensure that the height is atleast as big as prefHeight
			If height < prefHeight Then
				height = prefHeight
			End If
 
			MyBase.SetBoundsCore(x, y, width, height, specified)
		End Sub

Share with

Related FAQs

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

Please submit your question and answer.