Recent

Author Topic: Make Another Instance of Main TForm?  (Read 502 times)

Dan3468298

  • Full Member
  • ***
  • Posts: 125
Make Another Instance of Main TForm?
« on: July 10, 2020, 04:59:07 pm »
I have a one form test app.  Since Lazarus creates the form as class then creates an instance of that form when application starts, is there a method to create another instance of the form at runtime?   The result would be two instances of the Form side by side.   
MacOS 10.15.5/Lazarus 2.0.10 Build 2020-07-07

RAW

  • Hero Member
  • *****
  • Posts: 867
Re: Make Another Instance of Main TForm?
« Reply #1 on: July 10, 2020, 05:08:09 pm »
Sure ...  :)
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.  myForm: TForm1;
  4. begin
  5.   myForm:= TForm1.Create(Self);
  6.   myForm.Show;
  7. end;

I guess I don't understand the question ... :)
Windows 7 Pro (x64 Sp1) And Windows XP Pro (x86 Sp3) - LAZARUS 2.0.8 FPC 3.0.4 - TRUNK 2.1.0 FPC 3.3.1
// This is polarity (hell) and hell is reigned by lies, nothing but lies. Be careful what you believe ...
// There are 10 types of people in this world, those who understand mind control and ...

Dan3468298

  • Full Member
  • ***
  • Posts: 125
Re: Make Another Instance of Main TForm?
« Reply #2 on: July 10, 2020, 05:19:07 pm »
When the app starts up, I would like to have a app menuitem to create a duplicate form.  That worked, thanks.
« Last Edit: July 10, 2020, 05:23:01 pm by Dan3468298 »
MacOS 10.15.5/Lazarus 2.0.10 Build 2020-07-07

RAW

  • Hero Member
  • *****
  • Posts: 867
Re: Make Another Instance of Main TForm?
« Reply #3 on: July 10, 2020, 05:33:13 pm »
 :)
Windows 7 Pro (x64 Sp1) And Windows XP Pro (x86 Sp3) - LAZARUS 2.0.8 FPC 3.0.4 - TRUNK 2.1.0 FPC 3.3.1
// This is polarity (hell) and hell is reigned by lies, nothing but lies. Be careful what you believe ...
// There are 10 types of people in this world, those who understand mind control and ...

Dan3468298

  • Full Member
  • ***
  • Posts: 125
Re: Make Another Instance of Main TForm?
« Reply #4 on: July 11, 2020, 01:47:40 pm »
Is there a way to instantiate the new form in another thread that is not dependent on the main form, meaning the first form (main form) is closed and the new form remains open. 
MacOS 10.15.5/Lazarus 2.0.10 Build 2020-07-07

RAW

  • Hero Member
  • *****
  • Posts: 867
Re: Make Another Instance of Main TForm?
« Reply #5 on: July 11, 2020, 09:34:55 pm »
Quote
meaning the first form (main form) is closed and the new form remains open.
You can always use TProcess and start as many instances as you want ... at least on Windows, I heard on a Mac things are different ...
The question is what is the benefit?  :)
Windows 7 Pro (x64 Sp1) And Windows XP Pro (x86 Sp3) - LAZARUS 2.0.8 FPC 3.0.4 - TRUNK 2.1.0 FPC 3.3.1
// This is polarity (hell) and hell is reigned by lies, nothing but lies. Be careful what you believe ...
// There are 10 types of people in this world, those who understand mind control and ...

lucamar

  • Hero Member
  • *****
  • Posts: 3017
Re: Make Another Instance of Main TForm?
« Reply #6 on: July 11, 2020, 10:47:14 pm »
Is there a way to instantiate the new form in another thread that is not dependent on the main form, meaning the first form (main form) is closed and the new form remains open.

It can be done but not in "another" thread and the order of operations is very important: first you have to create the new "main" form, then assign it to Application.MainForm and finally free/destroy the previous main form. But, again, you should do this in the application's main thread!

The reason for all this rigmarole is that when you close/free the main form (i.e. the form assigned to Application.MainForm) the application closes itself, so you must tell the application that there is a "new" main form before closing the old one.

I don't really see why you would want to do that: it's way easier to just add a "ResetForm" method that would leave everything in the initial state.;)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.8/FPC 3.0.4 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Dan3468298

  • Full Member
  • ***
  • Posts: 125
Re: Make Another Instance of Main TForm?
« Reply #7 on: July 12, 2020, 03:12:38 pm »
Quote
meaning the first form (main form) is closed and the new form remains open.
You can always use TProcess and start as many instances as you want ... at least on Windows, I heard on a Mac things are different ...
The question is what is the benefit?  :)

Playing around now.   Can imagine an app with multiple independent window instances where you don't want closing the main window to close the others.   A monitoring app, for example.   
MacOS 10.15.5/Lazarus 2.0.10 Build 2020-07-07

jamie

  • Hero Member
  • *****
  • Posts: 3512
Re: Make Another Instance of Main TForm?
« Reply #8 on: July 12, 2020, 03:15:51 pm »
Doing such things involves helping the Forms a bit...

Only the main form receives those signals however, using those events in the main form one can query all the child forms by sending it the same signal and then checking the results on the return.

 Thus if a Child does not want to do as the Main would like the user can then make a choice about it.
The only true wisdom is knowing you know nothing

Warfley

  • Sr. Member
  • ****
  • Posts: 302
Re: Make Another Instance of Main TForm?
« Reply #9 on: July 12, 2020, 03:18:43 pm »
I solved the problem a while a ago like this:

My main form was some kind of splash screen, which had a few basic options to start the "tools", which where the windows in which you could actually do stuff. When such a tool was selected the splash screen got hidden. You could create as much "tool" instances as you wanted from the menu of each of the tools (so basically there was a New many point which had all the options available in the splash screen), and if one tool closed, it notified the splash screen, and if no other tool was visible, the splash screen reappeared.

 

TinyPortal © 2005-2018