mcpField
S
syncfusion-winforms-grid-bag-layout

syncfusion-winforms-grid-bag-layout

github.com/syncfusionDesign & UI279 installsUpdated 1w ago
Repository

About syncfusion-winforms-grid-bag-layout

A skill guide for Windows Forms developers using Syncfusion GridBagLayout to arrange controls in a virtual grid through constraints. It covers cell spanning, weighted space allocation, alignment, and fill behavior.

Key features

  1. 01Place controls at grid coordinates
  2. 02Span controls across rows or columns
  3. 03Distribute extra space by weight
  4. 04Set control alignment and fill behavior
  5. 05Configure spacing and insets

Use cases

  1. 01Build a calculator button grid
  2. 02Arrange wizard navigation buttons
  3. 03Create dialog button rows with varied sizing
  4. 04Lay out complex forms and spanning controls

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/syncfusion/winforms-ui-components-skills --skill syncfusion-winforms-grid-bag-layout

After installing, type/syncfusion-winforms-grid-bag-layoutin Claude Code to confirm it loaded.

Bundled files

References and scripts installed along with SKILL.md.

references/
anchoring-and-fill.md
child-control-constraints.md
getting-started.md
grid-positioning.md
sizing-and-spacing.md

Details

From the project README

Implementing GridBagLayout in Windows Forms

When to Use This Skill

Use this skill when you need to:

  • Arrange multiple child controls in a flexible grid layout
  • Create responsive layouts that span multiple rows and columns
  • Configure control positioning, sizing, and alignment
  • Manage complex control layouts beyond simple grid or flow arrangements
  • Set up constraints for child controls with precise positioning
  • Create layouts like wizard navigation buttons or calculator button grids

GridBagLayout is ideal for layouts where:

  • Controls need variable row/column sizing
  • Controls must span multiple cells
  • You need fine-grained control over spacing and alignment
  • Complex nested layouts are required

Component Overview

GridBagLayout is a layout manager that arranges child controls in a virtual grid where:

  • Rows and columns have variable sizes (unlike GridLayout)
  • Individual controls can span multiple cells
  • Each control's position and sizing is determined by constraints
  • Extra space is distributed based on weight properties
Key Features
  • Grid Positioning: Place controls at specific grid coordinates
  • Cell Spanning: Allow controls to occupy multiple rows/columns
  • Weight Distribution: Control how extra space is allocated
  • Anchor & Fill: Position and resize controls within their allocated space
  • Spacing Control: Add padding and insets around controls

Documentation Guide

Getting Started

📄 Read: references/getting-started.md

  • Assembly deployment and NuGet packages
  • Creating a Windows Forms project with GridBagLayout
  • Adding controls through Designer
  • Adding controls programmatically in C# and VB.NET
Grid Positioning & Layout Structure

📄 Read: references/grid-positioning.md

  • Understanding virtual grid concept
  • GridPostX and GridPostY properties
  • Setting grid coordinates for controls
  • Overlapping controls in same cells
Sizing & Space Distribution

📄 Read: references/sizing-and-spacing.md

  • WeightX and WeightY properties
  • How weight affects space distribution
  • Weight-to-column/row mapping
  • GetLayoutWeights() method usage
Anchoring & Fill Behavior

📄 Read: references/anchoring-and-fill.md

  • Anchor property and justification options
  • Fill property (None, Both, Horizontal, Vertical)
  • Insets for padding around controls
  • IPadX and IPadY for preferred size adjustment
Child Control Constraints

📄 Read: references/child-control-constraints.md

  • CellSpanX and CellSpanY for multi-cell controls
  • GridBagConstraints object structure
  • SetConstraints() method for programmatic configuration
  • GetConstraints() and GetConstraintsRef() methods
  • Designer vs code-based constraint setup

Quick Start Example

Here's a minimal example creating a 2×2 button grid:

// 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));

Common Patterns

Pattern 1: Equal-Sized Grid

Set all controls to equal weights and Fill.Both for uniform distribution:

gridBagLayout1.SetConstraints(control, new GridBagConstraints(x, y, 1, 1, 1, 1, AnchorTypes.Center, FillType.Both, ...));
Pattern 2: Control Spanning Multiple Columns

Use CellSpanX to stretch a control across columns:

// Button spanning 3 columns
gridBagLayout1.SetConstraints(button1, new GridBagConstraints(0, 0, 3, 1, 1, 1, AnchorTypes.Center, FillType.Horizontal, ...));
Pattern 3: Asymmetric Space Distribution

Use different weight values to allocate more space to specific columns/rows:

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 space
Pattern 4: Aligned Controls with No Fill

Use Anchor property with Fill.None to position controls without resizing:

gridBagLayout1.SetConstraints(button1, new GridBagConstraints(0, 0, 1, 1, 1, 1, AnchorTypes.NorthEast, FillType.None, ...));

Key Constraint Properties

PropertyPurposeCommon Values
GridPostXColumn position0, 1, 2, ...
GridPostYRow position0, 1, 2, ...
CellSpanXColumns to span1, 2, 3, ...
CellSpanYRows to span1, 2, 3, ...
WeightXHorizontal space weight0, 1, 2, or custom
WeightYVertical space weight0, 1, 2, or custom
AnchorAlignment within cellCenter, North, East, South, NorthEast, etc.
FillResizing behaviorNone, Both, Horizontal, Vertical
InsetsPadding around controlnew Insets(top, left, bottom, right)
IPadXWidth padding0-100+
IPadYHeight padding0-100+

Common Use Cases

Wizard Button Navigation

  • Multiple buttons arranged horizontally at bottom
  • Use CellSpanX and equal weights for uniform distribution

Calculator Button Grid

  • 4×4 grid of numeric buttons
  • 1×1 constraints with equal weights and Fill.Both

Dialog Button Panel

  • OK, Cancel, Help buttons in a row
  • Different sizes using Anchor and Insets

Complex Form Layouts

  • Labels, text boxes, and buttons in organized grid
  • Use CellSpanX for full-width controls
  • Asymmetric weights for proportional sizing

Comments

Markdown supported