1. Home
  2. Computing & Technology
  3. Delphi Programming
RTL reference|Glossary|Tips/Tricks|FREE App/VCL|Best'O'Net|Books|Link To
 
Creating Custom Delphi Components, Part I
Page 2: Creating your first component; Encapsulation
 More of this Feature
• Page 1: Why, when, how
• 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..."
Discuss!
 Related Resources
• Custom VCL development
• VCL with source
• Third party VCL
• VCL using

   Creating your first component
Once we have decided what our base class should be, the next step is to create the component. From the Component menu, select New Component and you will see the following dialog.

New component

Ancestor type This is the base type that we need to descend from.
Class name This is the class name of our new component.
Palette page This is which tab on the component palette you would like your component to appear on, entering a nonexistent tab will tell Delphi that you would like a new tab created.

   Writing some code
Already it is time for us to write something. This first example will have no use at all except to demonstrate some basic concepts of component writing.

First of all, select Component from the main Delphi menu and then select New Component. Enter TComponent as the "Ancestor type" and TFirstComponent as the name of your component. Next click the "Install" button.

At this point you may either install your new component into an existing package (a package holds a collection of components) or install into a new package. Click "into new package" and click the "Browse" button.

Install component

Once you have selected a path and filename for your new package and entered a description click "OK". At the next dialog (asking if you would like to recompile your package) click "Yes". Once the package is compiled and installed, save your package and unit.

At this point we have specified our base class, and also our new class name. We have created a new package to contain our component and have been presented with a skeleton class structure by Delphi.

If you look at the source code provided by Delphi you will now see the Private, Protected, Public, and Published sections.

   Encapsulation
Encapsulation is a simple concept to grasp, but extremely important to component writing. Encapsulation is implemented with the use of four reserved words: Private, Protected, Public, and Published, you will see the Delphi has included these sections automatically in the source code for your new component.

Section Accessibility
Private Methods, Properties and Events declared within this section will only be accessible to the unit the component is contained within. Components in the same unit can access each other's Private items.
Protected Methods, Properties and Events declared within this section will also accessible to any class descended from this class.
Public Methods, Properties and Events declared here accessible from anywhere.
Published This section allows you to declare properties / events that will appear in the object inspector. These settings are design time values that are saved with your project.

Next page > Raw Code > 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
Name
Email

 Got some code to share? Got a question? Need some help?
Explore Delphi Programming
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming

©2009 About.com, a part of The New York Times Company.

All rights reserved.