Prevent streaming of TToolButtons to DFM with custom TToolbar component

Viewed 95

I'm attempting to create a custom TToolbar component which shows buttons relevant to a current task.

However I'm finding that the buttons added to the component at design time are being stored in the DFM. I don't want the buttons to be stored (or accessible at design time) as I want to create them dynamically.

(I realize could prevent this happening by only adding buttons if csDesigning is not in ComponentState, but I want to show a design-time preview of the toolbar).

Here is the component code:

type
  TTestToolbar = class(TToolbar)
  private
    fTestButton1: TToolButton;
    fTestButton2: TToolButton;
    fTestButton3: TToolButton;
    fTestButton4: TToolButton;

  protected
    procedure SetParent(AParent: TWinControl); override;

  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;

    procedure InitializeButtons();
  published
  end;


constructor TTestToolbar.Create(AOwner: TComponent);
begin
  inherited;
end;

destructor TTestToolbar.Destroy;
begin
  FreeAndNil( fTestButton1 );
  FreeAndNil( fTestButton2 );
  FreeAndNil( fTestButton3 );
  FreeAndNil( fTestButton4 );

  inherited;
end;

procedure TTestToolbar.SetParent(AParent: TWinControl);
var
  changed: Boolean;
begin
  changed := AParent <> Parent;

  inherited;

  if changed then
    InitializeButtons();
end;

procedure TTestToolbar.InitializeButtons();
begin
  if Parent = nil then
    exit;  

  FreeAndNil( fTestButton1 );
  FreeAndNil( fTestButton2 );
  FreeAndNil( fTestButton3 );
  FreeAndNil( fTestButton4 );

  fTestButton1 := TToolButton.Create(Self);
  fTestButton1.Parent     := Self;
  fTestButton1.Caption     := 'Btn 1';
  fTestButton1.Hint     := 'Btn 1';

  fTestButton2 := TToolButton.Create(Self);
  fTestButton2.Parent     := Self;
  fTestButton2.Caption     := 'Btn 2';
  fTestButton2.Hint     := 'Btn 2';

  fTestButton3 := TToolButton.Create(Self);
  fTestButton3.Parent     := Self;
  fTestButton3.Caption     := 'Btn 3';
  fTestButton3.Hint     := 'Btn 3';

  fTestButton4 := TToolButton.Create(Self);
  fTestButton4.Parent     := Self;
  fTestButton4.Caption     := 'Btn 4';
  fTestButton4.Hint     := 'Btn 4';
end;

Example of DFM when toolbar is added to a form:

object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 411
  ClientWidth = 852
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object TestToolbar1: TTestToolbar
    Left = 0
    Top = 0
    Width = 852
    Height = 29
    Caption = 'TestToolbar1'
    TabOrder = 0
    object TToolButton
      Left = 0
      Top = 0
      Hint = 'Btn 4'
      Caption = 'Btn 4'
    end
    object TToolButton
      Left = 23
      Top = 0
      Hint = 'Btn 3'
      Caption = 'Btn 3'
    end
    object TToolButton
      Left = 46
      Top = 0
      Hint = 'Btn 2'
      Caption = 'Btn 2'
    end
    object TToolButton
      Left = 69
      Top = 0
      Hint = 'Btn 1'
      Caption = 'Btn 1'
    end
  end
end
0 Answers
Related