4

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?

8
  • 4
    I doubt that the IDE is capable of understanding the conditionals and editing different DFMs depending on them. The compiler has no problems with this, but the IDE has. Mar 11, 2018 at 2:07
  • 1
    Conditional defines only work when compiling, not in the IDE.
    – Ken White
    Mar 11, 2018 at 3:50
  • You could use plugin architecture to solve the problem. Eg. create two DLLs that implement a common interface and have a ShowDialog function in the DLL that creates the form. You could also add GetData and SetData functions to get and set data from the form.
    – quasoft
    Mar 11, 2018 at 4:56
  • 3
    With the only difference being the presence of a TCheckBox, I would simply Hide or Show it in the forms OnCreate() event with an {ifdef} block . Mar 11, 2018 at 5:15
  • 2
    As a recommendation, don't do this unless absolutely necessary. I inherited a system where someone had done this due to some issues when wrapping in a COM object. I eventually rewrote to create the problematic controls programmatically.
    – Gerry Coll
    Mar 11, 2018 at 7:20

1 Answer 1

8

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.

1
  • Form inheritance is a good solution going forward. But since these forms are used in two distinct projects, we have the downside that you still need two DFM files. The suggestion from Tom Brunberg (see comments) of using a {$IFDEF} to show or hide the checkbox is cleaner in my circumstances. Since Tom has not written this up as a possible answer, I'll accept yours...and hope this and Tom's comments serve as a useful addendum to anyone seeking a solution to this type of problem.
    – AlainD
    Mar 12, 2018 at 10:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.