Mega Code Archive

 
Categories / Delphi / VCL
 

How to write your own components in less than 3 minutes

Title: How to write your own components in less than 3 minutes Question: This tutorial was kept as short as possible, since Delphi can be rather overwhelming, and since this is aimed at the beginner adding conceptual support, this tutorial will give you a quick and easy start into writing [modifying] components. It was created after I found out that it was rather easy to write [modify] your own components. It is a very basic tutorial, which I hope will help many people, into making the Delphi programming somewhat more efficient. It is however not the only way, and I think certainly maybe not the best way, but it is the way I use to limit my efforts for repeating tasks, if you are into programming, then you know you can always use your time for different tasks than those you know how to and have to perform again and again. Here is a way to change all of the components you work with, so they provide you exactly with what you want saving you time! Answer: Easy Component Writing Guide Quick Start 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, 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 between {==Start...End==} unit ButtonHell; interface uses SysUtils, Classes, Controls, StdCtrls; type TButtonHell = class(TButton) private { Private declarations } protected { Protected declarations } public {==Start constructor Create(AOwner: TComponent); override; End==} published { Published declarations } end; procedure Register; implementation {==Start constructor TButtonHell.Create(AOwner: TComponent); begin inherited Create(AOwner); end; 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 it's 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.