Recent

Author Topic: Working in the IDE Environment Reusing Forms  (Read 10139 times)

flywire

  • Jr. Member
  • **
  • Posts: 85
Working in the IDE Environment Reusing Forms
« on: March 04, 2015, 02:51:04 am »
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.
« Last Edit: April 14, 2015, 01:45:17 pm by flywire »

Leledumbo

  • Hero Member
  • *****
  • Posts: 8747
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Working in the IDE Environment
« Reply #1 on: March 04, 2015, 06:41:21 am »
open in another lazarus instance

flywire

  • Jr. Member
  • **
  • Posts: 85
Re: Working in the IDE Environment
« Reply #2 on: March 24, 2015, 12:53:17 pm »
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.
« Last Edit: April 14, 2015, 01:42:00 pm by flywire »

flywire

  • Jr. Member
  • **
  • Posts: 85
Re: Working in the IDE Environment
« Reply #3 on: March 24, 2015, 01:01:22 pm »
It has got something to do with the {$R *.lfm} culpret in unit3.pas.

How do I add an old form to a project?
« Last Edit: March 24, 2015, 01:37:26 pm by flywire »

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4459
  • I like bugs.
Re: Working in the IDE Environment
« Reply #4 on: March 24, 2015, 01:46:41 pm »
It 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.
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

flywire

  • Jr. Member
  • **
  • Posts: 85
Re: Working in the IDE Environment
« Reply #5 on: April 14, 2015, 10:20:31 am »
Add 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

  • Hero Member
  • *****
  • Posts: 4144
Re: Working in the IDE Environment
« Reply #6 on: April 14, 2015, 10:43:30 am »
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.

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

  • Jr. Member
  • **
  • Posts: 85
Re: Working in the IDE Environment Reusing Forms
« Reply #7 on: April 14, 2015, 01:50:20 pm »
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 http://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.

... 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

  • Hero Member
  • *****
  • Posts: 4144
Re: Working in the IDE Environment Reusing Forms
« Reply #8 on: April 14, 2015, 04:28:07 pm »
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

  • Hero Member
  • *****
  • Posts: 532
Re: Working in the IDE Environment Reusing Forms
« Reply #9 on: April 14, 2015, 09:05:20 pm »
How 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
windows 10 &11, Ubuntu 21+ - fpc 3.0.4, IDE 2.0 general releases

flywire

  • Jr. Member
  • **
  • Posts: 85
Re: Working in the IDE Environment Reusing Forms
« Reply #10 on: April 14, 2015, 11:45:04 pm »
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).

How 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?
Quote
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.


howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Working in the IDE Environment Reusing Forms
« Reply #11 on: April 15, 2015, 12:10:31 am »
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 IDE
  • Close 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

  • Hero Member
  • *****
  • Posts: 532
Re: Working in the IDE Environment Reusing Forms
« Reply #12 on: April 15, 2015, 01:22:22 am »
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  O:-)
what I do to "clear" is simply go into the output folder /<project>/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 <project:view forms> goes, The forms all show up in the list after I <project:add unit to project> 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.
windows 10 &11, Ubuntu 21+ - fpc 3.0.4, IDE 2.0 general releases

flywire

  • Jr. Member
  • **
  • Posts: 85
Re: Working in the IDE Environment Reusing Forms
« Reply #13 on: April 15, 2015, 01:52:54 pm »
The Lazarus wiki Documentation (http://wiki.freepascal.org/Lazarus_Documentation#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

  • Hero Member
  • *****
  • Posts: 4144
Re: Working in the IDE Environment Reusing Forms
« Reply #14 on: April 15, 2015, 05:05:28 pm »
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.

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.

 

TinyPortal © 2005-2018