Recent

Author Topic: Calling a form within a project from another project  (Read 802 times)

pascal111

  • Sr. Member
  • ****
  • Posts: 420
  • Un trabajo en equipo para programas serias.
Calling a form within a project from another project
« on: July 04, 2022, 05:54:49 pm »
I designed a form within a project, and tried to call it from another new project but the compiler gave me an error, how we can do it?
La chose par la chose est rappelé.

Handoko

  • Hero Member
  • *****
  • Posts: 4690
  • My goal: build my own game engine using Lazarus
Re: Calling a form within a project from another project
« Reply #1 on: July 04, 2022, 06:47:11 pm »
What was the error message? Can you show us?

The easiest probably is copy/paste the *.lfm and *.pas files to new project's folder. And then use the Project Manager > Add > File from the File System.

Actually you don't have to copy/paste those files to the new project's folder, but if you move the original source code to other location or rename that folder, you'll get error. Unless you properly manage your project folders and have a shared library folder.

pascal111

  • Sr. Member
  • ****
  • Posts: 420
  • Un trabajo en equipo para programas serias.
Re: Calling a form within a project from another project
« Reply #2 on: July 04, 2022, 07:10:44 pm »
I did the next steps:

file->open->inputboxunit.lfm
file->open->inputboxunit.pas
project->view project source

and in project1.lpr added a line:

Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   {$IFDEF UNIX}
  7.   cthreads,
  8.   {$ENDIF}
  9.   {$IFDEF HASAMIGA}
  10.   athreads,
  11.   {$ENDIF}
  12.   Interfaces, // this includes the LCL widgetset
  13.   Forms, unit1
  14.   { you can add units after this };
  15.  
  16. {$R *.res}
  17.  
  18. begin
  19.   RequireDerivedFormResource:=True;
  20.   Application.Scaled:=True;
  21.   Application.Initialize;
  22.   Application.CreateForm(TForm1, Form1);
  23.   Application.CreateForm(TInputBoxForm, InputBoxForm);
  24.   Application.Run;
  25. end.
  26.  
  27.  

then I had the next errors as they are shown in next image:

https://i.postimg.cc/XY8vhnCd/errcopy.jpg
La chose par la chose est rappelé.

Handoko

  • Hero Member
  • *****
  • Posts: 4690
  • My goal: build my own game engine using Lazarus
Re: Calling a form within a project from another project
« Reply #3 on: July 04, 2022, 07:14:19 pm »
You need to add that unit into your project by using Project Inspector. See my previous post.

pascal111

  • Sr. Member
  • ****
  • Posts: 420
  • Un trabajo en equipo para programas serias.
Re: Calling a form within a project from another project
« Reply #4 on: July 04, 2022, 07:45:26 pm »
You need to add that unit into your project by using Project Inspector. See my previous post.

I was searching for "Project Manager" ... oops!

I noticed now you mean it by " Project Inspector".
La chose par la chose est rappelé.

pascal111

  • Sr. Member
  • ****
  • Posts: 420
  • Un trabajo en equipo para programas serias.
Re: Calling a form within a project from another project
« Reply #5 on: July 04, 2022, 07:52:15 pm »
Well! we have new error when I tried to use "showmodal" like next image:

https://i.postimg.cc/52MKjGSM/jksdhfksntitled-1-copy.jpg
La chose par la chose est rappelé.

Handoko

  • Hero Member
  • *****
  • Posts: 4690
  • My goal: build my own game engine using Lazarus
Re: Calling a form within a project from another project
« Reply #6 on: July 04, 2022, 09:06:43 pm »
Usually you need to make the form auto-create, unless you manually create it.

To make the form auto-create:
Lazarus main menu > Project > Project Options > left side: Project Options > Forms > click the arrow to move the form in Available forms panel to Auto-create forms

pascal111

  • Sr. Member
  • ****
  • Posts: 420
  • Un trabajo en equipo para programas serias.
Re: Calling a form within a project from another project
« Reply #7 on: July 04, 2022, 11:01:15 pm »
I found the two forms in the auto-create column, but I pressed to the test button and has these errors:

Quote
HINT: using config file D:\lazarus\fpc\3.2.2\bin\x86_64-win64\fpc.cfg
WARNING: ppu exists twice: D:\lazarus\fpc\3.2.2\units\x86_64-win64\fv\dialogs.ppu, d:\lazarus\lcl\units\x86_64-win64\dialogs.ppu
WARNING: ppu exists twice: D:\lazarus\fpc\3.2.2\units\x86_64-win64\fv\menus.ppu, d:\lazarus\lcl\units\x86_64-win64\menus.ppu

an image too:

https://i.postimg.cc/j2k65Jqy/laz1copy.jpg
La chose par la chose est rappelé.

Handoko

  • Hero Member
  • *****
  • Posts: 4690
  • My goal: build my own game engine using Lazarus
Re: Calling a form within a project from another project
« Reply #8 on: July 05, 2022, 05:52:45 am »
Maybe you can try:
Lazarus main menu > Run > Build

If that does not work, please provide the source code. Save all the related files into a zip file and attach the zip file here (or other online file sharing service).

pascal111

  • Sr. Member
  • ****
  • Posts: 420
  • Un trabajo en equipo para programas serias.
Re: Calling a form within a project from another project
« Reply #9 on: July 05, 2022, 10:21:39 am »
I compressed files I think they are the sources files.
La chose par la chose est rappelé.

rvk

  • Hero Member
  • *****
  • Posts: 4819
Re: Calling a form within a project from another project
« Reply #10 on: July 05, 2022, 10:33:02 am »
You are trying to access inputform in the Form1.FormCreate.
If you do that, you need to make sure inputform is created BEFORE Form1.

You can do that by changing the order in the auto-create form dialog.
Just press the inputform and then press the green up-arrow on the left of it.

Now inputform will be created before Form1.
(You can also do this manually in your main.lpr by moving the create-line above the form1 one)

« Last Edit: July 05, 2022, 10:35:08 am by rvk »

pascal111

  • Sr. Member
  • ****
  • Posts: 420
  • Un trabajo en equipo para programas serias.
Re: Calling a form within a project from another project
« Reply #11 on: July 05, 2022, 11:02:47 am »
You are trying to access inputform in the Form1.FormCreate.
If you do that, you need to make sure inputform is created BEFORE Form1.

You can do that by changing the order in the auto-create form dialog.
Just press the inputform and then press the green up-arrow on the left of it.

Now inputform will be created before Form1.
(You can also do this manually in your main.lpr by moving the create-line above the form1 one)

I did what you said, but another problem is triggered that inputform is shown in the running but there is no Form1 at all.
« Last Edit: July 05, 2022, 11:05:51 am by pascal111 »
La chose par la chose est rappelé.

rvk

  • Hero Member
  • *****
  • Posts: 4819
Re: Calling a form within a project from another project
« Reply #12 on: July 05, 2022, 11:14:22 am »
I did what you said, but another problem is triggered that inputform is shown in the running but there is no Form1 at all.
Yes, that's because you do a ShowModal in the FormCreate of Form1.
The ShowModal will block all other form-events so Form1 will not be shown.

If that's not what you want, you shouldn't do a ShowModal in the FormCreate of Form1.
(you can for example do the ShowModal of inputform in a ButtonClick of Form1)

pascal111

  • Sr. Member
  • ****
  • Posts: 420
  • Un trabajo en equipo para programas serias.
Re: Calling a form within a project from another project
« Reply #13 on: July 05, 2022, 11:18:32 am »
I did what you said, but another problem is triggered that inputform is shown in the running but there is no Form1 at all.
Yes, that's because you do a ShowModal in the FormCreate of Form1.
The ShowModal will block all other form-events so Form1 will not be shown.

If that's not what you want, you shouldn't do a ShowModal in the FormCreate of Form1.
(you can for example do the ShowModal of inputform in a ButtonClick of Form1)

Yes, it works like that within a button procedure, but can't we do the trick of showmessage and inputbox  in FormCreate procedure with another form like I tried to do?
La chose par la chose est rappelé.

Handoko

  • Hero Member
  • *****
  • Posts: 4690
  • My goal: build my own game engine using Lazarus
Re: Calling a form within a project from another project
« Reply #14 on: July 05, 2022, 11:31:48 am »
You did a beginner mistake:
Form's OnCreate event is for initializing variables, objects or states only. You should not do any heavy calculation nor long duration file loading, and should not perform any screen displaying, in your case showing a message box or form.

What you're trying to do is showing a temporary form before the main form. It has been asked several times, you can search the forum using keyword "splash". There are many solutions for it, or try this:

https://forum.lazarus.freepascal.org/index.php/topic,56600.msg420687.html#msg420687

 

TinyPortal © 2005-2018