1. Technology
Advanced editing - TadpEdit component
Full source code of a TadpEdit Delphi component, an extension to the standard TEdit control with few powerful properties: ColorOnEnter (background color in focused state), Alignment (determines text alignment), and TabOnEnter (transforms Enter keypress to Tab).
 Win prizes by sharing code!
Do you have some Delphi code you want to share? Are you interested in winning a prize for your work?
Delphi Programming Quickies Contest
 More of this Feature
• Download FULL source
 Join the Discussion
"Post your views, comments, questions and doubts to this article."
Discuss!
 Related Resources
• Building Delphi components - tutorials
• Free source Delphi components

TEdit - Delphi wrapper for a Windows single-line edit control
The standard TEdit Delphi component is designed to enable users of an application enter and edit text in a single-line edit control. In most occasions the TEdit does its job perfectly, but there are some situations when more power is needed - when certain (specific) properties/methods/events could come handy.
For example, sometimes you'll want to enable only numerical input in a TEdit component - one solution is to create your own TNumEdit - to restrict the input it will access.

TadpEdit - advanced editing
In this article I'll show you how to enhance the standard TEdit component by adding several new properties. The TadpEdit component has three more handy properties: ColorOnEnter, Alignment and TabOnEnter.

  • ColorOnEnter property lets you specify the background color of the edit control when it has the input focus (blinking insertion point displayed). This property ensures more user-friendly interfaces for your data entry forms: where all not-focused edit control have (default) white background, but only the one with the input focus provides a different back color.
  • Alignment property determines how the text in the edit component is aligned. Note that several data entry controls have this property, but the standard TEdit does not expose.
  • TabOnEnter property allows the edit control to react on the Enter key press as if the Tab key was pressed - sending the focus to the next control in the tab order.

Of course, I'm providing the full source code for download.

Customizing the TEdit
The easiest way to create a new Delphi component is to derive it from a component that does nearly everything you want, then make whatever changes you need. Developing Delphi components isn't a simple task, it involves quite a bit of knowledge of the VCL. However, developing custom components is not an impossible task - your starting point could be our "Everything about creating custom components in Delphi" tutorials collection.

TadpEdit in ObjectInspector Let's consider our first "advanced" property: ColorOnEnter. As explained above, the idea is to create a property that enables a user (developer) of your component to specify the background color of the edit control when it has the input focus. If a developer wants to add this behavior to a standard Delphi TEdit control, he/she would have to attach a common OnEnter / OnExit event handler to each edit box in a data entry form. The code handling the OnEnter event would have to change the background color (that is the Color property) and the OnExit event handling procedure would have to restore the original (meaning the background color for non-focused state) Color of a TEdit component.

Overriding virtual methods
Obviously, to add the ColorOnEnter property, you need to customize the internal handling of the OnEnter and OnExit events of a standard TEdit control. And as you know, most events are fired (by the VCL) after a specific Windows message has been sent to a control.
Now, this can be done in two ways. First, you can drill down to the specific Windows message that gets fired when a control receives the input focus (WM_SETFOCUS) and handle the message on your own (by raising the OnEnter event and much more).
Another approach, the recommended one, is to override a specific dynamic method of the base class. These "specific" methods are used in the VCL to actually raise an event after the Windows message has been handled. In the VCL such method names are often (and unfortunately not always, as you will see) prefixed with a "Do". The protected "DoEventName" method allows derived classes to override the event without having to duplicate and customize the message handling code.

What this means is that in order to add specific code for the OnEnter and OnExit events, you'll need to override the DoEnter and DoExit dynamic methods (introduced in the TWinControl class). Now you could ask yourself: but how do I know or how do I find out the exact name of that dynamic protected method I need to override? Well, this is the tricky part ... you'll need to have the access to the VCL source (and probably you do if you are not using the Personal Delphi version) ... and you'll have to dig and explore it a little (read: "a lot with understanding").

TadpEdit - advanced TEdit
I felt I needed to share with you the above (overriding virtual methods) discussion - it seems (from my mailbox) many developers want to add specific properties to Delphi components but don't know from where to begin.

I use this component every day, the users of my applications really love that ColorOnEnter behavior; some "older" user still prefer to move to next control by hitting the Enter key (TabOnEnter handles that) and finally the Alignment property is useful when entering numerical data (to right align the text).
The TadpEdit derives from the standard TEdit Delphi component.

Installing into a Component palette
First, download the component. The TadpEdit comes as a single unit file (.pas extension). You'll need to add the component into an existing package. Here's "How to Install Custom Component in Delphi (into Existing Package)"

TadpEdit - example application
Here's a sample example of using the TadpEdit component:

adpEdit in action: 1  adpEdit in action: 2  adpEdit in action: 3 

Questions? Comments? Extensions?
That's it. If you find this component practical and if you extend it by adding more properties, please send your source and make it available to other developers.
As always if there are any questions or comments please post them on the Delphi Programming Forum.

~ Zarko Gajic

©2014 About.com. All rights reserved.