Franky Brandt
Franky Brandt

Reputation: 203

Delphi - Add a Buttons property to component

I am making a Delphi DB aware component and i need to add a 'buttons' property, which should show with the ... button in the object inspector and after clicking the ... button a list should be shown to see the defined buttons, add, delete button definitions, i only know basics on component design and i'm puzzled on how to make this work.

The buttons definition of course has to be saved in the dfm file. I have been reading the question in this thread: How can I make a TList property from my custom control streamable? but the 'fItems:=TCollection.Create' statement in the constructor won't compile (error E2029 '(' expected but ')' found')

Does anyone see what i am doing wrong and/or can anyone provide a example of how to make a dynamic list of buttons in the component?

type
TAlignment = (Horizontal, Vertical);

TButtonsItem = class (TCollectionItem)
private
    FButton: TcxButton;
published
    property Button: TcxButton read FButton write FButton;
end;

TButtonsItemClass = class of TButtonsItem;

TFlexButtonGroupBox = class(TcxGroupBox)
private
    FDataLink: TFieldDataLink;
    FAbout: string;
    fAlignment: TAlignment;
    fEnabled: Boolean;
    fButtons: TCollection;
    procedure SetAlignment(const Value: TAlignment);
    function GetDataField: string;
    function GetDataSource: TdataSource;
    procedure SetDataField(const Value: string);
    procedure SetDataSource(const Value: TdataSource);
    procedure DataChange(Sender: TObject);
    procedure SetEnabled(const Value: Boolean);
protected
public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
published
    property DataField: string read GetDataField write SetDataField;
    property DataSource: TdataSource read GetDataSource write SetDataSource;
    property Enabled: Boolean read fEnabled write SetEnabled;
    property About: string read FAbout write FAbout;
    property Buttons: TCollection read fButtons write fButtons;
    property Alignment: TAlignment read fAlignment write SetAlignment;
end;

In constructor:

fButtons := TCollection.Create(TButtonsItemClass); <- error

Thanks in advance

Upvotes: 0

Views: 2016

Answers (1)

Max Abramovich
Max Abramovich

Reputation: 483

Franky, sorry nobody has explained the problem yet. I do agree that the compiler error

[dcc32 Error] Unit2.pas(26): E2029 '(' expected but ')' found

is not good enough to understand the problem. Actually you are using incompatible types in your create statement. The compiler needs a value(variable) of TButtonsItemClass but you are using a type there. To resolve the compiler error you should use

var
  LItemClass: TButtonsItemClass;
...
  LItemClass := TButtonsItem;
  fButtons := TCollection.Create(LItemClass);

or in short

fButtons := TCollection.Create(TButtonsItem);

PS I should also point out that your code may have another potential problem (button property of TButtonsItem class). I would assume that it is a reference to another component and I would expect that you will use FreeNotification for it.

Upvotes: 3

Related Questions