New to Telerik UI for WinForms? Download free 30-day trial

Save and Load Layout

RadCommandBar allows the user to add and remove items and rearrange strip elements on different rows. The Save/Load layout functionality gives your applications the opportunity to preserve user settings concerning position, visibility and orientation.

When you save the layout, all the preferences of a user will be saved in an XML file. When you load the file, the saved layout settings will be applied to RadCommandBar.

Here is an example demonstrating how you can implement a Save Layout button event handler:

Initially the layout looks like this:

WinForms RadCommandBar command-bar-save-and-load-layout 001

private void radButton1_Click(object sender, EventArgs e)
{
    string s = "default.xml";
    SaveFileDialog dialog = new SaveFileDialog();
    dialog.Filter =
       "xml files (*.xml)|*.xml|All files (*.*)|*.*";
    dialog.Title = "Select a xml file";
    if (dialog.ShowDialog() == DialogResult.OK)
    {
        s = dialog.FileName;
    }
    this.radCommandBar1.CommandBarElement.SaveLayout(s);
}

Private Sub RadButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadButton1.Click
    Dim s As String = "default.xml"
    Dim dialog As New SaveFileDialog()
    dialog.Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*"
    dialog.Title = "Select a xml file"
    If dialog.ShowDialog() = DialogResult.OK Then
        s = dialog.FileName
    End If
    Me.RadCommandBar1.CommandBarElement.SaveLayout(s)
End Sub

Now we are going to set some of the items VisibleInStrip properties to falseWinForms RadCommandBar command-bar-save-and-load-layout 002

The code snippets below demonstrate how you can implement a Load Layout button event handler:

private void radButton2_Click(object sender, EventArgs e)
{
    string s = "default.xml";
    OpenFileDialog dialog = new OpenFileDialog();
    dialog.Filter =
       "xml files (*.xml)|*.xml|All files (*.*)|*.*";
    dialog.Title = "Select a xml file";
    if (dialog.ShowDialog() == DialogResult.OK)
    {
        s = dialog.FileName;
    }
    this.radCommandBar1.CommandBarElement.LoadLayout(s);
}

Private Sub RadButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadButton2.Click
    Dim s As String = "default.xml"
    Dim dialog As New OpenFileDialog()
    dialog.Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*"
    dialog.Title = "Select a xml file"
    If dialog.ShowDialog() = DialogResult.OK Then
        s = dialog.FileName
    End If
    Me.RadCommandBar1.CommandBarElement.LoadLayout(s)
End Sub

After loading the layout it will reload the initial settings of the existing items:WinForms RadCommandBar Save Load Layout

When loading the layout it will only load the settings for the items that were there by the time when the layout was saved. If some of the items (i.e. CommandBarButton, CommandBarToggleButton) were deleted - the layout loading will not recreate them and if some items were added the layout will not effect them either.

Sample save/load scenario

The following example demonstrates how you can save the layout settings of a RadCommandBar when the parent form of this command bar is closed and how you can load these settings when the form is reopened. Basically, you need to handle the Load and FormClosing events of the form.

private void SaveAndLoadLayout1_Load(object sender, EventArgs e)
{
    if (File.Exists("MyLayout.xml"))
    {
        this.radCommandBar1.CommandBarElement.LoadLayout("MyLayout.xml");
    }
}

Private Sub SaveAndLoadLayout_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    If File.Exists("MyLayout.xml") Then
        Me.RadCommandBar1.CommandBarElement.LoadLayout("MyLayout.xml")
    End If
End Sub

In this article