-2

I have a Delphi project with a lot of DFM files and matching PAS files. I can compile the PAS by just rebuilding the project, but how do I rebuild and keep the DFM files in sync with their PAS files?
Right now I am getting a lot of "[Variable.field] does not have a corresponding component. Remove the declaration?" prompts from the IDE.

  • 6
    The question is not how to keep the files in sync, but rather what you are doing to get them out of sync. The IDE won't do that. What are you doing to make the files be out of sync? – David Heffernan Apr 18 at 20:12
  • Indeed, this can happen if you manually make changes outside of the IDE. Just don't do it. Use the IDE for such modifications, and you won't have the problem to begin with. – Jerry Dodge Apr 18 at 20:23
  • 1
    Did you delete components manually from the forms published section in the .pas file instead of deleting it in the form designer? – Delphi Coder Apr 18 at 20:23
  • Are these forms and datamodules in your main project, or in packages? The latter does pose a problem. Forms in packages still require their (separate) dfm when you use them in the executable. Same goes for other resources. In my office there is one package that relies on such resources, and I had to implement a post-build event in it to make sure that the resources were also updated in the output directory of the package. – GolezTrol Apr 18 at 22:10
  • Haven't you noticed that if you delete the code from an event handler created via the Object Inspector and then Save, the event handler automagically disappears? – MartynA Apr 19 at 0:39
12

The solution is simple.

  1. Do not edit a .pas file outside the IDE if it has an associated .dfm.

  2. Do not edit the .dfm outside the IDE.

  3. Do not edit the section of the .pas file that is owned by the IDE, which is the part between the opening of the form class definition and the first private section.

    type
      TForm1 = class(TForm)
        // Form designer property - keep out
        // It is where the IDE adds components you drag/drop, event handlers 
        // generated via the Object Inspector or automatically by the IDE.
        // It is the published section, and is used by the VCL streaming
        // system to create controls that are on the form and attach event
        // handlers at runtime. Leave it alone. If you need something in
        // a published state, add a new published section below the private
        // section.
      private
        // Yours - edit all you want
      public
        // Yours - edit all you want
      end;
    
  4. If you need to delete a component from the form, do so by first removing code from any event handlers (you can find them using the Object Inspector), leaving just the empty method shell (consisting of the procedure name, followed by a begin, blank line, and end), and then select the component on the form and hit delete. The IDE will remove the declaration from both the first section of the .pas file and the .dfm and remove the empty event handlers.

  • 1
    WTG, @KenWhite, +1 – MartynA Apr 18 at 23:24
  • What if you add a variable to the class in the PAS file that is the class that the DFM uses, i.e. when you view the DFM as text the first line has for example – Tom Kelly Apr 22 at 19:52
  • The first line in my DFM file is, object frmAppTools: TfrmAppTools In my PAS file. I have added a few new variables to TfrmAppTools and those new variables are the ones that I am getting the red error window that says, "Field frmAppTools.newfield does not have a corresponding component. Remove the declaration?" so it seems like I need to tell the DFM that there are new variables in the TfrmAppTools class and it needs to reload it somehow. If I add a new variable to the TfrmAppTools class I will get a new red error window for that new variable. – Tom Kelly Apr 22 at 20:02
  • @TomKelly I don't know what new variables means. If you're adding components to the form, do it from the IDE. If you're adding new variables, then add them to the sections I indicate above, not to the form-managed published section. I've made it pretty clear in my answer where you are (and are not) allowed to manually edit the form code. The IDE handles telling the DFM that new components exist, and if it's not doing so then you're editing it improperly. Read my answer again, particularly the comments in the code. – Ken White Apr 22 at 21:06
  • I think what may be happening is that my Delphi project found an obsolete DCU in my multi-directory linking path before it found the DCU in my project directory. The DCU in my project is the one that matches my PAS source code. Thank you all for your responses. I have given + feedback for everyone. – Tom Kelly Apr 27 at 1:23
3

The simple answer is: Use the Delphi IDE for editing your form related .PAS files whose job it is to keep them in sync.

  • I think what may be happening is that my Delphi project found an obsolete DCU in my multi-directory linking path before it found the DCU in my project directory. The DCU in my project is the one that matches my PAS source code. Thank you all for your responses. I have given + feedback for everyone. – Tom Kelly Apr 27 at 1:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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