Design Patterns
  Runtime Packages
  Modal Windows
  Delphi Links

Delphi Tips and Source Code

Conference Papers and Presentations:

Links:

Quick Tips on This Page:

Speed Tips for the Delphi IDE

  1. The Repository lets you save forms and projects. Items can also be marked as the default for the new form or project menu items. In a team environment, a shared repository can help achieve a common look and feel throughout your application.
  2. For faster programming, changing colors in the IDE can really help to highlight your syntax and pinpoint coding errors more quickly. You can change colors for keywords, strings, numbers, punctuation, comments, etc. to help them stand out from the rest of the code. Colors are a personal choice, but here's a quick sample:

    To change the color settings, choose Options|Environment|Editor Colors in Delphi 1.0 or Tools|Options|Colors in Delphi 2.0.

  3. Editing can also be tailored with the Editor Options tab page. The most useful settings I've found are the following:

    Environment Options

    especially find text at cursor, the block indent and tab stop settings. Note with block indent you can also highlight a block in the code editor and use CTRL-K-I to indent it or CTRL-K-U to unindent. These keys save a heap of time typing.

  4. On the subject of editor keystrokes, there are other useful shortcuts to explore. If you're like me, it's easy to overlook them for the most part. Steven Jatnieks logged this great tip on the delphi-talk mailing list: "Delphi has macros. Press CTRL-SHFT-R to start/stop recording and CTRL-SHFT-P to play the keystroke."
  5. Optimize the component palette when designing. If you have a lot of components installed, you need all the space you can get. Drag the palette area over the top of the speedbuttons to make more visible space. You can customize the speedbuttons using the right mouse button - get rid of the ones you never use to free up space for the palette.
  6. Palette pages are also fully customizable. It really can save time to reorganize the palette into the components you use most frequently. Use Options|Environment|Palette to get to the palette editor, then add, delete or reorder pages and components. You can include the same component on more than one page, by dragging the component from the [Library] page to a new page.
  7. Use the desktops feature in Delphi 5 and up to save frequently used window configurations for coding and debugging.

A Button Class to Automatically Create Forms

This example creates a handy button component that can be used to automatically create and show forms. It uses properties and events to set the form name and to show the form modally.

We're essentially using the same approach that Delphi uses in the Application.CreateForm method. If you have the VCL source you might want to check it out.

To dynamically create an object using a text string of its class name, you need to first obtain an object-type reference. This can be done using the FindClass or GetClass functions. These return a TPersistentClass type. To create forms on the fly, you need to recast this as a TFormClass.

Once you have this class reference you can use Application.CreateForm or the Create method to create a new object, for example:

var
  FormName: string;
  AForm: TForm; 
begin
  Application.CreateForm(TFormClass(FindClass('T'+FormName)), AForm);
end; 

There is one catch to using FindClass. Before your class can be found it needs to be registered with Delphi, otherwise you get a "Class ... not found" error. The best place to register the class is in the unit that defines the class. Include the following code in the initialization section at the end of the unit: 

initialization
 RegisterClass(TForm2); 
end. 

For a button component, we might want to modify this to make the button the owner of the form. The following extract from our finished component adds the following code to the button's Click method: 

procedure TFormButton.Click; 
var
  AClass: TPersistentClass;
  AFormClass: TFormClass; 
begin
  inherited Click;
  if FFormName = '' then Exit;
  if FForm = nil then
  begin
    AClass := FindClass('T' + FFormName);
    AFormClass := TFormClass(AClass);
    FForm := AFormClass.Create(Self);
  end;
  if FShowModal then
    FFormModalResult := FForm.ShowModal
  else
    FForm.Show;
  if Assigned(FAfterShow) then FAfterShow(Self);
end; 

You can see we've also added some properties and code to show the form either modal or non-modal, and to execute an event after showing the form. This event is essential for a modal form where you want to do something after the form is hidden or destroyed.

The full component source includes additional runtime properties and methods to access the form.

How to Save/Restore Forms With a Binary File

A common method of saving form properties such as position and size is to write the values to an INI file when destroying the form and to read them from the file again when creating the form. However with this approach you need to itemise the properties you want to save, and this method provides no help for components owned by the form.

An alternate approach is to use Delphi's own streaming methods. This allows you to save/restore an entire form to a binary resource file, including the properties of its owned components. This example is fairly large, so we've included it as a zip file you can download. The full source includes a sample project illustrating the use of this technique.

If you would like to contribute resources to this page, please send them to us at webmaster@obsof.com.

  2000 Objective Software Technology Pty Limited