Description:
I am using code from the AddBarItemLinks tutorial demo to create Items (buttons, edits, etc.) programmatically and have added some code to preserve the bars' layout in an XML file. However, after restoring the settings by calling the Bar Manager's RestoreFromXML method all the Item Links that were created programmatically disappear. Here is my code:
- To create a new Item and position it on a toolbar
C#DevExpress.XtraBars.Bar bar = barManager1.Bars[0];
barItem = new DevExpress.XtraBars.BarButtonItem();
barManager1.Items.Add(barItem);
bar.AddItem(barItem);
barItem.Caption = "Item " + (i++).ToString();
barItem.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(ItemClick);
- To save and restore the settings…
C#barManager1.SaveToXml("c:\\bar.xml");
...
barManager1.RestoreFromXml("c:\\bar.xml");
What am I doing wrong?
Answer:
When creating Items programmatically, you should initialize an Item's Id property if you need to preserve the settings of toolbars. This value is used to identify an Item when loading settings from storage. By default, this property's value is set to -1 and thus, the Bar Manager fails to find the corresponding Item.
We recommend that you define a base ID value for runtime-created Bar Items and increment it when generating ID values. This will allow you to prevent mixing the Items set up at design-time and the Items added programmatically.
For example:
C#int RuntimeItemsID = 1000;
barItem = new DevExpress.XtraBars.BarButtonItem();
barItem.Id = RuntimeItemsID++;
NOTE: You may also use the BarManager.GetNewItemId method to generate the ID values. However, this approach can be used only if you do not add more Items at design-time after storing settings. Otherwise, the ID values will be mixed.
IMPORTANT: The Bar Manager does not create items when loading settings. It just creates the necessary toolbars and menus, then looks for the existing items and arranges them on toolbars and menus according to the stored information about Item Links. So, you must create the necessary items before loading settings when you launch application. To learn how to create XtraBars Items programmatically, please refer to the XtraBars' documentation: BarManager and RibbonControl.
See Also:
A dynamically created DockPanel loses its child controls after its layout is restored