Back to Devexpress

How to Add Custom Items to a Toolbar's Popup Menu

vcl-155064-expressbars-how-to-add-custom-items-to-a-toolbar-s-popup-menu.md

latest3.1 KB
Original Source

How to Add Custom Items to a Toolbar's Popup Menu

  • Aug 02, 2022
  • 2 minutes to read

This example demonstrates how you can add your own items to the Toolbars Popup Menu.

Let’s add the Show Captions option to this popup menu. This option toggles the captions of all TdxBarLargeButtons within toolbars.

The following code snippet adds a new button within a bar manager’s OnShowToolbarsPopup event handler.

delphi
// ...
uses
  ..., dxBarExtItems;
// ...
btnShowCaptions: TdxBarButton;
// ...
// The OnClick event handler of the ShowCaptions button
procedure TMainForm.btnShowCaptionsClick(Sender: TObject);
var
  I: Integer;
begin
  dxBarManager.BeginUpdate;
    try
      for I := 0 to dxBarManager.ItemCount - 1 do
        if dxBarManager.Items[I] is TdxBarLargeButton then
          TdxBarLargeButton(dxBarManager.Items[I]).ShowCaption := btnShowCaptions.Down;
    finally
      dxBarManager.EndUpdate;
    end;
end;
// The OnShowToolbarsPopup event handler
procedure TMainForm.dxBarManagerShowToolbarsPopup(Sender: TdxBarManager; PopupItemLinks: TdxBarItemLinks);
var
  ABarItemLink: TdxBarItemLink;
begin
  if btnShowCaptions = nil then
    begin
      btnShowCaptions := Sender.AddButton;
      btnShowCaptions.ButtonStyle := bsChecked;
      btnShowCaptions.Caption := 'Show Captions';
      btnShowCaptions.Down := True;
      btnShowCaptions := btnShowCaptionsClick;
    end;
  end;
  ABarItemLink := PopupItemLinks.Add;
  ABarItemLink.BeginGroup := True;
  ABarItemLink.Item := btnShowCaptions;
end;
cpp
Unit1.h
// ...
#include "dxBarExtItems.hpp"
// ...
class TMainForm : public TForm
{
// ...
private:
  void __fastcall btnShowCaptionsClick(TObject *Sender);
// ...
};
Unit1.cpp
// ...
TdxBarButton *btnShowCaptions;
// ...
// The OnClick event handler of the ShowCaptions button
void __fastcall TMainForm::btnShowCaptionsClick(TObject * Sender)
{
  dxBarManager->BeginUpdate();
  try {
    for(int I = 0;I<dxBarManager->ItemCount;I++)
      if(static_cast<UnicodeString>(dxBarManager->Items[I]->ClassName()) == "TdxBarLargeButton")
        (dynamic_cast<TdxBarLargeButton*>(dxBarManager->Items[I]))->ShowCaption = btnShowCaptions->Down;
  }
  __finally {
    dxBarManager->EndUpdate();
  }
}
// The OnShowToolbarsPopup event handler
void __fastcall TMainForm::dxBarManagerShowToolbarsPopup(TdxBarManager *Sender, TdxBarItemLinks *PopupItemLinks)
{
  TdxBarItemLink *ANewItemLink;
  if (btnShowCaptions == nullptr)
  {
    btnShowCaptions = Sender->AddButton();
    btnShowCaptions->ButtonStyle = bsChecked;
    btnShowCaptions->Caption = "Show Captions";
    btnShowCaptions->Down = true;
    btnShowCaptions->OnClick = btnShowCaptionsClick;
  }
    ANewItemLink = PopupItemLinks->Add();
    ANewItemLink->BeginGroup = true;
    ANewItemLink->Item = btnShowCaptions;
}