1. Tech

Your suggestion is on its way!

An email with a link to:


was emailed to:

Thanks for sharing About.com with others!

RTL reference|Glossary|Tips/Tricks|FREE App/VCL|Best'O'Net|Books|Link To
Creating Custom Delphi Components, Part I
Page 1: Why, when and how you should need to write a component.
 More of this Feature
• Page 2: New...component
• Page 3: Raw Code
• Page 4: Adding events
• Page 5: Writing standards

• Download Demo Projects
 Join the Discussion
"Post your questions, concerns, views and comments to this article..."
 Related Resources
• Custom VCL development
• VCL with source
• Third party VCL
• VCL using

Article submitted by: Peter Morris
"This article originally appeared in Delphi Developer. Copyright Pinnacle Publishing, Inc. All rights reserved."

This first part demonstrates some of the best approaches to building components, and at the same time provides tips on deciding on the best base class to inherit from, using virtual declarations, the complexities of overriding, and so on. Part two will cover how to write advanced properties, how to write custom streaming for those properties, and sub-properties. The final part will cover property / component editors, how to write dedicated editors for your component / property, and how to write "hidden" components.

The first two things to ask yourself are why you should make use of component writing, and when you should need to write a component.

The first question is easy to answer, in fact, it has many answers.
· Ease of use: Encapsulated code means that you can simply drop the same component onto various forms again and again without writing a single line of code.
· Debugging: Centralised code makes it easy to repair an entire application (or set of applications) by fixing an error in a single component and recompiling.
· Money: There are a lot of companies that are only too happy to pay for the privilege of not reinventing the wheel.

The second question is not too difficult to answer either. Whenever you find yourself having to write the same code more than once it is a good idea to write a component, especially if the code performs differently based on given parameters.

   How components are created
The first step is to decide which base class you need to derive your component from. When you derive from another class you inherit property / method and event that that component owns. Below is an example of how to decide which class you should inherit from when writing your own component.

Click for a larger photo

In the case of method A and method B, presume that the component in question is TMemo.

Method Solution
A Derive from TMemo
B Derive from TCustomMemo
C Derive from TCustomControl
D Derive from TGraphicControl
E Derive from TComponent

This may seem a little complicated, so let me explain the process.

A: When you simply need to add extra functionality to an existing component you derive from that component. This will automatically give your new component all of the existing functionality and properties of the existing component.

B: Sometimes you not only need to add functionality, but you need to remove it at the same time. The standard practise when writing a component is first to write a TCustomXXXXX component where all of the properties / events and methods are declared within the Protected section of the component. The actual component is then derived from this base class. The trick therefore is not to hide functionality, but to derive your component from the "custom" version of the component and only publish the properties you want to keep (effectively removing unwanted properties / events).

C: Any component that needs to receive focus needs a window handle. TWinControl is where this handle is first introduced. TCustomControl is simply a TWinControl with its own Canvas property.

D: TGraphicControl does not have a window handle and therefore cannot receive focus. Components such as TLabel are derived from this base class.

E: Some components are not created to enhance GUI interactivity but to make your life easier. Anything derived from TComponent is only visible at design-time. A typical use of this type of component is for database connections, timers etc.

Next page > New...component > Page 1, 2, 3, 4, 5

Creating Custom Delphi Components >>
>> Part II.

All graphics (if any) in this feature created by Peter Morris.

 More Delphi
· Learn another routine every day - RTL Quick Reference.
· Download free source code applications and components.
· Talk about Delphi Programming, real time.
· Link to the Delphi Programming site from your Web pages.
· Tutorials, articles, tech. tips by date: 2001|2000|1999|1998 or by TOPIC.
· NEXT ARTICLE: Look Ma, I've found a BUG.
How do you know if a bug in your Delphi application is really yours? Windows applications are NOT bug-free; Delphi is no exception to this rule.
 Stay informed with all new and interesting things about Delphi (for free).
Subscribe to the Newsletter

 Got some code to share? Got a question? Need some help?

©2016 About.com. All rights reserved.