Recent

Author Topic: [SOLVED]Cannot create form?  (Read 1478 times)

JamesSci

  • New Member
  • *
  • Posts: 30
[SOLVED]Cannot create form?
« on: February 19, 2020, 05:34:51 pm »
Hi!

My program was working fine, then I decided to remove a form out of "Auto-create forms" and instead initialize it using:

Code: Pascal  [Select][+][-]
  1. unit2
  2.  
  3. implementation
  4.  
  5. uses unit1
  6.  
  7. [Other code to execute] // This code all executes fine, up until the below code...
  8.  
  9. Form2 := TForm2.Create(nil);
  10.   try
  11.     Form2.ShowModal;
  12.   finally
  13.     Form2.Free;
  14.   end;

This raises a run-time exception:

Quote
Failed to create win32 control, error 1407 : Cannot find window class
In file 'win32\win32wscontrols.pp' at line 220:

What might be causing this?

Thank you.
« Last Edit: February 19, 2020, 09:19:20 pm by JamesSci »

ezlage

  • Guest
Re: Cannot create form?
« Reply #1 on: February 19, 2020, 06:24:51 pm »
Hi!

My program was working fine, then I decided to remove a form out of "Auto-create forms" and instead initialize it using:

Code: Pascal  [Select][+][-]
  1. unit2
  2.  
  3. implementation
  4.  
  5. uses unit1
  6.  
  7. [Other code to execute] // This code all executes fine, up until the below code...
  8.  
  9. Form2 := TForm2.Create(nil);
  10.   try
  11.     Form2.ShowModal;
  12.   finally
  13.     Form2.Free;
  14.   end;

This raises the exception:

Quote
Failed to create win32 control, error 1407 : Cannot find window class
In file 'win32\win32wscontrols.pp' at line 220:

What might be causing this?

Thank you.

Are you working with forms in a console app project?

JamesSci

  • New Member
  • *
  • Posts: 30
Re: Cannot create form?
« Reply #2 on: February 19, 2020, 06:28:13 pm »
The code executes from a plain unit, not a console unit or a forms unit, but I did made sure to add units Forms and Interfaces to the top.

Thaddy

  • Hero Member
  • *****
  • Posts: 14201
  • Probably until I exterminate Putin.
Re: Cannot create form?
« Reply #3 on: February 19, 2020, 06:38:51 pm »
If your application is a console application of course that will never. ever wok.
Specialize a type, not a var.

JamesSci

  • New Member
  • *
  • Posts: 30
Re: Cannot create form?
« Reply #4 on: February 19, 2020, 06:50:14 pm »
Oh. I just want my application to run everything in the plain non-forms unit without creating the other form until explicitly asked to do so - the unit runs all of it's code, but when it gets to From.Create, it fails. How do I make the program start, run everything in this unit and then after, and only after that, show the form?
« Last Edit: February 19, 2020, 07:00:28 pm by JamesSci »

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Cannot create form?
« Reply #5 on: February 19, 2020, 07:05:50 pm »
Hi!

Is your Form already created in the lpr file?

Something like
Application.CreateForm(TForm1, Form1);
??

Then you try to create the Form a second time. Crash!

Winni

JamesSci

  • New Member
  • *
  • Posts: 30
Re: Cannot create form?
« Reply #6 on: February 19, 2020, 07:08:27 pm »
Hi!

Is your Form already created in the lpr file?

Something like
Application.CreateForm(TForm1, Form1);
??

Then you try to create the Form a second time. Crash!

Winni
Hi!

No the form is not created there - I removed it from the Project auto-create forms list.

Also, I'm okay if there's a solution where the form is created when the program starts, I just need it to remain in the background (i.e. not set off the Form Activate) until the code in the unit is all executed. If there's a way to do that, then great.
« Last Edit: February 19, 2020, 07:10:55 pm by JamesSci »

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Cannot create form?
« Reply #7 on: February 19, 2020, 07:27:05 pm »
Instead of doing:
Code: Pascal  [Select][+][-]
  1. Form2 := TForm2.Create(nil);

I'd recommend you to use :
Code: Pascal  [Select][+][-]
  1. Application.CreateForm(TForm2, Form2);

I don't know why but
Code: Pascal  [Select][+][-]
  1. TSomeform.Create
fails sometimes in unexpected ways.

Note that, except for the main form, auto-created forms are not shown unless they have their Visible property set to True, so you can still do Form2.ShowModal even if it was auto-created.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

JamesSci

  • New Member
  • *
  • Posts: 30
Re: Cannot create form?
« Reply #8 on: February 19, 2020, 07:53:49 pm »
Hi lucamar,

Your suggestion didn't work unfortuantely. I made another project to see if I could replicate my issue and, luckily, it was easily replicable. If you could take a look, it would be much appreciated, see attached.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Cannot create form?
« Reply #9 on: February 19, 2020, 08:07:09 pm »
In your example project, you're trying to create the form in the initialization section of the unit! Simplifying much, no objects are created , much less initialized at that point, not even Application (IIRC)

Please provide us with something closer to what you want to achieve.

Or is that how you're doing it now?
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

JamesSci

  • New Member
  • *
  • Posts: 30
Re: Cannot create form?
« Reply #10 on: February 19, 2020, 08:16:08 pm »
Hello. Yes this is how it's being done. There's loads of code in the unit2 that comes before the form1 is asked to show. I need it so that all of that code is executed prior to the form showing. Is there any way to wait for everything to initialize and then call for the form to show?

Thanks for your help.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Cannot create form?
« Reply #11 on: February 19, 2020, 08:38:54 pm »
Easiest way? Put all your code inside a procedure/function and call it from the main program (the .lpr file). For example, supposing you put all that code in a procedure called MoreSetup in Unit2, your main program might look like:

Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   Interfaces, // this includes the LCL widgetset
  7.   Forms, Unit1, Unit2;
  8. {$R *.res}
  9.  
  10. begin
  11.   RequireDerivedFormResource:=True;
  12.   Application.Scaled:=True;
  13.   Application.Initialize;
  14.   Application.CreateForm(TForm1, Form1);
  15.   Unit2.MoreSetup();
  16.   Application.Run;
  17. end.

The form will not show up or give any other sign of life unitl the call to Application.Run, but it will exist and be (mostly) initialized before the call to MoreSetUp (and yes, I just tested it :) )

The way you're doing it now, your code is being run before almost any other code in the application, including the initialization of some RTL/FCL/LCL units. Unless absolutely needed it's better to set your code in some kind of "setup" or whatever procedure/function and call it from the main program (or any other unit): It even can be the very first line, if needed.
« Last Edit: February 19, 2020, 08:41:17 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

JamesSci

  • New Member
  • *
  • Posts: 30
Re: Cannot create form?
« Reply #12 on: February 19, 2020, 09:19:03 pm »
Thank you very much lucamar! Your advice solved my problem 100%!  :D :D

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Cannot create form?
« Reply #13 on: February 19, 2020, 09:50:58 pm »
Thank you very much lucamar! Your advice solved my problem 100%!  :D :D

Glad to be of help ;)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018