Getting Started with Windows Forms LayoutManagers
8 Oct 2020 / 3 minutes to read
This section explains how to add the
GridBagLayout control in a Windows Forms application and overview its basic functionalities.
Refer to the control dependencies section to get the list of assemblies or NuGet package needs to be added as reference to use the control in any application.
Find more details about installing the nuget packages in a Windows Forms application in the following link: How to install nuget packages.
Creating the project
Create a new Windows Forms project in Visual Studio to display the GridBagLayout with basic functionalities.
GridBagLayout control can be added to an application by dragging it from the toolbox to designer view.
The following required assembly reference will be added automatically.
A pop-up will appear automatically to add the form as a container control of the
Adding Layout components through designer
The child controls can be added to the layout by dragging it from the toolbox to designer view.
To add the control manually in C#, follow the given steps:
Step 1: Create a C# or VB application through Visual Studio.
Step 2: Add the following required assembly reference to the project:
Step 3: Include the required namespace.
Step 4: Create a
GridBagLayout control instance, and set
ContainerControl as form.
GridBagLayout gridBagLayout1 = new GridBagLayout(); this.gridBagLayout1.ContainerControl = this;
Dim gridBagLayout1 As GridBagLayout = New GridBagLayout() Me.gridBagLayout1.ContainerControl = Me
Adding layout components through code
The child controls can be added to the layout by simply adding it to the form since the form is its container control.
ButtonAdv buttonAdv1 = new ButtonAdv(); ButtonAdv buttonAdv2 = new ButtonAdv(); ButtonAdv buttonAdv3 = new ButtonAdv(); this.buttonAdv1.Text = "buttonAdv1"; this.buttonAdv2.Text = "buttonAdv2"; this.buttonAdv3.Text = "buttonAdv3"; this.Controls.Add(this.buttonAdv1); this.Controls.Add(this.buttonAdv2); this.Controls.Add(this.buttonAdv3);
Dim buttonAdv1 As ButtonAdv = New ButtonAdv() Dim buttonAdv3 As ButtonAdv = New ButtonAdv() Dim buttonAdv3 As ButtonAdv = New ButtonAdv() Me.buttonAdv1.Text = "buttonAdv1" Me.buttonAdv2.Text = "buttonAdv2" Me.buttonAdv3.Text = "buttonAdv3" Me.Controls.Add(this.buttonAdv1) Me.Controls.Add(this.buttonAdv2) Me.Controls.Add(this.buttonAdv3)