AutoLayout by Examples - Part 3 - Dialog 2

Dialog 2: Horizontally resizable dialog with textboxes

 

Project Name: TextBoxesFixedHeightDialog.csproj. Download source.

 

Characteristics:

  • Fixed height dialog with resizable width

 

Screen Shots:

 

 

 

 

Designer Layout:

 

Document Outline:

 

Key Notes:

  • The height of the dialog in the designer will not be the actual run-time height. Note the big gap at the bottom in the designer. This gap will be closed at run-time.
  • The “trick” (download source):
    • The dialog AutoSize is set to True in the designer which tells Windows Forms to “wrap” snuggly around its content at run-time
    • Once the dialog is loaded (in the dialog OnLoad override), AutoSize is programmatically changed to False to allow dialog resizing

 

        /// <summary>

  /// Hook dialog loading to pin the height and enable resize.

        /// </summary>

        /// <param name="e"></param>

        protected override void OnLoad(EventArgs e)

        {

            base.OnLoad(e);

            // AutoSize was turned on in the designer to allow the dialog

            // to fit to its content. Now that the dialog snugly fits

            // its content, we can clip the dialog height but let its

            // width freely sizes.

            MinimumSize = new Size(Width, Height);

            MaximumSize = new Size(int.MaxValue, Height);

            // Now that the dialog has the height restriction enforced,

            // turn off AutoSize to allow horizontal sizing.

            AutoSize = false;

        }

 

  • The fixed dialog height is enforced by setting the dialog’s minimum and maximum height to the same value

 

This posting is provided "AS IS" with no warranties, and confers no rights. Use of included

script samples are subject to the terms specified at https://www.microsoft.com/info/cpyright.htm