KB Article A1660
Visible to All Users

Why are the Items created in code not restored when loading settings?

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:

  1. 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);
  1. 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

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.