_I_Play_Chess
11-17-2003, 07:15 PM
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD colSpan=2>Code Completion in Delphi <!-- End of Headline --></TD></TR>
<TR>
<TD bgColor=#cc0000 colSpan=2 height=1><IMG height=1 src="http://images.about.com/all/bullets/dot_clea.gif" width=1 border=0></TD></TR>
<TR>
<TD colSpan=2>It's time to learn how to let Delphi help you code faster: start using code templates, code insight, code completion, shortcut keys, ... <!-- End of Subhead --></TD></TR>
<TR>
<TD colSpan=2><IMG height=6 src="http://images.about.com/all/bullets/dot_clea.gif" width=1></TD></TR></TBODY></TABLE>Welcome to the <B>12ves chapter</B> of the FREE online programming course:<B>How to code faster and more efficiently</B>
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: <B>Delphi Code Insight!</B>
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:
<H5 style="PADDING-LEFT: 10px; MARGIN-BOTTOM: 0px">Code Completion</H5>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.
<CENTER><IMG height=196 alt="Code Completion" src="http://delphi.about.com/library/graphics/abg1301.gif" width=324></CENTER>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. <B>"Unable to invoke Code Completion due to errors in source code"?</B>
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.
<H5 style="PADDING-LEFT: 10px; MARGIN-BOTTOM: 0px">Code Templates</H5>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.
<CENTER><IMG height=193 alt="Delphi Code Templates" src="http://delphi.about.com/library/graphics/abg1302.gif" width=323></CENTER>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.
<H5 style="PADDING-LEFT: 10px; MARGIN-BOTTOM: 0px">Code Parameters</H5>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.
<CENTER><IMG height=193 alt="Delphi Code Parameters" src="http://delphi.about.com/library/graphics/abg1303.gif" width=322></CENTER>
<B>Tooltip symbol insight, Tooltip expression evaluation</B>
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.
<H5 style="PADDING-LEFT: 10px; MARGIN-BOTTOM: 0px">Class Completion</H5>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).
<TABLE width="100%" bgColor=#ffffcc border=1>
<TBODY>
<TR>
<TD><PRE>...
<B>public</B>
<B>property</B> MyProp : integer <B>read</B> fProp <B>write</B> SetProp; CTRL + SHIFT + C
...
</PRE></TD><PRE></PRE></TR></TBODY></TABLE>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:
<TABLE width="100%" bgColor=#ffffcc border=1>
<TBODY>
<TR>
<TD><PRE>...
<B>private</B>
fProp: integer;
<B>procedure</B> SetProp(<B>const</B> Value: integer)
...
//implementation
...
<B>procedure</B> TForm1.SetProp(<B>const</B> Value: integer);
<B>begin</B>
fProp := Value;
<B>end</B>;
</PRE></TD><PRE></PRE></TR></TBODY></TABLE>
<H5 style="PADDING-LEFT: 10px; MARGIN-BOTTOM: 0px">Code Insight Settings</H5>Options for turning Code Insight tools on and off are located on the Code Insight page of the Tools | Editor Options dialog.
<CENTER><IMG height=367 alt="Delphi Code Insight Settings" src="http://delphi.about.com/library/graphics/abg1300.gif" width=390></CENTER>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.
<H5 style="PADDING-LEFT: 10px; MARGIN-BOTTOM: 0px">Some exercises for you...</H5>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.
<P clear=all> <B>To the next chapter: A Beginner's Guide to Delphi Programming</B>
This is the end of the twelves chapter, in the next chapter, we'll deal with ... well I don't know yet...
<TBODY>
<TR>
<TD colSpan=2>Code Completion in Delphi <!-- End of Headline --></TD></TR>
<TR>
<TD bgColor=#cc0000 colSpan=2 height=1><IMG height=1 src="http://images.about.com/all/bullets/dot_clea.gif" width=1 border=0></TD></TR>
<TR>
<TD colSpan=2>It's time to learn how to let Delphi help you code faster: start using code templates, code insight, code completion, shortcut keys, ... <!-- End of Subhead --></TD></TR>
<TR>
<TD colSpan=2><IMG height=6 src="http://images.about.com/all/bullets/dot_clea.gif" width=1></TD></TR></TBODY></TABLE>Welcome to the <B>12ves chapter</B> of the FREE online programming course:<B>How to code faster and more efficiently</B>
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: <B>Delphi Code Insight!</B>
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:
<H5 style="PADDING-LEFT: 10px; MARGIN-BOTTOM: 0px">Code Completion</H5>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.
<CENTER><IMG height=196 alt="Code Completion" src="http://delphi.about.com/library/graphics/abg1301.gif" width=324></CENTER>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. <B>"Unable to invoke Code Completion due to errors in source code"?</B>
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.
<H5 style="PADDING-LEFT: 10px; MARGIN-BOTTOM: 0px">Code Templates</H5>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.
<CENTER><IMG height=193 alt="Delphi Code Templates" src="http://delphi.about.com/library/graphics/abg1302.gif" width=323></CENTER>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.
<H5 style="PADDING-LEFT: 10px; MARGIN-BOTTOM: 0px">Code Parameters</H5>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.
<CENTER><IMG height=193 alt="Delphi Code Parameters" src="http://delphi.about.com/library/graphics/abg1303.gif" width=322></CENTER>
<B>Tooltip symbol insight, Tooltip expression evaluation</B>
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.
<H5 style="PADDING-LEFT: 10px; MARGIN-BOTTOM: 0px">Class Completion</H5>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).
<TABLE width="100%" bgColor=#ffffcc border=1>
<TBODY>
<TR>
<TD><PRE>...
<B>public</B>
<B>property</B> MyProp : integer <B>read</B> fProp <B>write</B> SetProp; CTRL + SHIFT + C
...
</PRE></TD><PRE></PRE></TR></TBODY></TABLE>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:
<TABLE width="100%" bgColor=#ffffcc border=1>
<TBODY>
<TR>
<TD><PRE>...
<B>private</B>
fProp: integer;
<B>procedure</B> SetProp(<B>const</B> Value: integer)
...
//implementation
...
<B>procedure</B> TForm1.SetProp(<B>const</B> Value: integer);
<B>begin</B>
fProp := Value;
<B>end</B>;
</PRE></TD><PRE></PRE></TR></TBODY></TABLE>
<H5 style="PADDING-LEFT: 10px; MARGIN-BOTTOM: 0px">Code Insight Settings</H5>Options for turning Code Insight tools on and off are located on the Code Insight page of the Tools | Editor Options dialog.
<CENTER><IMG height=367 alt="Delphi Code Insight Settings" src="http://delphi.about.com/library/graphics/abg1300.gif" width=390></CENTER>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.
<H5 style="PADDING-LEFT: 10px; MARGIN-BOTTOM: 0px">Some exercises for you...</H5>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.
<P clear=all> <B>To the next chapter: A Beginner's Guide to Delphi Programming</B>
This is the end of the twelves chapter, in the next chapter, we'll deal with ... well I don't know yet...