Working in the IDE Environment Reusing Forms lazarus / 13-General ---------------------------------------------------------------------- flywire · 2015-03-04 01:51 How do you manage working on a program and testing a piece of code? For example, I am working on a program and I find some sample code that has something that I would like to include in my program. Currently I add a bodgy procedure or close my program, load the sample code into a new test program, copy the line(s) that I like then reopen my program and insert them. I would prefer a process where I could test some code without affecting my current program until I am ready to change it. ---------------------------------------------------------------------- Leledumbo · 2015-03-04 05:41 open in another lazarus instance ---------------------------------------------------------------------- flywire · 2015-03-24 11:53 I seem to be plagued by ghosts in the IDE. I have MMW.lpr, Unit1.pas, Unit3.pas and Form1.lfm in the IDE How do I get the following error: Code: [Select]Compile Project, Target: MMW.exe: Exit code 1, Errors: 1 unit3.pas(27,0) Error: Can't open resource file "C:\Users\Fearless User\Documents\Lazarus\MMW\unit3.lfm" The first time I saw error was when I added Form1.lfm to a project but it came in as unit4.pas rather than a form. I have since deleted everything in the directory and restarted but the unit2.lfm ghost returns. Version #:1.4RC2, Date: 2015-03-01, FPC Version: 2.6.4, SVN Revision: 48058, i386-win32-win32/win64 Intel Core duo running Win7 Edit: Deleted the units because they only confused. ---------------------------------------------------------------------- flywire · 2015-03-24 12:01 It has got something to do with the {$R *.lfm} culpret in unit3.pas. How do I add an old form to a project? ---------------------------------------------------------------------- JuhaManninen · 2015-03-24 12:46 Quote from: flywire on March 24, 2015, 01:01:22 pmIt has got something to do with the {$R *.lfm} culpret in unit3.pas. How do I add an old form to a project? Add it to a GUI LCL application, not to a command line program. ---------------------------------------------------------------------- flywire · 2015-04-14 08:20 Quote from: JuhaManninen on March 24, 2015, 01:46:41 pmAdd it to a GUI LCL application, not to a command line program. Can someone explain? I am currently just putting the MMW.lpr, Unit1.pas, Unit2.pas and Form1.lfm in an empty directory then double clicking on MMW.lpr to open the program in the IDE. The form gets attached to the Source Editor not the Project Forms. Version #:1.4RC2, Date: 2015-03-01, FPC Version: 2.6.4, SVN Revision: 48058, i386-win32-win32/win64 Intel Core duo running Win7 ---------------------------------------------------------------------- howardpc · 2015-04-14 08:43 Quote from: flywire on April 14, 2015, 10:20:31 amCan someone explain? I am currently just putting the MMW.lpr, Unit1.pas, Unit2.pas and Form1.lfm in an empty directory then double clicking on MMW.lpr to open the program in the IDE. Without adding Unit2.lfm, the Lazarus form streaming system has no way to instantiate a valid TForm1. Its property values are set at load time. Your unit2 (or is it Unit3 - your posts are inconsistent?) includes the instruction: {$R *.lfm} However, there is no unit2.lfm to read. Hence the loading error. The message is clear and to the point. You must either add the appropriate .lfm file, or comment the read directive thus: //{$R *.lfm} and build a resourceless form yourself starting with TForm1.CreateNew(). ---------------------------------------------------------------------- flywire · 2015-04-14 11:50 Sorry, this is not clear yet. 1. The original post is six weeks old and I can't explain the contents of those two units as they don't contain what I expected in them which must have something to do with my attempt to bring the form in. 2. The current situation is that I have the three files in reply#6 htt p://forum.lazarus.freepascal.org/index.php/topic,27584.msg174478.html# msg174478. 3. My process is: Start Lazarus IDE. It seems to hold the last program files in the application so I go Project, New Project, Application to clear the existing files. Then File, Open MMW.lpr, OK to the message to close current project and create new Lazarus project, Application, OK. 4. I can press run button to compile and run the program okay but it is still the only file in the source editor.  It recognises unit1.lfm from the {$R *.lfm} line and there is no form used in unit2.pas. File Open loads the two Unit?.pas files into the Source Editor. 5. >> All I am trying to achieve is to get the form to appear under View Project Forms so that I can use it with View Forms like I do with View Units. Quote from: howardpc on April 14, 2015, 10:43:30 am... You must either add the appropriate .lfm file, or comment the read directive thus: //{$R *.lfm} and build a resourceless form yourself starting with TForm1.CreateNew(). I don't understand how to add Unit1.lfm.  I didn't //{$R *.lfm} in Unit1.pas. All I seem to be able to do is create a new Form1 and when I go File, Save All the IDE it saves Unit3.pas, then File Quit. The new form is unit3.lfm. When I open Lazarus IDE again Unit3.lfm is in the View [Project] Forms but not Unit1.lfm. ********** 6. Back to Step 3 then File, Open load the two Unit?.pas files into the Source Editor.  Insert //{$R *.lfm} in Unit1.pas. File, New Form, File Quit, OK to save changes to project MMW, Save Unit3.pas. Same as above. ---------------------------------------------------------------------- howardpc · 2015-04-14 14:28 You've deleted the various file attachments that showed the problem, so it's hard to be specific. In general, you're far better off starting a new project and generating new forms as needed, rather than using forms from other projects. This is certainly possible, but it has several possible pitfalls, and if there is the slightest problem, things quickly go from bad to worse, as you have found. It seems somewhere you have included a pre- existing form unit file (pas or pp) in a new project without including that form's lfm. This screws Lazarus up royally. Because a form unit is inseparably bound to its lfm resource definition Lazarus normally autogenerates the lfm and keeps it continuously in sync with changes you make in the source editor to the Pascal code (and does not let you see the lfm at all unless you specifically request to view it). Attempting to reuse a .pas form file without its completely-up-to-date accompanying .lfm, or changing the .lfm yourself (rather than letting Lazarus do this automatically) is not a good way of developing unless you really understand the internals of how Lazarus synchronises the pas and lfm, and the effects of the file caches it holds. You will find it far simpler to abandon those files (lpr, pas, lfm) that you posted here, together with their accompanying lpi and lps, and start afresh. Sometimes a new build from scratch is far easier than a repair. ---------------------------------------------------------------------- mas steindorff · 2015-04-14 19:05 Quote from: flywire on March 04, 2015, 02:51:04 amHow do you manage working on a program and testing a piece of code? ... in general, what the others have said is true, but here is what I do for testing which may be all your after... I've created  /common and a /diag folders even with the project folder. In the /diag i've created the testing project (and it's main form). I put the various units and forms in the /common folder and add "..\common" to both projects Paths:other unit files field.  then as I need them I add each unit/form to the main project or the diag.  Using this approach, some times the different "open form" will not be able find the form but you can open them via the menu File:open option.  once showing, just click project:add editor file to project and your ready to go. another side effect is that you will need to do a clean and build (or just delete the compiler output files) each time you switch projects.  with 1.2.6, some times the compiler will not see the files in the /common folder have changed.  if you are looking to include several items onto one form, have a look at "Frames" Mas ---------------------------------------------------------------------- flywire · 2015-04-14 21:45 I do understand there is more to forms than the *.lfm file (but I don't understand why I can't add a form and have it show in the View Project Forms window, even if I have to manually add the declaration to a unit to call the form). Quote from: mas steindorff on April 14, 2015, 09:05:20 pmQuote from: flywire on March 04, 2015, 02:51:04 amHow do you manage working on a program and testing a piece of code? ... ... here is what I do for testing which may be all your after... Certainly, this is the main issue. I will try the approach you have suggested. Can I get a comment on the following process to ensure that I clear the existing files from the IDE when opening a program? Quote3. My process is: Start Lazarus IDE. It seems to hold the last program files in the application so I go Project, New Project, Application to clear the existing files. Then File, Open MMW.lpr, OK to the message to close current project and create new Lazarus project, Application, OK. ---------------------------------------------------------------------- howardpc · 2015-04-14 22:10 It is better to think in terms of projects rather than think in terms of .lpr files, which are only one small part of a Lazarus project. I suggest you: Open the IDEClose any open project (Project, Close project)This gives you the Project Wizard. Choose either New Project, or Open Project (Open Recent Project saves you navigating to other folders to open an existing project). Leave Lazarus to manage the lpr file. If you need to edit it, choose Project->View project source. But don't use an lpr file as your entry point to a Lazarus project. If you insist on opening Lazarus by clicking on a file in your OS's file browser, click on an .lpi (project) file to open that project. ---------------------------------------------------------------------- mas steindorff · 2015-04-14 23:22 I've not had any issue switching between projects. (i.e. I don't close the current one, I just go directly to opening the next).  I may have been lucky  what I do to "clear" is simply go into the output folder //lib (defined in the project path panel) and delete everything.  the .lrs, the .o, .ppu, ... it should all be rebuilt the next time you compile. as far as the goes, The forms all show up in the list after I and compile (I think).  I just looked and can see them all there now. I really don't know when they showed up in that list since I use the [F12] shortcut to switch between unit and it's form. ---------------------------------------------------------------------- flywire · 2015-04-15 11:52 The Lazarus wiki Documentation (http://wiki.freepascal.org/Lazarus_Doc umentation#Lazarus_and_Pascal_Tutorials) lists Lazarus Programming A series of tutorials to get beginners started. Limited "how to install" material. From SheepdogSoftware.co.uk http://sheepdogguides.com/lut/ One of the Level 2 Tutorials: Re-using a Lazarus project is as follows: Quote... copy all parts of source project, to the new folder. (The "backup" and "lib" folders do not need to be copied ... Double-click on the .lpr file ... (a conversation there for howardpc) Use the Object Inspector to change the name of the form ... Click "File | Save As", and save the unit ... To save the project specification (with the new base name), use the "Project" menu item. "Project | Save As... Now, to see if you were successful ... refer to http://sheepdogguides.com/lut/lt2c.htm. This seems to be mas steindorff's approach, and howardpc's too with the exception of the double click on the *.lpi. ie copy all of the files in the directory excluding the subdirectories and *.exe. I appreciate your explanations. Now if you only copy the *.lpr, *.pas and *.lfm the program will function properly except the [F12] will bring up the form but not in the View Project Forms window unless you also copy the *.lpi (thanks howardpc). I can't see that there is a need to copy the *.lps or *.res files and I understand that if the *.ico file is not copied then the default icon will be used for the compiled file. This is not really what I want because I only want to copy the unit and the forms I am interested into the other project. Given that everything seems to work without the *.lpi file except the form appearing in the View Project Forms window I suspect that this is a bug and would be interested to know how it works in Delphi. As a feature enhancement - why can't the IDE work so that the form be brought into the current unit with file open and appear in the View Project Forms window? In the interim I will try adding the unit, comment out the form, add a dummy form, compile, replace dummy form with required form. ---------------------------------------------------------------------- howardpc · 2015-04-15 15:05 Quote from: flywire on April 15, 2015, 01:52:54 pmAs a feature enhancement - why can't the IDE work so that the form be brought into the current unit with file open and appear in the View Project Forms window? In the interim I will try adding the unit, comment out the form, add a dummy form, compile, replace dummy form with required form. The IDE handles existing forms perfectly well already. You don't need any of that convoluted nonsense of adding a unit, commenting out the form, and adding a dummy form. If there is an existing form file you want to add to your project, simply open its pas (or pp) source in the editor. Then in the Project Inspector use the Add... button (the one with the 'doubleplus' icon) to open the three-page Add to Project dialog. Pick the form pas file you want from the list under the Add Editor Files tab, and click Add Files. This form is now part of your project, and shows in the Project->Forms list. ---------------------------------------------------------------------- flywire · 2015-04-15 22:07 I would like a demonstration with these three files from http://code- sd.com/startprog/StartProgUsingPascal.pdf: Code: [Select]program FirstGui; {$mode objfpc}{$H+} uses   {$IFDEF UNIX}{$IFDEF UseCThreads}   cthreads,   {$ENDIF}{$ENDIF}   Interfaces, // this includes the LCL widgetset   Forms, Unit1   { you can add units after this }; {$R *.res} begin   RequireDerivedFormResource := True;   Application.Initialize;   Application.CreateForm(TForm1, Form1);   Application.Run; end. Code: [Select]unit Unit1; {$mode objfpc}{$H+} interface uses   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls; type   { TForm1 }   TForm1 = class(TForm)     Button1: TButton;     procedure Button1Click(Sender: TObject);   private     { private declarations }   public     { public declarations }   end; var   Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.Button1Click(Sender: TObject); begin   ShowMessage('Hello world, this is my first GUI application' ); end; end. Code: [Select]object Form1: TForm1   Left = 411   Height = 240   Top = 166   Width = 320   Caption = 'Form1'   ClientHeight = 240   ClientWidth = 320   LCLVersion = '1.4.0.2'   object Button1: TButton     Left = 10     Height = 25     Top = 10     Width = 75     Caption = 'Click'     OnClick = Button1Click     TabOrder = 0   end end ---------------------------------------------------------------------- JuhaManninen · 2015-04-15 22:19 The most important file, the project .lpi file is missing from your list. It contains for example the dependence for LCL library which is needed for form loading. ---------------------------------------------------------------------- flywire · 2015-04-16 03:19 Quote from: JuhaManninen on April 16, 2015, 12:19:43 amThe most important file, the project .lpi file is missing from your list. It contains for example the dependence for LCL library which is needed for form loading. I agree but typically, as with other authors, it wasn't provided. Still the objective is to open up the project with the form available in the View Project Forms window so that it functions in the IDE the same as projects created in the IDE from the start. ---------------------------------------------------------------------- taazz · 2015-04-16 03:32 Quote from: flywire on April 16, 2015, 05:19:08 amQuote from: JuhaManninen on April 16, 2015, 12:19:43 amThe most important file, the project .lpi file is missing from your list. It contains for example the dependence for LCL library which is needed for form loading. I agree but typically, as with other authors, it wasn't provided. Still the objective is to open up the project with the form available in the View Project Forms window so that it functions in the IDE the same as projects created in the IDE from the start. the file names are the default ones that the IDE assigns when a new application is created. Start a new application from the ide save everything close the IDE and replace the unit1.pas and unit1.lfm with the ones provided I doubt that the first gui has anything in it that the IDE does not put there so ignore it for now. Reopen the IDE and load the project you previously saved. You should now see what ever the author had in mind, although I have to say that in your case I would try to recreate it manually placing the components on the form my self in order to better understand what the IDE has to offer.