Saving and Restoring a Bars Layout Manually
- 4 minutes to read
The bars’ layout can be automatically saved by the BarManager, or manually by the user. The layout can only be automatically saved/restored if the Bar Manager belongs to a Form class instance. Otherwise, the layout should be saved/loaded manually by a user. This topic describes the various ways to perform this task.
Please refer to the Saving and Restoring a Bars Layout Automatically topic, for information on how to save/restore the bars’ layout automatically. For information on saving/restoring the layout of dock panels, see Saving and Restoring the Layout of Dock Panels.
For information on saving the layout of bar commands within the Ribbon Quick Access Toolbar, see this link.
The bar items within the BarManager, and within the stored layout, are identified by the values of their BarItem.Id properties. For items created at design time, the BarItem.Id properties are initialized automatically. For items created via code, initialize their BarItem.Id properties manually to allow the layout of the bars to be correctly saved and restored. To set the BarItem.Id property’s value, the BarManager.GetNewItemId method can be used.
Do not restore the layout of bars until the Bar Manager is completely initialized. Typically, you need to restore the layout within the Form.Load event.
When saving/loading the layout of bars and using the merging feature, take note of the following. If you saved the layout while bars have been merged, do not load this layout after the bars have been unmerged. Typically, you need to save and load the layout of bars while the bars are unmerged.
How to Save the Bars’ Layout to a System Registry
The bars’ layout can be saved to the system registry by calling the BarManager’s BarManager.SaveLayoutToRegistry method. You can then restore these settings using the BarManager.RestoreLayoutFromRegistry method.
You can define absolute and relative registry keys as the parameter. For instance, specifying “Software\MyCompany\MyProject" as a partial key will result in saving the settings to the “HKEY_CURRENT_USER\Software\MyCompany\MyProject" path. You can also specify the entire key if, for instance, you need to store the layout to another root key. The following sample code shows how the bars’ layout can be saved to the specified system registry path and then restored.
string regKey = "DevExpress\\XtraBars\\Layouts\\MainLayout"; barManager1.SaveLayoutToRegistry(regKey); // ... barManager1.RestoreLayoutFromRegistry(regKey);
How to Save the Bars’ Layout to a Stream
The bars’ layout settings can also be written to a stream. Call the BarManager’s BarManager.SaveLayoutToStream method for this purpose. You can then read the saved settings using the BarManager.RestoreLayoutFromStream method. The example below shows how this can be done. The sample code below saves the layout settings to a memory stream, and then restores them.
System.IO.Stream str; //... // creating and saving the bars' layout to a new memory stream str = new System.IO.MemoryStream(); barManager1.SaveLayoutToStream(str); str.Seek(0, System.IO.SeekOrigin.Begin); // ... // loading the bars' layout from a previously saved memory stream barManager1.RestoreLayoutFromStream(str); str.Seek(0, System.IO.SeekOrigin.Begin);
How to Save the Bars’ Layout to an XML File
If it is necessary to save the bars’ layout to an XML file, call the BarManager’s BarManager.SaveLayoutToXml method. The BarManager.RestoreLayoutFromXml method restores previously saved layout settings from the XML file. The following sample code shows how to save and load the bars’ layout at runtime.
string fileName = "c:\\XtraBars_SaveToXML.xml"; barManager1.SaveLayoutToXml(fileName); // ... barManager1.RestoreLayoutFromXml(fileName);
A layout previously saved to an XML file can also be loaded and applied to bars at design time, using the XtraBars Editor provided. Please see the Saving And Restoring Layout Pages topic, for information.