AutoLayout by Examples - Part 3 - Dialog 4

Dialog 4: Horizontally resizable dialog with growing/shrinking label

 

Project Name: AutoSizedLabelFixedHeightDialog.csproj. Download source.

 

Characteristics:

  • Fixed height dialog with resizable width
  • Auto-size label which grows and shrinks

 

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 since the dialog AutoSize is True.
  • See the “trick” in Key Notes of Dialog 2 for information on allowing the dialog to be auto-sized and still be resizable
  • Notice in the screen shots how the label height is reduced from 4 to 2 lines as the dialog width is increased. The label grows and shrinks in height as its width changes. Unlike the label in Dialog 1, growing and shrinking the label cannot be done automatically because there is no other control in the dialog (like a ListView) to absorb extra or to give away vertical spaces.

 

In this case, the entire “overarching” (i.e. the outer-most) TableLayoutPanel height is changed in lockstep with the label’s height. To compensate for the growing/shrinking “overarching” TableLayoutPanel, the dialog height is programmatically adjusted via the SizeChanged event of the “overarching” TableLayoutPanel. Additionally, the dialog minimum and maximum heights are also updated to maintain the fixed height. Download source.

 

        /// <summary>

        /// Hook table size changes to resize the dialog's height.

        /// </summary>

        /// <param name="sender"></param>

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

        private void overarchingLayoutPanel_SizeChanged(object sender, EventArgs e)

        {

            // Calculate the new dialog height

            int decorHeight = Bounds.Height - ClientRectangle.Height;

            Size newSize = new Size(Width, decorHeight + Padding.Top + overarchingLayoutPanel.Height + Padding.Bottom);

            // Re-pin the dialog height

            MinimumSize = new Size(MinimumSize.Width, newSize.Height);

            MaximumSize = new Size(MaximumSize.Width, newSize.Height);

            Size = newSize;

        }

 

 

 

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 http://www.microsoft.com/info/cpyright.htm