Delphi Programming
Advertisement

This article on delphi.about.com gives a quick description on how to create component templates but doesn't really tell why it helps you.

Component templates are a way to quickly create a modified version of a standard component without writing any code. Let's assume you write lots of dialogs which contain an OK and a Cancel button (and who doesn't?). Now, doing it the classic way, you would have to do the following for each of these dialogs:

  1. Add a TButton from the component palette to your form
  2. Change its caption to "OK"
  3. Change its name to "b_OK" (or whatever your usual name for the OK button is)
  4. Change its Default property to true

Now, if you had a component template for an OK button, you could reduce this list to only step 1.

To create a component template for an OK button

  1. Follow the steps above to create the OK button on a form
  2. In the "Component" menu select "Create Component Template"
  3. Give it the name TOkButton (or whatever you prefer)
  4. Enter the name of the palette page on which you want your template to appear (default: Templates)
  5. Press OK

Now, remove your button from the form, switch to the palette page you just specified and, voila there is a TOkButton component. If you drop that component on your form, you will find, that it adds a preconfigured OK-Button.

The same goes for the Cancel button:

  1. Add a TButton from the component palette to your form
  2. Change its caption to "Cancel"
  3. Change its name to "b_Cancel"
  4. Change its Cancel property to true

Now, just create a similar TCancelButton component template and with dropping two components on a form you get a dialog with a default OK button and a Cancel button that is pressed using the Esc key.

But we can do even better:

What about adding both buttons at once? This is also possible because a component template also allows multiple components in a single template:

  1. Drop a TOkButton and a TCancelButton on your form
  2. Arrange them to your liking
  3. Select both and create a new component template TOkCancelButtons for them

Now you can add an OK and a Cancel button with just dropping one TOkCancelButton on your form.

Is this cool or what?

Of course you are not limited to buttons. You could for example create a labeled edit (but later Delphi versions already provide a TLabeledEdit component) or a labeled memo, e.g. TResultMemo for displaying the output of your program.

Advertisement