ComponentOne Sizer for WinForms
In This Topic
    Store and Load
    In This Topic

    Sizer provides built-in options to load and save the grid layout. Let us explore these options in the following sections.

    Store Layout

    Sizer lets you store the grid layout in the xml format or save it as backup of the layout. It not only lets you save the overall grid layout, but also allows you to save the cell location of each control positioned within the grid layout. 

    You can use the GridDefinition property to store layout information for the Sizer control. The GridDefinition property gets or sets a string containing the grid information.

    The following code snippet demonstrates how to store the grid layout information in xml string using the GridDefinition property.

    C#
    Copy Code
    List<ControlInfo> _ctrlBounds;
    LayoutInfo layoutInfo = new LayoutInfo();
    //save the grid definition
    layoutInfo.GridDefinition = c1Sizer1.GridDefinition;
                
    //save cell location of each sizer control
    _ctrlBounds = new List<ControlInfo>();
    foreach (Control ctl in c1Sizer1.Controls)
    {
           _ctrlBounds.Add(new ControlInfo(ctl.Name, c1Sizer1.GetCellAtPoint(ctl.Location)));
    }
    layoutInfo.ControlInfos = _ctrlBounds;
    
    //serialize the LayoutInfo object to xml
    XmlSerializer serializer = new XmlSerializer(typeof(LayoutInfo));
    using(var writer = new XmlTextWriter("layout.xml", System.Text.Encoding.UTF8))
          serializer.Serialize(writer, layoutInfo);
    

    Back to Top 

    Load Layout

    With Sizer, you can easily load the stored layouts. Loading the stored layout also helps in reducing your time and effort in re-creating the same structure.

    You can easily load a layout by deserializing the data from the xml file, and then setting the grid definition of the Sizer control using the GridDefinition property. Please note that at first you need to disable auto-resizing of the Sizer using AutoSizeMode property of the C1Sizer class before loading the new layout. Then, you can enable the auto-sizing again after loading the layout. This ensures that resizing takes place according to the new layout. 

    The following code snippet demonstrates how to load the layout stored in the Store Layout section using the GridDefinition property.

    C#
    Copy Code
    List<ControlInfo> _ctrlBounds;
    string filePath = "layout.xml";
    if (!File.Exists(filePath))
        return;
    XmlSerializer serializer = new XmlSerializer(typeof(LayoutInfo));
                
    using (var reader = new XmlTextReader(filePath))
    {
           //deserialize data from file to LayoutInfo object
           LayoutInfo layoutInfo = (LayoutInfo)serializer.Deserialize(reader);
           //turn off sizing
           c1Sizer1.AutoSizeMode = AutoSizeEnum.None;
           //clear currernt grid layout
           c1Sizer1.Grid.Clear();
           //set the gridDefinition
           c1Sizer1.GridDefinition = layoutInfo.GridDefinition;
    
           //set each control to its saved cell location
           _ctrlBounds = layoutInfo.ControlInfos;
           foreach (ControlInfo cbi in _ctrlBounds)
           {
                   Control ctl = c1Sizer1.Controls[cbi.Name];
                   var bounds = c1Sizer1.GetCellBounds(cbi.ColRow.Y, cbi.ColRow.X);
                   bounds.Inflate(-2, -2);
                   ctl.Bounds = bounds;
           }
           //turn autosizing back on
           c1Sizer1.AutoSizeMode = AutoSizeEnum.Grid;
    }
    

    Back to Top