How to add a layout component to a form in WinForms GridBagLayout?
Add layout component to a form
Using the designer:
Just drag-and-drop the appropriate layout manager component onto your form. You can optionally drop the component on the container that you want to layout and the design-time will automatically set the ContainerControl property in the layout manager.
You can then configure the layout manager by changing its properties via the designer’s property grid. The layout manager will also provide some extended properties on the child controls that you can set via the control’s property grid.
This example shows how to initialize a GridBagLayout programmatically. Take a look at the class reference for the other layout managers, for example, the code on how to initialize them programmatically.
// Binding a Control to the GridBagLayout manager programaitcally this.gridBagLayout1 = new GridBagLayout(); // Set the container control; Doing this will automatically register all the child controls of this container control 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;
' Binding a Control to the GridBagLayout manager programaitcally Me.gridBagLayout1 = New GridBagLayout() ' Set the container control; Doing this will automatically register all the child controls of this container control with the manager. Me.gridBagLayout1.ContainerControl = Me.panel1 Me.gridBagLayout1.SetConstraints(Me.button1, GridBagConstraints(0,0,3,1,1,0.2,AnchorTypes.Center,FillType.Both,New Insets(0,0,0,0),0,0,False)) Me.gridBagLayout1.SetConstraints(Me.button2, 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 Me.gridBagLayout1.SetConstraints(Me.button3, GridBagConstraints.Empty) ' Modify an exisiting constraint. Dim constraints1 As GridBagConstraints = Me.gridBagLayout1.GetConstraintsRef(Me.button1) constraints1.Fill = FillType.Horizontal