Loading...
Loading...
How to use Syncfusion Windows Forms GridBagLayout control to arrange child controls in a flexible virtual grid with customizable rows, columns, spacing, and alignment. Use this skill whenever the user needs to create complex layouts with GridBagLayout, arrange controls dynamically, configure grid positioning, set up control spanning, or manage control alignment and sizing within a Windows Forms application.
npx skill4agent add syncfusion/winforms-ui-components-skills syncfusion-winforms-grid-bag-layout// Create layout manager
GridBagLayout gridBagLayout1 = new GridBagLayout();
gridBagLayout1.ContainerControl = this;
// Create buttons
ButtonAdv button1 = new ButtonAdv { Text = "Button 1" };
ButtonAdv button2 = new ButtonAdv { Text = "Button 2" };
ButtonAdv button3 = new ButtonAdv { Text = "Button 3" };
ButtonAdv button4 = new ButtonAdv { Text = "Button 4" };
// Add to form
this.Controls.Add(button1);
this.Controls.Add(button2);
this.Controls.Add(button3);
this.Controls.Add(button4);
// Set constraints: (gridPosX, gridPosY, cellSpanX, cellSpanY, weightX, weightY, anchor, fill, insets, iPadX, iPadY)
gridBagLayout1.SetConstraints(button1, new GridBagConstraints(0, 0, 1, 1, 1, 1, AnchorTypes.Center, FillType.Both, new Insets(0, 0, 0, 0), 0, 0, false));
gridBagLayout1.SetConstraints(button2, new GridBagConstraints(1, 0, 1, 1, 1, 1, AnchorTypes.Center, FillType.Both, new Insets(0, 0, 0, 0), 0, 0, false));
gridBagLayout1.SetConstraints(button3, new GridBagConstraints(0, 1, 1, 1, 1, 1, AnchorTypes.Center, FillType.Both, new Insets(0, 0, 0, 0), 0, 0, false));
gridBagLayout1.SetConstraints(button4, new GridBagConstraints(1, 1, 1, 1, 1, 1, AnchorTypes.Center, FillType.Both, new Insets(0, 0, 0, 0), 0, 0, false));gridBagLayout1.SetConstraints(control, new GridBagConstraints(x, y, 1, 1, 1, 1, AnchorTypes.Center, FillType.Both, ...));// Button spanning 3 columns
gridBagLayout1.SetConstraints(button1, new GridBagConstraints(0, 0, 3, 1, 1, 1, AnchorTypes.Center, FillType.Horizontal, ...));gridBagLayout1.SetConstraints(button1, new GridBagConstraints(0, 0, 1, 1, 2, 1, AnchorTypes.Center, FillType.Both, ...)); // Column 0 gets 2x space
gridBagLayout1.SetConstraints(button2, new GridBagConstraints(1, 0, 1, 1, 1, 1, AnchorTypes.Center, FillType.Both, ...)); // Column 1 gets 1x spacegridBagLayout1.SetConstraints(button1, new GridBagConstraints(0, 0, 1, 1, 1, 1, AnchorTypes.NorthEast, FillType.None, ...));| Property | Purpose | Common Values |
|---|---|---|
| GridPostX | Column position | 0, 1, 2, ... |
| GridPostY | Row position | 0, 1, 2, ... |
| CellSpanX | Columns to span | 1, 2, 3, ... |
| CellSpanY | Rows to span | 1, 2, 3, ... |
| WeightX | Horizontal space weight | 0, 1, 2, or custom |
| WeightY | Vertical space weight | 0, 1, 2, or custom |
| Anchor | Alignment within cell | Center, North, East, South, NorthEast, etc. |
| Fill | Resizing behavior | None, Both, Horizontal, Vertical |
| Insets | Padding around control | new Insets(top, left, bottom, right) |
| IPadX | Width padding | 0-100+ |
| IPadY | Height padding | 0-100+ |