Back to Devexpress

How to: Save the layout between application runs

windowsforms-2313-controls-and-libraries-form-layout-managers-layout-and-data-layout-controls-examples-how-to-save-the-layout-between-application-runs.md

latest2.0 KB
Original Source

How to: Save the layout between application runs

  • Nov 13, 2018

Changes in the layout that are made by end-users at runtime are not automatically saved between application runs. After the LayoutControl is destroyed, for instance, when a form is closed, all the end-user’s changes are lost. The solution that allows you to resolve this problem is to save the layout manually when the application closes and restore the saved layout when the application starts. This example demonstrates how to do this.

In this example, the layout will be stored in an XML file, so the LayoutControl.SaveLayoutToXml and LayoutControl.RestoreLayoutFromXml methods will be used. A list of all the methods for saving and loading the layout can be found in the Member Table: Save and Restore Layout topic.

csharp
string layoutFileName = "layout.xml";
//...
private void Form1_Load(object sender, EventArgs e) {
   if (System.IO.File.Exists(layoutFileName))
      layoutControl1.RestoreLayoutFromXml(layoutFileName);
}

private void Form1_Closing(object sender, FormClosingEventArgs e) {
   layoutControl1.SaveLayoutToXml(layoutFileName);
}
vb
Dim layoutFileName As String = "layout.xml"
' ...
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
  Handles MyBase.Load
   If System.IO.File.Exists(layoutFileName) Then
      LayoutControl1.RestoreLayoutFromXml(layoutFileName)
   End If
End Sub

Private Sub Form1_Closing(ByVal sender As Object, _
  ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
   LayoutControl1.SaveLayoutToXml(layoutFileName)
End Sub