Skip to main content

How to Create Items at Runtime and Place Them on a Toolbar or a Submenu

  • 2 minutes to read

This example illustrates how to create new items at runtime and then add them to a toolbar or submenu. The code below creates items of type TdxBarColorCombo and TdxBarButton and places them onto a toolbar or in a submenu. First you should place a bar manager and a toolbar on a form and add a subitem to the toolbar calling it siFormat. The btnCustomColorClick procedure represents the button’s OnClick event handler.

uses
  ..., dxBarExtItems;
// ...
type
  TMainForm = class(TForm)
  private
    // ...
    procedure btnCustomColorClick(Sender: TObject);
    // ...
  end;
procedure TMainForm.btnCustomColorClick(Sender: TObject);
var
  cbColor: TdxBarColorCombo;
begin
  cbColor := TdxBarColorCombo(TdxBarButton(Sender).Tag);
  with TColorDialog.Create(Self) do
  begin
    Color := cbColor.Color;
    if Execute then
      cbColor.Color := Color;
    Free;
  end;
end;
procedure TMainForm.FormCreate(Sender: TObject);
var
  cbColor: TdxBarColorCombo;
  btnCustomColor: TdxBarButton;
begin
  cbColor := TdxBarColorCombo.Create(Self);
  with cbColor do
  begin
    Caption := 'Color:';
    Color := clAqua;
  end;
  btnCustomColor := TdxBarButton.Create(Self);
  with btnCustomColor do
  begin
    Caption := '...';
    Hint := 'Click to select custom color';
    PaintStyle := psCaption;
    Tag := Integer(cbColor);
    OnClick := btnCustomColorClick;
  end;
  // Adds an item to a submenu
  siFormat.ItemLinks.Add.Item := cbColor;
  // Adds an item to a toolbar
  with dxBarManager1.Bars[0], ItemLinks do
  begin
    BeginUpdate;
    with Add do
    begin
      Item := cbColor;
      Index := 0;
      BringToTopInRecentList(False);
    end;
    with Add do
    begin
      Item := btnCustomColor;
      Index := 1;
      BringToTopInRecentList(False);
    end;
    EndUpdate;
  end;
end;
See Also