3

I want to create a custom toolbar control (descendant TToolBar) which should have some default-toolbarButtons.

So I've created a simple constructor which creates 1 default button:

constructor ZMyToolbart.Create(AOwner: TComponent);
var
  ToolButton : TToolButton;
begin
  inherited;
  Parent := Owner as TWinControl;
  ToolButton := TToolButton.Create(Self);
  ToolButton.Parent := Self;
  ToolButton.Caption := 'Hallo';
end;

The problem is, after draggign the custom control on a form, the toolbar-button is visible, but it's not shown in the object inspector as a part of the toolbar.

If tried to assign the button via the button property of the toolbar, but this doesn't work. Maybe somebody has an advice how this could be done? thank you!

2
  • Toolbars are one of the more wrinkly controls to work with. If I were you I'd be inclined to put it inside a frame and re-use it that way. Commented Mar 7, 2011 at 14:52
  • Are you sure you want to use a Toolbar here? It would be a lot simpler to use a panel, with a bitmap texture, and some speedbuttons, maybe make it a Frame, so you can reuse it easily. I would write a component, but then I've been doing this for a lot of years. If you're new, maybe you should consider a frame. Commented Mar 9, 2011 at 13:44

2 Answers 2

4

If you make the toolbar the owner of the tool button, you need to have a published property to be able to set its properties in the object inspector. This will also make it possible to free it later. The local variable in your code sample suggests this is not the case.

type
  ZMyToolbart = class(TToolbar)
  private
    FHalloButton: TToolButton;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property HalloButton: TToolButton read FHalloButton write FHalloButton;
  end;

constructor ZMyToolbart.Create(AOwner: TComponent);
begin
  inherited;
  Parent := Owner as TWinControl;
  FHalloButton := TToolButton.Create(Self);
  FHalloButton.Parent := Self;
  FHalloButton.Caption := 'Hallo';
end;

destructor ZMyToolbart.Destroy;
begin
  FHalloButton.Free;
  inherited;
end;


This, probably, will not give you what you want, you'll see the button's properties in a sub-property in the OI, not like other buttons. If you want your button to appear like ordinary tool buttons, make its owner the form, not the toolbar.

Then the button will be selectable on its own. This also means the button might be deleted at design-time (as well as at run time), hence you'd want to be notified when it's deleted and set its reference to nil.

Finally, you only want to create the button at design-time, since at run-time the button will be stream-created from the .dfm and then you'll have two buttons.

And don't forget to register the button class:

type
  ZMyToolbart = class(TToolbar)
  private
    FHalloButton: TToolButton;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  protected
    procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  end;

[...]
constructor ZMyToolbart.Create(AOwner: TComponent);
begin
  inherited;
  Parent := Owner as TWinControl;
  if Assigned(FHalloButton) then
    Exit;

  if csDesigning in ComponentState then begin
    FHalloButton := TToolButton.Create(Parent);
    FHalloButton.Parent := Self;
    FHalloButton.FreeNotification(Self);
    FHalloButton.Caption := 'Hallo';
  end;
end;

destructor ZMyToolbart.Destroy;
begin
  FHalloButton.Free;
  inherited;
end;

procedure ZMyToolbart.Notification(AComponent: TComponent;
  Operation: TOperation);
begin
  inherited Notification(AComponent, Operation);
  if (AComponent = FHalloButton) and (Operation = opRemove) then
    FHalloButton := nil;
end;

initialization
  RegisterClass(TToolButton);
Sign up to request clarification or add additional context in comments.

Comments

1

it seems that the owner of the ToolButton should be the form itself instead of the toolbar. When changing the code to the following the ToolButton is displayed under the ToolBar in the object inspector:

constructor ZMyToolbart.Create(AOwner: TComponent);
var
  ToolButton : TToolButton;
begin
  inherited;
  Parent := Owner as TWinControl;
  ToolButton := TToolButton.Create(Self.Parent);
  ToolButton.Parent := Self;
  ToolButton.Caption := 'Hallo';
end;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.