Serialcoder en Français Serialcoder in English
TEL : +33 (0)9 72 13 15 17

Windows Forms FAQ resources

2. Windows Forms Controls

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


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