70-526 MCTS


Layout

The layout of a form can be affected in many ways


AutoScaleMode

The default value is set to None although the IDE sets the default to Font for a new Form. This property specifies the current automatic scaling mode of the Form and takes a value from the AutoScaleMode enumeration:

  • Dpi - Controls scale relative to the display resolution. Common resolutions are 96 and 120 DPI.
  • Font - Controls scale relative to the dimensions of the font the classes are using, which is typically the system font.
  • Inherit - Controls scale according to the classes' parent's scaling mode. If there is no parent, automatic scaling is disabled.
  • None - Automatic scaling is disabled.

    To change the AutoScaleMode property at run time, simply set the property to the desired value.

    this.AutoScaleMode = AutoScaleMode.Font;

    AutoScroll

    The default value is set to False. This property indicates whether the form enables autoscrolling. When you set this property is true, scroll bars are displayed on the form when any controls are located outside of the form's client region. Also, the client area of the form automatically scrolls to make the control with input focus visible.

    To change the AutoScroll property at run time, simply set the property to the desired value.

    Form1.AutoScroll = true;

    AutoScrollMargin

    The default value is set to 0,0. This property is the distance between any child controls and the edges of the scrollable parent control. When determining whether or not scroll bars are needed, the AutoScrollMargin size is added to the size of any child controls contained in the scrollable control. Docked controls are excluded from the calculations that determine if scroll bars must be displayed.

    To change the AutoScrollMargin property at run time, use the SetAutoScrollMargin method.

    Form1.SetAutoScrollMargin(5, 5);

    AutoScrollMinSize

    The default value is set to 0,0. This property determines the minimum size of the area in the form through which the user can scroll.

    For example, if AutoScrollMinSize is set to 300,200, then anytime that either the height is smaller than 200 or the width is smaller than 300, then the scroll bars will appear. (Technically the scroll bars appear anytime that the width is AutoScrollMinSize.Width - scrollbar.width or anytime that the height is AutoScrollMinSize.Height - scrollbar.height).

    To change the AutoScrollMinSize property at run time, set the property to a new instance of the Size structure:

    this.AutoScrollMinSize = new Size (10, 20);

    AutoSize

    The default value is set to False. This property resizes the form according to the setting of AutoSizeMode. When set to true, the form will automatically resize to fit its contents.

    Note

    When using AutoSize, the MinimumSize and MaximumSize properties are respected, but the current value of the Size property is ignored. Using AutoSize and AutoSizeMode also renders the AutoScroll property superfluous, as there is no way to shrink the form to hide its contained controls from view.

    To change the AutoSize property at run time, simply set the property to the desired value.

    this.AutoSize = true;

    AutoSizeMode

    The default value is set to GrowOnly. This property determines the mode by which the form automatically resizes itself and takes a value from the AutoSizeMode enumeration:

  • GrowAndShrink - The control grows or shrinks to fit its contents. The control cannot be resized manually.
  • GrowOnly - The control grows as much as necessary to fit its contents but does not shrink smaller than the value of its Size property. The form can be resized, but cannot be made so small that any of its contained controls are hidden.

    To change the AutoSizeMode property at run time, simply set the property to the desired value.

    this.AutoSizeMode = AutoSizeMode.GrowAndShrink;

    Location

    The default value is set to 0,0. This property determines the position on the screen when StartPosition is set to Manual.

    Note

    If you want to set the size and location of a form in one go, you can use the DesktopBounds property.

    To change the Location property at run time, set the property to a new instance of the Point structure:

    this.Location = new System.Drawing.Point (100, 100);

    MaximumSize

    The default value is set to 0,0. This property determines the maximum size the form can be resized to.

    To change the MaximumSize property at run time, set the property to a new instance of the Size structure:

    this.MaximumSize = new Size(500, 500);

    MinimumSize

    The default value is set to 0,0. This property determines the minimum size the form can be resized to.

    To change the MinimumSize property at run time, set the property to a new instance of the Size structure:

    this.MinimumSize = new Size(200, 200);

    Padding

    The default value is set to 0, 0, 0, 0 and is inherited from Control. This property represents the control's internal spacing characteristics. Controls receive default values for Padding that are reasonably close to Windows user interface guidelines.

    The padding is the internal space between the body of the UI element and its edge. A useful diagram explaining this can be found on MSDN - Margin and Padding in Windows Forms Controls.

    To change the Padding property at run time, set the property to a new instance of the Padding structure:

    this.Padding = new System.Windows.Forms.Padding(5);

    Size

    The default value is set to 300,300. This property gets or sets the size of the form and is an instance of the Size structure.

    This property allows you to set both the height and width (in pixels) of the form at the same time instead of setting the Height and Width properties individually.

    To change the Size property at run time, set the property to a new instance of the Size structure:

    Form1.Size = new Size(600,500);
    Note

    If you want to set the size and location of a form in one go, you can use the DesktopBounds property.

    The size of the form can also be altered at run time by grabbing and dragging any part of the border of the form.


    StartPosition

    The default value is set to WindowsDefaultLocation. This property determines the initial position of the form when it is first displayed and takes a value from the FormStartPosition enumeration:

  • CenterParent - The form is centred within the bounds of its parent form.
  • CenterScreen - The form is centred on the current display, and has the dimensions specified in the form's size.
  • Manual - The position of the form is determined by the Location property.
  • WindowsDefaultBounds - The form is positioned at the Windows default location and has the bounds determined by Windows default.
  • WindowsDefaultLocation - The form is positioned at the Windows default location and has the dimensions specified in the form's size.

    To change the StartPosition property at run time, simply set the property to the desired value.

    this.StartPosition = FormStartPosition.CenterScreen;

    WindowState

    The default value is set to Normal. This property represents the window state of the Form and takes a value from the FormWindowState enumeration:

  • Maximized - A maximized window.
  • Minimized - A minimized window.
  • Normal - A default sized window.

    By setting a value other than Normal in the Properties Window of the IDE, this will dictate the state of the Form when the Form first opens.

    To change the WindowState property at runtime, simply set the property to the desired value.

    Form1.WindowState = FormWindowState.Minimized;

    or

    Form1.WindowState = FormWindowState.Maximized;

    DesktopBounds

    This property is not located in the Properties Window of the IDE.

    If you want to set the size and location of a form, you can use the DesktopBounds property to size and locate the form based on desktop coordinates or use the Bounds property of the Control class to set the size and location of the form based on screen coordinates.

    For example, to set the size of the form to 400 wide and 400 high and the position of a form so that the form is positioned 50 pixels from the left border of the desktop and 50 pixels from the top of the desktop you would use the following code:

    // Create a Rectangle object that will be used as the bound of the form.
    Rectangle tempRect = new Rectangle(50,50,400,400);
    
    //  Set the bounds of the form using the Rectangle object.
    this.DesktopBounds = tempRect;
    Note

    The maximum value of this property is limited by the resolution of the screen on which the form runs. The value cannot be greater than 12 pixels over each screen dimension (horizontal + 12 and vertical + 12).

    MSDN references

  • ContainerControl.AutoScaleMode Property
  • Form.AutoScroll Property
  • ScrollableControl.AutoScrollMargin Property
  • ScrollableControl.AutoScrollMinSize Property
  • Form.AutoSize Property
  • Form.AutoSizeMode Property
  • Form.Location Property
  • Form.MaximumSize Property
  • Form.MinimumSize Property
  • Control.Padding Property
  • Form.Size Property
  • Form.StartPosition Property
  • Form.WindowState Property
  • Form.DesktopBounds Property
  • Automatic Scaling in Windows Forms
  • Adjusting the Size and Scale of Windows Forms
  • How to: Resize Windows Forms (size)


    << Previous Contents Next >>

    © Publicjoe, 2007