Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include different form (.dfm) file based on a conditional define

We produce two versions of our software for two slightly different versions of machine. The hardware used on the two machines is sufficiently different that we maintain two projects, Project1 and Project2. Some code (.pas and .dfm, as appropriate) is shared between the two projects, but most code is currently unique to Project1 or Project2.

Project2 was initially cloned from Project1 to get the new machine up-and-running, but now I'm in the process of restructuring the code so forms and units can be made common between the two projects to reduce development effort. Conditional defines, as in {$IFDEF PROJ1}, are used where required.

We have one form that is almost identical between the projects, apart from one TCheckBox which is in Project1 but not Project2. Lets call this form Masking with unit file Masking.pas (and Masking.dfm).

Steps:

  • Moved the .pas and .dfm from Project1 into the common shared folder
  • Renamed Masking.dfm to MaskingProj1.dfm
  • Copied Masking.dfm from Project2 into the common shared folder and renamed to MaskingProj2.dfm
  • Manually edited the project (.dpr) files for each project to reflect the changes
  • In Masking.pas, changed {$R *.dfm} into:

    {$IFDEF PROJECT1} {$R MaskingProj1.dfm} {$ELSE} {$R MaskingProj2.dfm} {$ENDIF}

  • Also in Masking.pas, used the {$IFDEF PROJECT1} conditional define to compile out the TCheckBox control and associated code for Project2.

Finally, re-open both projects and re-compile. Hey presto! it works. Now Project1 uses Masking.pas and MaskingProj1.dfm. Project2 uses Masking.pas and MaskingProj2.dfm.

This all appears to works fine...except that when I try and view the form in the IDE, by switching between form and unit (F12), nothing is displayed. I can manually edit either .dfm file and the changes are reflected in the application after recompilation...but the IDE won't show the form.

What am I doing wrong? Is it possible to change {$R *.dfm} in this way to get the .pas file to use a different form file, depending on a conditional define?

like image 324
AlainD Avatar asked Jan 29 '23 12:01

AlainD


1 Answers

This is a typical case for form inheritance.

Create a common ancestor form containing all controls needed for both of the projects. Now inherit this form for Project1 and add those controls needed only for Project1. Then do the same for Project2 (probably no controls to add here).

Both projects contain the common form, but each project contains only that inherited form suitable for said project.

Although you can omit the inherited form for Project2 here, I suggest to do it this way for clarity.

like image 182
Uwe Raabe Avatar answered Mar 05 '23 14:03

Uwe Raabe