Reputation: 947
I have this application that reuses a sort of idiom in a number of places. There's a TPanel, and on it are some labels and buttons. The purpose is to allow the user to select a date range.
The "&Dates" caption is one label, and the "All Dates" part is a second one. When the user clicks the "Choose" button a form pops up presenting the user with a pair of Date/Time controls and OK/Cancel buttons. If the user hits OK after selecting some dates, the 2nd label changes to "From mm/dd/yyyy To mm/dd/yyyy".
Is it plausible to create a component that packages up these controls? I have been looking at various resources for component writers and they don't seem to be pointed at the problems I am thinking about, such as handling the onclick events for the buttons. If this is a reasonable thing to attempt, I'd also appreciate pointers to descriptions of how to make such a "composite control."
Upvotes: 3
Views: 1734
Reputation: 3349
Yet another way is to make the group of components a component template.
Upvotes: 2
Reputation: 24513
A very pragmatic way to develop composite controls is to use TFrame as a base for it.
That way, you can visually design your control, and either use events or inheritance
There are a couple of things you need to watch but all in all it is a much easier process than coding everything by hand (like some of the other answers suggest).
Things to watch for (not a complete list, but close):
As a bonus, you don't have to remove the bevel/border and caption from the TPanel.
Upvotes: 2
Reputation: 3857
Yes, absolutely it is wise to build components like that because It saves a huge amount of coding.
Here is a guide to creating them semi-visually: How to Build Aggregate/Composite Components in Delphi
Essentially, the process outlined in this document is:
Where the document I think is wrong is that, for example, the example component is descended from a TPanel whereas to me it makes more sense to use TCustomPanel and expose only the methods you want to.
But it also explains how to add OnClick handlers etc.
the advantage of this method is that you get the layout of the components within the Panel done visually.
Upvotes: 5
Reputation: 711
It's reasonable, yes.
To create such a component, just derive a new class from for instance TCustomPanel, and add the sub-components as fields within the class.
Like this:
TMyDatePicker = class(TCustomPanel)
protected
FChooseButton: TButton;
FClearButton: TButton;
public
constructor Create(Owner: TComponent); override;
end;
constructor TMyDatePicker.Create(Owner: TComponent)
begin
// Inherited
Inherited;
// Create Choose Button
FChooseButton := TButton.Create(Self);
FChooseButton.Parent := Self;
FChooseButton.Align := alRight;
FChooseButton.Caption := 'Choose';
// Create Clear Button
FClearButton := TButton.Create(Self);
FClearButton.Parent := Self;
FClearButton.Align := alRight;
FClearButton.Caption := 'Clear';
end;
To add event handers, just add new protected procedures to your class.
For instance:
procedure TMyDatePicker.HandleChooseButtonClick(Sender: TObject)
begin
// Do whatever you want to do when the choose button is clicked
end;
Then connect the event handler to the OnClick event of the choose button (this should be done within the Create method of the class):
FChooseButton.OnClick := HandleChooseButtonClick;
There's a bit more to it than this, of course, such as fine tuning the alignments of the buttons and adding icons. Also you'll need to create your own events, such as OnDateSelected or OnDateModified.
But, apart from that, I think the above example should at least get you going. :)
Upvotes: 6