With Sizer, you can easily add controls to the form to create a form layout. Each added control can be positioned over a single grid cell or it may span over multiple cells.
Let us discuss how to add controls to the Sizer control at design time and through code.
You can add controls to Sizer at design time without writing a single line of code. For more information on how to add controls at design time, see Create Layout at design time section in the Quick Start topic.
You can add controls to the Sizer and position them within the grid layout through code as well.
Follow the given steps to add controls to Sizer.
C# |
Copy Code
|
---|---|
//set rows and columns
c1Sizer1.Grid.Columns.Count = 3;
c1Sizer1.Grid.Rows.Count = 3;
|
C# |
Copy Code
|
---|---|
//initialize a new control Button btn1 = new Button(); btn1.Text = "Text"; c1Sizer1.Controls.Add(btn1); |
C# |
Copy Code
|
---|---|
//get bounds of the cell [1,1] var cellBounds = c1Sizer1.GetCellBounds(1, 1, 1, 1); |
C# |
Copy Code
|
---|---|
//set the control's bounds same as the cell bounds
btn1.Bounds = cellBounds;
|
This adds the control to the desired location within the grid. You can observe that the Sizer snaps the controls to the specified position on the grid, and automatically resizes the control when the form is resized.