Skip to main content

How to Create Several Item Controls for the Same Underlying Item

  • 5 minutes to read

This example illustrates how you can create several item controls based on a single item. This procedure creates a toolbar and several item controls. Fifteen item controls determine standard colors for a form; the sixteenth control activates the Color Selection dialog, which allows you to select a non-standard color. The dxBarManagerBarDockingStyleChange procedure is the OnBarDockingStyleChange event handler. It changes the paint style of the item controls when changing the docking style of a new toolbar.

// ...
unit Unit1;
interface
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, dxBar, StdCtrls;
// form declaration
type
  TMainForm = class(TForm)
    dxBarManager: TdxBarManager;
    CreateColorBarButton: TButton;
    procedure CreateColorBarButtonClick(Sender: TObject);
    procedure dxBarManagerBarDockingStyleChange(Sender: TdxBarManager; ABar: TdxBar);
  private
    { Private declarations }
    barColor: TdxBar;
    procedure btnColorClick(Sender: TObject);
  public
    { Public declarations }
  end;
var
  MainForm: TMainForm;
implementation
{$R *.DFM}
// The OnClick event handler of a new item
procedure TMainForm.btnColorClick(Sender: TObject);
begin
  with TdxBarButton(Sender).ClickItemLink do
    if Data = -1 then
      with TColorDialog.Create(nil) do
      begin
        Color := Self.Color;
        if Execute then
          Self.Color := Color;
        Free;
      end
    else
      Color := Data;
end;
procedure TMainForm.CreateColorBarButtonClick(Sender: TObject);
const
  Captions: array[1..16] of string = ('Black', 'Maroon', 'Green', 'Olive', 'Navy', 'Purple', 'Teal', 'Gray', 'Silver', 'Red', 'Lime', 'Yellow', 'Blue', 'Fuchsia', 'Aqua', 'White');
  Colors: array[1..16] of TColor = (clBlack, clMaroon, clGreen, clOlive, clNavy, clPurple, clTeal, clGray, clSilver, clRed, clLime, clYellow, clBlue, clFuchsia, clAqua, clWhite);
var
  btnColor: TdxBarButton;
  B: TBitmap;
  I: Integer;
begin
  with dxBarManager do
  begin
    // Creates a new item
    btnColor := TdxBarButton.Create(Self);
    with btnColor do
    begin
      PaintStyle := psCaptionGlyph;
      OnClick := btnColorClick;
    end;
    // Creates a new toolbar for new item controls
    barColor := Bars.Add;
    with barColor do
    begin
      AllowCustomizing := False;
      Caption := 'Color';
    end;
    // Creates a bitmap drawn on an item control's surface
    B := TBitmap.Create;
    with B do
    begin
      Width := 18;
      Height := 16;
      with Canvas do
      begin
        // For transparent color
        Brush.Color := clBtnFace;
        FillRect(Rect(0, 0, Width, Height));
        // Draws frame
        Brush.Color := clBtnShadow;
        FrameRect(Rect(1, 0, Width - 1, Height));
      end;
    end;
    // Creates item controls on a new toolbar
    for I := 1 to 16 do
      with barColor.ItemLinks.Add do
      begin
        Data := Colors[I];
        Item := btnColor;
        UserCaption := Captions[I];
        with B, Canvas do
        begin
          Brush.Color := Colors[I];
          FillRect(Rect(2, 1, Width - 2, Height - 1));
        end;
        UserGlyph := B;
      end;
    // Creates the 16th item control
    // that calls the color selection dialog
    with barColor.ItemLinks.Add do
    begin
      BeginGroup := True;
      Data := -1;
      Item := btnColor;
      UserCaption := 'Custom...';
      UserPaintStyle := psCaption;
    end;
    B.Free;
    // Specifies position and displays a new toolbar on screen
    barColor.FloatLeft := Left - 80;
    barColor.FloatTop := Top;
    barColor.Visible := True;
  end;
end;
// The OnDockingStyleChange event handler of a bar manager
procedure TMainForm.dxBarManagerBarDockingStyleChange(Sender: TdxBarManager; ABar: TdxBar);
begin
  if ABar = barColor then
    with ABar do
    begin
      LockUpdate := True;
      with TdxBarButton(ItemLinks[0].Item) do
        if DockingStyle = dsNone then
          PaintStyle := psCaptionGlyph
        else
          PaintStyle := psStandard;
      LockUpdate := False;
    end;
end;
end.