StackPanel Class

A panel that arranges its child elements in a single line, either vertically or horizontally.

Namespace: DevExpress.Utils.Layout

Assembly: DevExpress.Utils.v20.1.dll

Declaration

public class StackPanel :
    XtraLayoutPanelBase,
    IStackPanel,
    IXtraLayoutPanel
Public Class StackPanel
    Inherits XtraLayoutPanelBase
    Implements IStackPanel,
               IXtraLayoutPanel

Remarks

The Stack Layout Panel arranges its child controls in one of four directions depending on the StackPanel.LayoutDirection setting. The figure below illustrates the top-to-bottom element direction.

stack-panel

To populate a panel at design time, drag and drop controls onto the panel. You can drag child panel elements to re-arrange them.

To populate a stack panel in code, add elements to the panel's Controls collection.

 stackPanel1.Controls.Add(this.labelControl6);
 stackPanel1.Controls.Add(this.textEdit5);
 stackPanel1.Controls.Add(this.labelControl5);
 stackPanel1.Controls.Add(this.textEdit6);
 stackPanel1.Controls.Add(this.labelControl4);
 stackPanel1.Controls.Add(this.textEdit8);
 stackPanel1.Controls.Add(this.labelControl3);
 stackPanel1.Controls.Add(this.textEdit9);
 stackPanel1.Controls.Add(this.simpleButton5);
 stackPanel1.Controls.Add(this.simpleButton6);
 stackPanel1.LayoutDirection = DevExpress.Utils.Layout.StackPanelLayoutDirection.TopBottom;
 stackPanel1.Padding = new System.Windows.Forms.Padding(0, 10, 0, 0);

Stretch Controls

You can stretch a control to make it occupy all the free space available in a stack panel. To do that at design time, enable the control's Stretched extender property. In the figure below, a TextEdit control lying in between two SimpleButtons is stretched.

stack-panel

To do that in code, call the StackPanel.SetStretched(Control, Boolean) method and pass the required control as the first method parameter.

stackPanel3.SetStretched(textEdit10, true);

To limit the size of a stretched control, use its MaximumSize value.

NOTE

The Stretched extender property is ignored in auto-size mode.

Auto-Size Mode

The StackPanel can automatically adjust its size to fit its contents. Use the following properties to enable the panel's auto-size mode:

  • AutoSize - Set this option to true to enable the auto-size functionality.
  • AutoSizeMode - Use this property to choose the auto-size behavior - GrowOnly or GrowAndShrink. See the AutoSizeMode topic in MSDN to learn more.
TIP

You can additionally enable the auto-size functionality for controls within the StackPanel, and for containers (e.g., a form) where the StackPanel resides.

Example

The following example enables the auto-size functionality for the panel and the form that contains this panel. The code also adds an empty space around the panel via the Padding setting.

private void Form1_Load(object sender, EventArgs e) {
    this.AutoSize = true;
    this.AutoSizeMode = AutoSizeMode.GrowAndShrink;
    stackPanel1.Dock = DockStyle.Fill;
    stackPanel1.Padding = new Padding(10);
    stackPanel1.AutoSize = true;
    stackPanel1.AutoSizeMode = AutoSizeMode.GrowAndShrink;
}
NOTE

The Stretched extender property is ignored in auto-size mode.

Other Settings

  • AutoScroll - Gets or sets whether the panel enables scrollbars that allow you to scroll to any controls placed outside the panel's visible boundaries.
  • AutoTabOrder - Gets or sets whether the panel maintains an automatic tab order of its child controls.
  • LabelVertAlignment - Gets or sets how LabelControls are vertically aligned in relation to the text editors displayed to the label's right (to the label's left in right-to-left locales).
See Also