Thursday 12/11/2008 17:42
How to write your own delphi components
See also:
This little tutorial will take you only 3 minutes in 8 steps!
---------------------------------------------------------------------------------------------------------
1. Choose Component and then New Component to start....
---------------------------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------------------------
2. The New Component dialog appears, in which you need to choose a few items.
----------------------------------------------------------------------------------------------------------


Note that the ClassName and the Unit File Name need to be different, for example if the ClassName is
TButtonHell, your unit could be named ButtonHell.pas. It is standard to begin your Classname with the
character T but not required.
----------------------------------------------------------------------------------------------------------
3. Next choose the Install button and the Install dialog will appear
----------------------------------------------------------------------------------------------------------


----------------------------------------------------------------------------------------------------------
4. Choose the default option above, or click the second tab and install to a new package
----------------------------------------------------------------------------------------------------------


Choose a unique name for your package, you could include the version like in this case ButtonHell7.dpk
----------------------------------------------------------------------------------------------------------
5. Click ok, and delphi will ask for a confirmation using the confirmation dialog, click Yes
----------------------------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------------------------
The resulting Unit will look like the one below:
----------------------------------------------------------------------------------------------------------
unit ButtonHell;
interface
uses
SysUtils, Classes, Controls, StdCtrls;
type
TButtonHell = class(TButton)
private
{ Private declarations }
protected
{ Protected declarations }
public
{ Public declarations }
published
{ Published declarations }
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Fdehell', [TButtonHell]);
end;
end.
----------------------------------------------------------------------------------------------------------
6. Make the following changes in green
----------------------------------------------------------------------------------------------------------
unit ButtonHell;
interface
uses
SysUtils, Classes, Controls, StdCtrls;
type
TButtonHell = class(TButton)
private
{ Private declarations }
protected
{ Protected declarations }
public
constructor Create(AOwner: TComponent); override;
published
{ Published declarations }
end;
procedure Register;
implementation
constructor TButtonHell.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
end;
procedure Register;
begin
RegisterComponents('Fdehell', [TButtonHell]);
end;
end.
----------------------------------------------------------------------------------------------------------
7. Now, this was not that difficult, if you keep reading, you will find that the rest is not that
difficult either! In fact one more step and you have created your own component! Let's
change some of the values to suit our needs, say we want the Tbutton, to show its Hint
[visible when hovering over it] by default, here is how we can do that.
Publish the property that you want to change by default, in the Published Section:
----------------------------------------------------------------------------------------------------------
published
property Showhint default True;
----------------------------------------------------------------------------------------------------------
Then declare the property in the constructor
----------------------------------------------------------------------------------------------------------
constructor TButtonHell.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
ShowHint := True;
end;
----------------------------------------------------------------------------------------------------------
8. Save and compile this, you have just created your own Tbutton,
that has your default! For your reference here
follows the completed
tutorial. One more example is included in the zipfile, note that you
have to create it using
the Delphi interface, meaning that copy and
pasting the whole code will not work, since Delphi creates several
files more as you use the above descripbed way to create components.
----------------------------------------------------------------------------------------------------------
unit ButtonHell;
interface
uses
SysUtils, Classes, Controls, StdCtrls;
type
TButtonHell = class(TButton)
private
{ Private declarations }
protected
{ Protected declarations }
public
constructor Create(AOwner: TComponent); override;
published
property Showhint default True;
end;
procedure Register;
implementation
constructor TButtonHell.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
ShowHint := True;
end;
procedure Register;
begin
RegisterComponents('Fdehell', [TButtonHell]);
end;
end.