Sizer for WinForms
保存とロード

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#
コードのコピー
List<ControlInfo> _ctrlBounds;
LayoutInfo layoutInfo = new LayoutInfo();
//グリッド定義を保存します
layoutInfo.GridDefinition = c1Sizer1.GridDefinition;
            
//各Sizerコントロールのセル位置を保存します
_ctrlBounds = new List<ControlInfo>();
foreach (Control ctl in c1Sizer1.Controls)
{
       _ctrlBounds.Add(new ControlInfo(ctl.Name, c1Sizer1.GetCellAtPoint(ctl.Location)));
}
layoutInfo.ControlInfos = _ctrlBounds;

//LayoutInfo オブジェクトを 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#
コードのコピー
List<ControlInfo> _ctrlBounds;
string filePath = "layout.xml";
if (!File.Exists(filePath))
    return;
XmlSerializer serializer = new XmlSerializer(typeof(LayoutInfo));
            
using (var reader = new XmlTextReader(filePath))
{
       //データをファイルから LayoutInfo オブジェクトに逆シリアル化します
       LayoutInfo layoutInfo = (LayoutInfo)serializer.Deserialize(reader);
       //サイズ変更を無効にします
       c1Sizer1.AutoSizeMode = AutoSizeEnum.None;
       //現在のグリッド レイアウトをクリアします
       c1Sizer1.Grid.Clear();
       //gridDefinition を設定します
       c1Sizer1.GridDefinition = layoutInfo.GridDefinition;

       //各コントロールを保存されたセルの場所に設定します
       _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;
       }
       //自動サイズ調整を有効にします
       c1Sizer1.AutoSizeMode = AutoSizeEnum.Grid;
}

Back to Top