0

I would like to create a TSpeedButton descendant named TMyDropDownButton with a TButtonGroup field. When I push the TSpeedButton, the TButtonGroup should appear in the right place (due to some other property values venished here).

This TButtonGroup property should be read-only (the program creates it in the Create() constructor before the inherited constructor is called), and should be published (save the design-time values into the DFM).

But, when I place a TMyDropDownButton instance onto a form, the TButtonGroup property is unset by default. I can link a dropped TButtonGroup when I add a write accessor to the TButtonGroup property, but I dont want this. I want the TButtonGroupproperty to be linked to theTButtonGroupinstance created in the constructor, and preset by theLoaded()` method.

How can I do that?

Here is my attempt:

unit MyDropDownButton;

interface

uses
    System.Classes
  , Vcl.ButtonGroup
  , Vcl.Buttons
  ;

type
  TMyDropDownButton = class ( TSpeedButton )
    private
      // Fields
      fButtonGroup : TButtonGroup;

    protected
      procedure Loaded; override;

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

    published
      property buttonGroup : TButtonGroup read fButtonGroup;

  end;


procedure Register;


implementation


procedure Register;
begin
  RegisterComponents('myDropDownButton', [TMyDropDownButton]);
end;

procedure TMyDropDownButton.Loaded;
begin
  inherited Loaded;
  // I need some settings here due to venished props inrelevant for the question
end;

constructor TMyDropDownButton.Create( owner_ : TComponent );
begin
  fButtonGroup := TButtonGroup.Create( self );
  inherited Create( owner_ );
  fButtonGroup.Parent := parent;
end;

destructor TMyDropDownButton.Destroy;
begin
//  FreeAndNIL( fButtonGroup );
  inherited Destroy;
end;


end.

1 Answer 1

1

TButtonGroup is a TComponent descendant, so in order for your component to use it as a nested property within the Object Inspector, you have to call its SetSubComponent() method, eg:

constructor TMyDropDownButton.Create( owner_ : TComponent );
begin
  inherited Create( owner_ );
  fButtonGroup := TButtonGroup.Create( Self );
  fButtonGroup.SetSubComponent( True );
  ... 
end;

Otherwise, the Object Inspector will expect you to link the property to an external component, as you have already discovered.

Also, you can't set the TButtonGroup.Parent property to your component's Parent property from inside of your component's constructor, because your component's Parent has not been assigned yet at that time.

If you need the TButtonGroup to use the same Parent that is assigned to your component, you will have to override your component's virtual SetParent() method, eg:

type
  TMyDropDownButton = class ( TSpeedButton )
    ...
    protected
      ...
      procedure SetParent(AParent: TWinControl); override;
    ...
  end;

procedure TMyDropDownButton.SetParent(AParent: TWinControl); 
begin
  inherited SetParent(AParent);
  fButtonGroup.Parent := AParent;
end;
0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.