Ticket A177
Visible to All Users

How to create a toolbar application menu and populate it at runtime

created 21 years ago (modified 5 years ago)

Description:
I would like to drop a BarManager onto a form and then during the form's OnCreate event create the menus and toolbars in code. Do you have any examples on how this can be achieved?

Answer:
From time to time you may need to create toolbars or bar items at runtime. The attached sample demonstrates the full run-time creation of a BarManager, toolbars, categories, items and item links.

Delphi
var ABarManager: TdxBarManager; B1, B2: TdxBarButton; ABarItem: TdxBarItem; ALargeButton: TdxBarLargeButton; begin // create a BarManager ABarManager := TdxBarManager.Create(Self); // ---------------------------------- // create a toolbar with ABarManager.Bars.Add do begin DockingStyle := dsTop; Visible := True; Caption := 'Toolbar'; end; // create the main menu with ABarManager.Bars.Add do begin DockingStyle := dsTop; Visible := True; IsMainMenu := True; Caption := 'Menu'; end; // ---------------------------------- // add a category ABarManager.Categories.Add('Main category'); // ---------------------------------- // create buttons B1 := ABarManager.AddButton; with B1 do begin Caption := 'BarButton 1'; Category := ABarManager.Categories.IndexOf('Main category'); Description := 'A button'; Glyph.Assign(Image1.Picture.Graphic); OnClick := Button1Click; end; B2 := ABarManager.AddButton; B2.Caption := 'BarButton 2'; ALargeButton := ABarManager.AddItem(TdxBarLargeButton) as TdxBarLargeButton; ALargeButton.Caption := 'LargeButton'; // create a subitem ABarItem := ABarManager.AddSubItem; ABarItem.Caption := '&SubMenu'; // ---------------------------------- // assign item links of // the main menu ABarManager.MainMenuBar.LockUpdate := True; ABarManager.MainMenuBar.ItemLinks.Add.Item := ABarItem; ABarManager.MainMenuBar.LockUpdate := False; // a submenu TdxBarSubItem(ABarItem).ItemLinks.Add.Item := B1; with TdxBarSubItem(ABarItem).ItemLinks.Add do begin Item := B2; Index := 0; // make it to be the first item link of the submenu end; // a toolbar ABarManager.Bars[0].LockUpdate := True; ABarManager.Bars[0].ItemLinks.Add.Item := B2; with ABarManager.Bars[0].ItemLinks.Add do begin Item := B1; BeginGroup := True; UserPaintStyle := psCaptionGlyph; end; ABarManager.Bars[0].LockUpdate := False; ABarManager.Bars[0].ItemLinks.Add.Item := ALargeButton; // ---------------------------------- end;
C/C++
// create a BarManager TdxBarManager *ABarManager = new TdxBarManager(this); // ---------------------------------- // create a toolbar TdxBar *AToolBar = ABarManager->Bars->Add(); AToolBar->DockingStyle = dsTop; AToolBar->Visible = True; AToolBar->Caption = "Toolbar"; // create the main menu TdxBar *AMainMenu = ABarManager->Bars->Add(); AMainMenu->DockingStyle = dsTop; AMainMenu->Visible = True; AMainMenu->IsMainMenu = True; AMainMenu->Caption = "Menu"; // ---------------------------------- // add a category ABarManager->Categories->Add("Main category"); // ---------------------------------- // create buttons TdxBarButton *B1 = ABarManager->AddButton(); B1->Caption = "BarButton 1"; B1->Category = ABarManager->Categories->IndexOf("Main category"); B1->Description = "A button"; B1->Glyph->Assign(Image1->Picture->Graphic); B1->OnClick = Button1Click; TdxBarButton *B2 = ABarManager->AddButton(); B2->Caption = "BarButton 2"; TdxBarLargeButton *ALargeButton = ((TdxBarLargeButton*) ABarManager->AddItem( __classid(TdxBarLargeButton) ) ); ALargeButton->Caption = "LargeButton"; // create a subitem TdxBarSubItem *ABarSubItem = ABarManager->AddSubItem(); ABarSubItem->Caption = "&SubMenu"; // ---------------------------------- // assign item links of // the main menu ABarManager->MainMenuBar->LockUpdate = True; ABarManager->MainMenuBar->ItemLinks->Add()->Item = ABarSubItem; ABarManager->MainMenuBar->LockUpdate = False; // a submenu ABarSubItem->ItemLinks->Add()->Item = B1; TdxBarItemLink *ABarItemLink = ABarSubItem->ItemLinks->Add(); ABarItemLink->Item = B2; ABarItemLink->Index = 0; // make it to be the first item link of the submenu // a toolbar ABarManager->Bars->Items[0]->LockUpdate = True; ABarManager->Bars->Items[0]->ItemLinks->Add()->Item = B2; ABarItemLink = ABarManager->Bars->Items[0]->ItemLinks->Add(); ABarItemLink->Item = B1; ABarItemLink->BeginGroup = True; ABarItemLink->UserPaintStyle = psCaptionGlyph; ABarManager->Bars->Items[0]->LockUpdate = False; ABarManager->Bars->Items[0]->ItemLinks->Add()->Item = ALargeButton; // ---------------------------------- }

See also:
How to make ExpressBars add new items to toolbars when releasing a new application version
Why are dynamically created items not displayed when loading settings in the next sessions of an application?

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.