New to Telerik UI for WinFormsStart a free 30-day trial

Tutorial: Saving and Loading Layout and Content

Updated over 6 months ago

As stated in this documentation article RadDock can save and then load the layout of its DockWindows. However, this mechanism does not save the content of these windows. The purpose of this article is to demonstrate what approach you should follow when you want to restore not only the layout of the DockWindows, but their content as well.

Example: Saving and Loading layout and content

1. Usually, each DockWindow contains a set of controls. To make the task of saving and loading the content easier, let's put this content in UserControls.

Figure 1: Three UserControls containing different controls.

WinForms RadDock First UserControl

WinForms RadDock Second UserControl

WinForms RadDock Third UserControl

2. Then, let's define the path to the xml file where we will save our layout:

C#
private string dockLayoutPath = Application.StartupPath + "\\dock.xml";

3. At the Load event of our form we will check if the xml file with the saved layout exists. If the file exists at the specified location, we will load the layout as shown in the next paragraphs. If it does not exists, we will create a generic layout in RadDock loading our user controls:

C#
private void MainForm_Load(object sender, EventArgs e)
{
    if (!File.Exists(dockLayoutPath))
    {
        InitializeLayout();
    }
    else
    {
        this.radDock1.LoadFromXml(dockLayoutPath);
        LoadContent();
    }
}
private void InitializeLayout()
{
    this.radDock1.MainDocumentContainerVisible = false;
    HostWindow afW = this.radDock1.DockControl(new AvailableFlights(), DockPosition.Left);
    afW.Text = "Available Flights";
    HostWindow bfW = this.radDock1.DockControl(new BookFlight(), DockPosition.Left);
    bfW.Text = "Book a Flight";
    HostWindow fsW = this.radDock1.DockControl(new FlightsSummary(), DockPosition.Left);
    fsW.Text = "Flight Summary";
}

Please note that the names of the types of the UserControls are important, because these names will actually give the names of the HostWindows that will be created to host the UserControls. The names of the HostWindows will be later used during the process of loading the content.

WinForms RadDock Three UserControls Inside HostWindows

4. Let's now arrange the dock window in a more user friendly way:

WinForms RadDock Arrange Dock Window

5. Now close the form containing RadDock. The FormClosing event handler is a convenient place to save our layout:

C#
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
    this.radDock1.SaveToXml(dockLayoutPath);
}

6. Reopen the form. Since the XML file defined at paragraph two now exists, our layout and content will be loaded with the help of the following methods:

  • LoadFromXml: This method will create HostWindows and will arrange them according to the information saved in the XML file.

  • LoadContent: This is our custom method which loads the content in the created HostWindows. Note that the different user controls are loaded in the appropriate HostWindows depending on the names of these windows.

C#
private void LoadContent()
{
    for (int i = 0; i < this.radDock1.DockWindows.Count; i++)
    {
        HostWindow hw = this.radDock1.DockWindows[i] as HostWindow;
        if (hw != null)
        {
            if (hw.Name.StartsWith("Available"))
            {
                hw.LoadContent(new AvailableFlights());
                hw.Text = "Available Flights";
            }
            if (hw.Name.StartsWith("Book"))
            {
                hw.LoadContent(new BookFlight());
                hw.Text = "Book a Flight";
            }
            if (hw.Name.StartsWith("Flight"))
            {
                hw.LoadContent(new FlightsSummary());
                hw.Text = "Flight Summary";
            }
        }
    }
}

As a result we get the layout shown on the screenshot below:

WinForms RadDock Save Load Layouts

RELATED VIDEOS
Saving and Loading RadDock for WinForms Layouts In this video, you will learn how to use the simple XML serialization features of RadDock for WinForms to easily save and load RadDock layouts. (Runtime: 07:03)WinForms RadDock Save Load Layouts Tutorial

See Also