Sdílet prostřednictvím


AutoSize Property Overview

The AutoSize property enables a control to change its size, if necessary, to attain the value specified by the PreferredSize property. You adjust the sizing behavior for specific controls by setting the AutoSizeMode property.

AutoSize Behavior

Only some controls support the AutoSize property. In addition, some controls that support the AutoSize property also support the AutoSizeMode property.

The AutoSize property produces somewhat different behavior, depending on the specific control type and the value of the AutoSizeMode property, if the property exists. The following table describes the behaviors that are always true and provides a brief description of each:

Always true behavior

Description

Automatic sizing is a run-time feature.

This means it never grows or shrinks a control and then has no further effect.

If a control changes size, the value of its Location property always remains constant.

When a control's contents cause it to grow, the control grows toward the right and downward. Controls do not grow to the left.

The Dock and Anchor properties are honored when AutoSize is true.

The value of the control's Location property is adjusted to the correct value.

Note   The Label control is the exception to this rule. When you set the value of a docked Label control's AutoSize property to true, the Label control will not stretch.

A control's MaximumSize and MinimumSize properties are always honored, regardless of the value of its AutoSize property.

The MaximumSize and MinimumSize properties are not affected by the AutoSize property.

There is no minimum size set by default.

This means that if a control is set to shrink under AutoSize and it has no contents, the value of its Size property is 0,0. In this case, your control will shrink to a point, and it will not be readily visible.

If a control does not implement the GetPreferredSize method, the GetPreferredSize method returns last value assigned to the Size property.

This means that setting AutoSize to true will have no effect.

A control in a TableLayoutPanel cell always shrinks to fit in the cell until its MinimumSize is reached.

This size is enforced as a maximum size. This is not the case when the cell is part of an AutoSize row or column.

AutoSizeMode Property

The AutoSizeMode property provides more fine-grained control over the default AutoSize behavior. The AutoSizeMode property specifies how a control sizes itself to its content. The content, for example, could be the text for a Button control or the child controls for a container.

The following table shows the AutoSizeMode settings and a description of the behavior each setting elicits.

AutoSizeMode setting

Behavior

GrowAndShrink

The control grows or shrinks to encompass its contents.

The MinimumSize and MaximumSize values are honored, but the current value of the Size property is ignored.

This is the same behavior as controls with the AutoSize property and no AutoSizeMode property.

GrowOnly

The control grows as much as necessary to encompass its contents, but it will not shrink smaller than the value specified by its Size property.

This is the default value for AutoSizeMode.

Controls That Support the AutoSize Property

The following table lists the controls that support the AutoSize and AutoSizeMode properties.

AutoSize support

Control type

  • AutoSize property supported.

  • No AutoSizeMode property.

CheckBox

DomainUpDown

Label

LinkLabel

MaskedTextBox (TextBox base)

NumericUpDown

RadioButton

TextBox

TrackBar

  • AutoSize property supported.

  • AutoSizeMode property supported.

Button

CheckedListBox

FlowLayoutPanel

Form

GroupBox

Panel

TableLayoutPanel

CheckedListBox

ComboBox

DataGridView

DateTimePicker

ListBox

ListView

MaskedTextBox

MonthCalendar

ProgressBar

PropertyGrid

RichTextBox

SplitContainer

TabControl

TabPage

TreeView

WebBrowser

ScrollBar

AutoSize in the Design Environment

The following table describes the sizing behavior of a control at design time, based on the value of its AutoSize and AutoSizeMode properties.

Override the SelectionRules property to determine whether a given control is in a user-resizable state. In the following table, "cannot" means Moveable only, "can" means AllSizeable and Moveable.

AutoSize settings

Design-time sizing gesture

  • AutoSize = true

  • No AutoSizeMode property.

The user cannot resize the control at design time, except for the following controls:

The user cannot resize the control at design time.

The user can resize the control at design time. When the Size property is set, the user can only increase the size of the control.

User can resize the control at design time.

Poznámka

To maximize productivity, the Windows Forms Designer shadows the AutoSize property for the Form class. At design time, the form behaves as though the AutoSize property is set to false, regardless of its actual setting. At runtime, no special accommodation is made, and the AutoSize property is applied as specified by the property setting.

See Also

Reference

AutoSize

PreferredSize

GetPreferredSize