1. Home
  2. Computing & Technology
  3. Delphi Programming
Code Completion in Delphi
It's time to learn how to let Delphi help you code faster: start using code templates, code insight, code completion, shortcut keys, ...
 More of this Feature
Printer friendly versionPrinter friendly version
 Join the Discussion
"Post your views and comments to this chapter of the free Delphi Programming Course"
Discuss!
 Related Resources
• A Beginner's Guide to Delphi Programming.TOC

• Code navigation in Delphi
• Delphi IDE articles
• OOP in Delphi
• Top Delphi Add-On
 Elsewhere on the web
• Delphi 6 Code Completion missing feature
• Delphi editor Keyboard shortcuts

Welcome to the thirteenth chapter of the FREE online programming course:
A Beginner’s Guide to Delphi Programming.
It's time to learn how to let Delphi help you code faster: start using code templates, code insight, code completion...

How to code faster and more efficiently
So, you've managed to develop several (great) applications using Delphi, but whenever you need to add a try/finally/end block you write the entire "try finally end" command? Do you know that you can do the same by typing "tryf" and than hit Ctrl + J?

And how about writing the MessageDlg function? Do you know all the parameters (and their order) this method expects? I do not know, neither do I tend to remember each functions parameter list. I simply write "MessageDlg" and hit Ctrl + Space + Shift: the list of all the parameters gets shown in a tool tip.

The answer is: Delphi Code Insight!
Delphi, from version 3, provides a very useful functionality called code insight - a set of tools available while you are working in the Code editor.
The Code Insight comprises a number of Delphi IDE Code editor features:

Code Completion
This handy feature automatically suggests methods, properties and events to insert in your code depending on what you are doing. This works for your own classes as well as standard methods etc. What this means, is that when you start a code line by typing the name of an object (Edit1, for example) followed by a period (dot), Delphi will display a list box with all the properties of the edit box you can pick. In general, it lists valid elements that you can select from and add to your code - you can then select the item and press Enter to add it to your code.

Code Completion

Note: you can always invoke Code completion using Ctrl + Space.

Another example includes the following situation:
If you have defined several string variables and several integer variables in your unit, when you start a command like "Form1.Caption:=" and hit Ctrl + Space, a list of arguments that are valid for the assignment is displayed. The list will contain (among others) string variables which you have defined, but not the integer ones.

When the code completion list is displayed, you can hold down the Ctrl key and click on any identifier in the list to browse to its declaration (if you have the sources of Borland Delphi units, you can see "inside" Delphi). Also, in the Code editor, if you hover the mouse pointer over the identifier, a hint window tells where it is declared. You can press Ctrl, point to the identifier in the code and click to move to its declaration.

"Unable to invoke Code Completion due to errors in source code"?
Uops, the code completion does not work! Now what?
The message 'Unable to invoke Code Completion' usually indicates some type of syntax error in your code. It can also indicate that you have not defined the method you are working in. Almost always the problem does exist but you might try recompiling the entire project to see if syntax errors do exist.

Code Templates
As suggested above, this feature can save you lots of keystrokes. Templates include commonly used programming statements (such as if, while, for and tyr/finally/end statements) - each statement has a shortcut, eg. "forb" is a for loop with begin end. You simply start a statement by typing several characters and press Ctrl+J and pick from a list of predefined code templates.

Delphi Code Templates

Another great example includes pressing Ctrl+J on a blank line, typing f, and double-clicking the selected function declaration - the function skeleton code will be inserted automatically at the current cursor position.

Code Parameters
This tool enables you to see the arguments of a method call as you enter them into your code. After typing a method and a '(', a description of the parameters will be displayed, as each parameter is input the next parameter to be input is highlighted in bold. Use Ctrl+Shift+Space to activate or reactivate if the tooltip disappears.

Delphi Code Parameters

Tooltip symbol insight, Tooltip expression evaluation
Great debugging tool. To test expression evaluation while the program is running "under" IDE, you stop the compiler (enter debug), and you can view the value of a variable by pointing to it with your cursor.
Symbol insight feature displays declaration information for any identifier by passing the mouse over it in the Code editor.

Class Completion
This is a "subset" of the code completion feature, designed to automate the definition of (new) classes by generating skeleton code for the class members. This is how it works:
You write a new property declaration inside a public section of the interface section of your class definition (Form1).

...
 public
  property MyProp : integer read fProp write SetProp;  CTRL + SHIFT + C
...

When complete hit Ctrl + Shift + C, Delphi automatically adds private read and write specifiers to the declarations, then adds skeleton code in the implementation section. This is what gets added by Delphi:

...
 private
  fProp: integer;
  procedure SetProp(const Value: integer)
...
 //implementation
...
procedure TForm1.SetProp(const Value: integer);
begin
  fProp := Value;
end;

Code Insight Settings
Options for turning Code Insight tools on and off are located on the Code Insight page of the Tools | Editor Options dialog.

Delphi Code Insight Settings

Note that all the Code Insight tools have lot more usage ways. The list can be found on Delphi Help under "Code Insight" when you click Help in the above dialog.

Some exercises for you...
Hmm, simply start using Code insight features, you will not believe how much time you can save in coding, and of course you can drastically reduce the number of bugs in your code.

   To the next chapter: A Beginner's Guide to Delphi Programming
This is the end of the thirteenth chapter, in the next chapter, we'll deal with ... well I don't know yet...

If you need any kind of help at this point, please post to the Delphi Programming Forum where all the questions are answered and beginners are treated as experts.

A Beginner's Guide to Delphi Programming: Next Chapter >>
>> Making Forms Work - a Primer

Explore Delphi Programming
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

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

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

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

All rights reserved.