ComponentOne Sizer for WinForms
In This Topic
    Add Controls
    In This Topic

    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.

    Add Controls at design time

    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.

    Add Controls using code

    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.

    1. Subscribe to Form1_Load event from the Properties window.
    2. Switch to the code view and add the following code to the event handler created for Form1_Load event for adding band.
      C#
      Copy Code
      //set rows and columns
      c1Sizer1.Grid.Columns.Count = 3;
      c1Sizer1.Grid.Rows.Count = 3;
      
      For more information on how to add bands using code, see Add Bands section in Bands topic.
    3. Initialize a control and add it to the Sizer control using the following code.
      C#
      Copy Code
      //initialize a new control
      Button btn1 = new Button();
      btn1.Text = "Text";
      c1Sizer1.Controls.Add(btn1);
      
    4. Get the bounds of the cells where you wish to position the control within the grid using GetCellBounds method of the C1Sizer class as demonstrated in the following code snippet.
      C#
      Copy Code
      //get bounds of the cell [1,1]
      var cellBounds = c1Sizer1.GetCellBounds(1, 1, 1, 1);
      
    5. Set the bounds of the control to the bounds of the cell range obtained using the GetCellBounds method, as showcased in the following code.
      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.