TCoreGridPanel
A responsive grid panel container using CSS Grid. Supports breakpoints for different layouts at different viewport widths with automatic mobile stacking.
Overview
TCoreGridPanel provides a flexible grid layout system that adapts to different screen sizes. It uses CSS Grid at runtime for optimal performance and responsive behaviour. At design time, it displays cells with color-coding and position indicators.
Unit & Inheritance
UNIT Obj.Container.GridPanel;
TCoreGridPanel = CLASS(TCoreDiv)
// Inherits from TCoreDiv for CSS class support
Key Concepts
Rows and Columns
The grid is defined by RowCollection and ColumnCollection. Each row/column can specify:
- SizeStyle - ssPercent (proportional) or ssAbsolute (fixed pixels)
- Value - The size value (percentage or pixels)
- Alignment - How content is aligned within cells
- Margins - Spacing around cells
Cells
Cells are automatically created as TSubCoreDiv components based on the grid dimensions. Access cells using GetCell(ColIndex, RowIndex). Drop controls onto cells at design time - they become children of that cell.
Responsive Layouts
The Layout collection defines breakpoints for different viewport widths. Each layout specifies a CSS grid-template-columns value that determines how many columns appear at that breakpoint.
Published Properties
| Property | Type | Description |
|---|---|---|
| ColumnCollection | TCoreGridPanelColumns |
Collection of column definitions with size, alignment, and margins. |
| RowCollection | TCoreGridPanelRows |
Collection of row definitions with size, alignment, and margins. |
| Layout | TResponsiveLayoutCollection |
Collection of responsive breakpoints defining grid layouts at different viewport widths. |
| GridLineWidth | Integer |
Width of grid lines between cells (0 = no lines, just dotted guides at design time). |
| GridLineColor | TColor |
Color of grid lines when GridLineWidth > 0. Default: clSilver. |
| OnLayoutChange | TLayoutChangeEvent |
Fired when the active responsive layout changes due to viewport resize. |
Column/Row Collection Item Properties
TCoreGridPanelColumn Properties
| Property | Type | Default | Description |
|---|---|---|---|
| SizeStyle | TSizeStyle |
ssPercent | ssPercent = proportional sizing, ssAbsolute = fixed pixel width. |
| Value | Integer |
50 | The size value - percentage (0-100) or pixels depending on SizeStyle. |
| Alignment | TAlignment |
taCenter | Horizontal alignment of content: taLeftJustify, taCenter, taRightJustify. |
| MarginLeft | Integer |
0 | Left margin in pixels for cells in this column. |
| MarginRight | Integer |
0 | Right margin in pixels for cells in this column. |
TCoreGridPanelRow Properties
| Property | Type | Default | Description |
|---|---|---|---|
| SizeStyle | TSizeStyle |
ssPercent | ssPercent = proportional sizing, ssAbsolute = fixed pixel height. |
| Value | Integer |
50 | The size value - percentage or pixels. |
| Alignment | TVerticalCellAlignment |
vcaCenter | Vertical alignment: vcaTop, vcaCenter, vcaBottom. |
| MarginTop | Integer |
0 | Top margin in pixels for cells in this row. |
| MarginBottom | Integer |
0 | Bottom margin in pixels for cells in this row. |
Responsive Layout Item Properties
| Property | Type | Description |
|---|---|---|
| Width | Integer |
Maximum viewport width for this layout (0 = default/largest, no limit). |
| Style | STRING |
CSS grid-template-columns value (e.g. '1fr', '1fr 1fr', '200px 1fr'). |
| ColumnGap | STRING |
Gap between columns (e.g. '10px', '1rem'). |
| RowGap | STRING |
Gap between rows (e.g. '10px', '1rem'). |
| Description | STRING |
Description for IDE display (e.g. 'Mobile', 'Tablet', 'Desktop'). |
Public Methods
Returns the cell component at the specified column and row index. Returns nil if indices are out of range.
Returns the total number of cells in the grid (Rows × Columns).
Re-aligns all child controls within their cells according to the column/row alignment settings. Called automatically on resize.
Default Layouts
When you drop a TCoreGridPanel on a form, it creates three default responsive layouts:
| Description | Width | Style | Behaviour |
|---|---|---|---|
| Mobile | 576 | 1fr |
Single column, cells stack vertically |
| Tablet | 768 | 1fr 1fr |
Two equal columns |
| Desktop | 0 | 1fr 1fr 1fr 1fr 1fr |
Five equal columns (default) |
Design-Time Behaviour
At design time:
- Cells are color-coded with alternating pastel colors for visibility
- Cell indices [col,row] are displayed in the top-left corner of each cell
- Dotted grid lines show cell boundaries (or solid lines if GridLineWidth > 0)
- A drag handle appears at top-left for dragging the entire panel
- Drop controls onto cells to make them children of that cell
At runtime, cell colors become transparent and indices are hidden.
Usage Examples
Basic Setup - 2x2 Grid
// In Form Designer or code:
GridPanel1.RowCollection.Clear;
GridPanel1.ColumnCollection.Clear;
// Add 2 rows
GridPanel1.RowCollection.Add.Value := 50; // 50%
GridPanel1.RowCollection.Add.Value := 50; // 50%
// Add 2 columns
GridPanel1.ColumnCollection.Add.Value := 50; // 50%
GridPanel1.ColumnCollection.Add.Value := 50; // 50%
Accessing Cells
VAR
Cell: TSubCoreDiv;
BEGIN
// Get cell at column 1, row 0
Cell := GridPanel1.GetCell(1, 0);
IF Assigned(Cell) THEN
BEGIN
// Add a label to the cell
MyLabel.Parent := Cell;
END;
END;
Custom Responsive Layouts
// Clear default layouts
GridPanel1.Layout.Clear;
// Mobile: single column
WITH GridPanel1.Layout.Add DO
BEGIN
Width := 480;
Style := '1fr';
ColumnGap := '8px';
RowGap := '8px';
Description := 'Mobile';
END;
// Tablet: 2 columns with fixed sidebar
WITH GridPanel1.Layout.Add DO
BEGIN
Width := 1024;
Style := '250px 1fr';
ColumnGap := '16px';
RowGap := '16px';
Description := 'Tablet';
END;
// Desktop: 3 columns
WITH GridPanel1.Layout.Add DO
BEGIN
Width := 0; // Default (largest)
Style := '250px 1fr 300px';
ColumnGap := '20px';
RowGap := '20px';
Description := 'Desktop';
END;
Responding to Layout Changes
PROCEDURE TMainForm.GridPanel1LayoutChange(Sender: TObject;
ALayout: TResponsiveLayoutItem);
BEGIN
IF ALayout.Description = 'Mobile' THEN
BEGIN
// Hide sidebar content on mobile
SidebarPanel.Visible := False;
END
ELSE
BEGIN
SidebarPanel.Visible := True;
END;
END;
Fixed + Proportional Columns
// Create a layout with fixed sidebar and flexible content
GridPanel1.ColumnCollection.Clear;
// Fixed 200px sidebar
WITH GridPanel1.ColumnCollection.Add DO
BEGIN
SizeStyle := ssAbsolute;
Value := 200;
END;
// Flexible content area (takes remaining space)
WITH GridPanel1.ColumnCollection.Add DO
BEGIN
SizeStyle := ssPercent;
Value := 100;
END;
CSS Classes
TCoreGridPanel applies the CSS class core-grid-panel to itself. Cells can be styled via their ElementClassName property.
Cell Styling
Cells are TSubCoreDiv components. By default they use flex layout with alignment based on column/row settings. To override:
- Set the cell's
ElementClassNameto apply custom CSS - Set the cell's
ChildLayoutto override the default flex layout
See Also
- TMultiContainer - Page layout container
- TCoreDiv - Base component class
- TDom - DOM access utilities
- TDashboardCanvas - Dashboard card layout