Class GridBagLayout
Represents the LayoutManager that performs GridBagLayout.
Implements
Inherited Members
Namespace: Syncfusion.Windows.Forms.Tools
Assembly: Syncfusion.Shared.Base.dll
Syntax
public class GridBagLayout : LayoutManager, IExtenderProvider, ILayoutManager, ISupportInitialize
Remarks
The GridBagLayout manager creates and manages a grid within the container's layout bounds. Each column and row in this grid can be of different sizes and a component can span more than one cell.
Each component can be assigned weights to determine how to distribute extra space between components. A component can also be anchored to a border / corner of its virtual grid cell. It can also fill its cell.
Note that if you want the child components to be laid out purely based on their weights, set their preferred sizes to be (0,0), specify a non-zero weightX and weigthY, and set their Fill mode to FillMode.Both.
The GridBagConstraints structure specifies all the above constraints associated with a component. Use the SetConstraints method to set the constraints for each component. To exclude a component from layout, call SetConstraints with NULL constraints. Each component has a constraint, which means that there is potential for conflicts in size and boundaries between components. Make sure to manually test the layout design to determine that it behaves appropriately.
Examples
The following example shows you how to initialize a GridBagLayout manager with a container control and its children:
// Binding a control to the GridBagLayout manager programmatically:
this.gridBagLayout1 = new GridBagLayout();
// Set the container control; all the child controls of this container control are
// automatically registered as children with the manager:
this.gridBagLayout1.ContainerControl = this.panel1;
this.gridBagLayout1.SetConstraints(
this.button1,
new GridBagConstraints(0, 0, 3, 1, 1, 0.2, AnchorTypes.Center, FillType.Both, new Insets(0, 0, 0, 0), 0, 0, false)
);
this.gridBagLayout1.SetConstraints(
this.button2,
new GridBagConstraints(0, 1, 1, 3, 0.2, 0.6, AnchorTypes.Center, FillType.Both, new Insets(0, 0, 0, 0), 0, 0, false)
);
// Exclude button3 from layout:
this.gridBagLayout1.SetConstraints(this.button3, GridBagConstraints.Empty);
// Modify an exisiting constraint:
GridBagConstraints constraints1 = this.gridBagLayout1.GetConstraintsRef(this.button1);
constraints1.Fill = FillType.Horizontal;
// You can prevent automatic layout during the layout event.
// If you decide to do so, make sure to call gridBagLayout1.LayoutContainer manually:
// this.gridBagLayout1.AutoLayout = false;
Also take a look at the project in Tools/Samples/Quick Start/LayoutManager for an example.
Constructors
GridBagLayout()
Overloaded. Creates a new instance of the GridBagLayout class and sets its defaults.
Declaration
public GridBagLayout()
GridBagLayout(IContainer)
Creates a new instance of the GridBagLayout class and adds itself to the specified container.
Declaration
public GridBagLayout(IContainer container)
Parameters
| Type | Name | Description |
|---|---|---|
| System.ComponentModel.IContainer | container | The logical ContainerControl parent into which to add itself. |
Remarks
This constructor is used by the design-time to add a component to the form's IContainer field so that it gets Disposed when the form gets Disposed.
Note that this is not the same as the LayoutManager 's ContainerControl.
GridBagLayout(Control)
Creates a new instance of the GridBagLayout class and sets its ContainerControl.
Declaration
public GridBagLayout(Control container)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Windows.Forms.Control | container | The logical ContainerControl parent into which to add itself. |
Fields
columnWeights
Largest weight in each column.
Declaration
public double[] columnWeights
Field Value
| Type |
|---|
| System.Double[] |
columnWidths
Largest minimum width in each column.
Declaration
public int[] columnWidths
Field Value
| Type |
|---|
| System.Int32[] |
MaxGridSize
The maximum number of rows or columns allowed in the grid layout.
Declaration
public const int MaxGridSize = 512
Field Value
| Type |
|---|
| System.Int32 |
MinimumSize
Indicates that the minimum size should be used for layout calculations.
Declaration
protected static int MinimumSize
Field Value
| Type |
|---|
| System.Int32 |
MinSize
The minimum size allowed for layout components.
Declaration
public const int MinSize = 1
Field Value
| Type |
|---|
| System.Int32 |
noFillSizes
Stores the sizes of controls that should not be resized or filled by the layout engine.
Declaration
protected Hashtable noFillSizes
Field Value
| Type |
|---|
| System.Collections.Hashtable |
PreferredSize
Indicates that the preferred size should be used for layout calculations.
Declaration
protected static int PreferredSize
Field Value
| Type |
|---|
| System.Int32 |
rowHeights
Largest minimum height in each row.
Declaration
public int[] rowHeights
Field Value
| Type |
|---|
| System.Int32[] |
rowWeights
Largest weight in each row.
Declaration
public double[] rowWeights
Field Value
| Type |
|---|
| System.Double[] |
Methods
AddLayoutComponent(Control, Object)
Adds a component to the child layout list.
Declaration
public override void AddLayoutComponent(Control control, object constraints)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Windows.Forms.Control | control | The control to add to the layout list. |
| System.Object | constraints | The GridBagConstraints associated with this component. |
Overrides
Remarks
If the second argument is not a GridBagConstraints , an exception will be thrown.
AdjustForGravity(GridBagConstraints, ref Rectangle)
Adjusts the bounds of a control based on its gravity and fill constraints.
Declaration
protected void AdjustForGravity(GridBagConstraints constraints, ref Rectangle r)
Parameters
| Type | Name | Description |
|---|---|---|
| GridBagConstraints | constraints | The layout constraints associated with the control. |
| System.Drawing.Rectangle | r | A reference to the rectangle representing the control's bounds, which will be modified. |
GetConstraints(Control)
Returns the constraints associated with the specified control.
Declaration
public GridBagConstraints GetConstraints(Control control)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Windows.Forms.Control | control | The control whose constraints to retrieve. |
Returns
| Type | Description |
|---|---|
| GridBagConstraints | A clone of the stored constraints object. |
Remarks
The returned value is a clone which can be used independently by itself. The changes made to the returned instance will not have any effect on the stored constraints. Use GetConstraintsRef(Control) to get hold of the actual constraints object that is used by the manager.
GetConstraintsRef(Control)
Returns a reference to the constraints associated with the specified control.
Declaration
public GridBagConstraints GetConstraintsRef(Control control)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Windows.Forms.Control | control | The control with constraints to retrieve. |
Returns
| Type | Description |
|---|---|
| GridBagConstraints | A reference to the actual constraints object. |
Remarks
This is the actual object where the manager stores the constraints for the control. Hence, making changes to the returned object will affect the layout logic.
GetLayoutDimensions()
Returns the row and column dimensions of the current layout.
Declaration
[CLSCompliant(false)]
public int[][] GetLayoutDimensions()
Returns
| Type | Description |
|---|---|
| System.Int32[][] | A 2D integer array containing the dimensions. |
GetLayoutInfo(Int32)
Calculates and returns layout information for the grid bag layout based on the specified size flag.
Declaration
protected GridBagLayoutInfo GetLayoutInfo(int sizeflag)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int32 | sizeflag |
Returns
| Type |
|---|
| GridBagLayoutInfo |
GetLayoutOrigin()
Returns the top-left origin of the virtual grid in the current layout.
Declaration
public Point GetLayoutOrigin()
Returns
| Type | Description |
|---|---|
| System.Drawing.Point | A point representing the top-left position. |
GetLayoutWeights()
Returns the row and column weights of the current layout.
Declaration
[CLSCompliant(false)]
public double[][] GetLayoutWeights()
Returns
| Type | Description |
|---|---|
| System.Double[][] | A 2D integer array containing the weights. |
GetLocation(Int32, Int32)
Returns the cell in the virtual grid (as a point) given a location.
Declaration
public Point GetLocation(int x, int y)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int32 | x | The x coordinate of the location. |
| System.Int32 | y | The y coordinate of the location. |
Returns
| Type | Description |
|---|---|
| System.Drawing.Point | A Point representing the virtual grid cell. |
GetMinSize(GridBagLayoutInfo)
Computes the minimum size required to accommodate all components in the layout.
Declaration
protected Size GetMinSize(GridBagLayoutInfo info)
Parameters
| Type | Name | Description |
|---|---|---|
| GridBagLayoutInfo | info | The layout information containing row and column sizes. |
Returns
| Type |
|---|
| System.Drawing.Size |
GetStaticMinimumSize(Control)
Declaration
protected override Size GetStaticMinimumSize(Control control)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Windows.Forms.Control | control |
Returns
| Type |
|---|
| System.Drawing.Size |
Overrides
GetStaticPreferredSize(Control)
Declaration
protected override Size GetStaticPreferredSize(Control control)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Windows.Forms.Control | control |
Returns
| Type |
|---|
| System.Drawing.Size |
Overrides
LayoutContainer()
Overridden. See LayoutContainer().
Declaration
public override void LayoutContainer()
Overrides
LayoutGridBag()
Performs the layout of components using the grid bag algorithm.
Declaration
protected void LayoutGridBag()
MinimumLayoutSize()
Overridden. See MinimumLayoutSize().
Declaration
public override Size MinimumLayoutSize()
Returns
| Type | Description |
|---|---|
| System.Drawing.Size | Returns the MinimumSize of the ContainerControl. |
Overrides
OnControlAdded(Object, ControlEventArgs)
Declaration
protected override void OnControlAdded(object sender, ControlEventArgs e)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Object | sender | |
| System.Windows.Forms.ControlEventArgs | e |
Overrides
PreferredLayoutSize()
Overridden. See PreferredLayoutSize().
Declaration
public override Size PreferredLayoutSize()
Returns
| Type | Description |
|---|---|
| System.Drawing.Size | Reutrns the preferred size of the Container control. |
Overrides
RemoveLayoutComponent(Control)
Overridden. See RemoveLayoutComponent(Control).
Declaration
public override void RemoveLayoutComponent(Control control)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Windows.Forms.Control | control | The control to be removed. |
Overrides
ResetConstraints(Control)
Resets the layout constraints for the specified control to the default GridBagConstraints.
Declaration
protected void ResetConstraints(Control control)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Windows.Forms.Control | control | The control whose layout constraints are to be reset. |
ResetLayoutInfo()
Overridden. See ResetLayoutInfo().
Declaration
protected override void ResetLayoutInfo()
Overrides
ResetMinimumSize(Control)
Overridden. See ResetMinimumSize(Control).
Declaration
public override void ResetMinimumSize(Control control)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Windows.Forms.Control | control | The Control which MinimumSize to be reset. |
Overrides
ResetPreferredSize(Control)
Overridden. See ResetPreferredSize(Control).
Declaration
public override void ResetPreferredSize(Control control)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Windows.Forms.Control | control | The control which PreferredSize to be reset. |
Overrides
SetConstraints(Control, GridBagConstraints)
Specifies the constraints associated with the specified control.
Declaration
public void SetConstraints(Control control, GridBagConstraints value)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Windows.Forms.Control | control | The control for which to set the constraints. |
| GridBagConstraints | value | The constraints of the control. Or NULL to remove the control from the layout list. |
Remarks
Passing a NULL value will actually remove the component from the layout list.
SetPreferredSize(Control, Size)
Overridden. See SetPreferredSize(Control, Size).
Declaration
public override void SetPreferredSize(Control control, Size value)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Windows.Forms.Control | control | The control to associate the preferred size with. |
| System.Drawing.Size | value | The preferred size of the control. |
Overrides
ShouldSerializeConstraints(Control)
Determines whether the layout constraints for the specified control should be serialized.
Declaration
protected bool ShouldSerializeConstraints(Control control)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Windows.Forms.Control | control | The control whose layout constraints are being evaluated. |
Returns
| Type |
|---|
| System.Boolean |
ShouldSerializeMinimumSize(Control)
Indicates whether to serialize the MinimumSize property.
Declaration
public override bool ShouldSerializeMinimumSize(Control control)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Windows.Forms.Control | control |
Returns
| Type |
|---|
| System.Boolean |
Overrides
ShouldSerializePreferredSize(Control)
Indicates whether to serialize the PreferredSize property.
Declaration
public override bool ShouldSerializePreferredSize(Control control)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Windows.Forms.Control | control |
Returns
| Type |
|---|
| System.Boolean |