Recent

Author Topic: [SOLVED]Form2.Show Exception Error  (Read 8562 times)

IndianaJones

  • Hero Member
  • *****
  • Posts: 509
[SOLVED]Form2.Show Exception Error
« on: January 23, 2011, 07:41:36 pm »
Hi all,

I have encountered the following error message when I call the Form2 from the Form1 in the oncreate event.
Is it a bug or restriction for carbon?
Thanks.

FPC 2.4.2-0
Lazarus SVN 29176
Leopard 10.5.8
« Last Edit: January 24, 2011, 12:03:43 am by IndianaJones »

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Form2.Show Exception Error
« Reply #1 on: January 23, 2011, 08:10:11 pm »
I think you call Form2 too early. At that time (Form1 OnCreate) is not Form2 created yet.
Try it call later.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

jw

  • Full Member
  • ***
  • Posts: 126
Re: Form2.Show Exception Error
« Reply #2 on: January 23, 2011, 09:32:21 pm »
this doesn't just happen on mac i've had to create what i call a start timer that gets set enabled:=true in the form.activate event and wait for an interval of 1000 or so and have the timer make the calls to open the next form.

or make the call to from2 from form1.onactivate
« Last Edit: January 23, 2011, 09:34:17 pm by jw »

xenblaise

  • Sr. Member
  • ****
  • Posts: 358
Re: Form2.Show Exception Error
« Reply #3 on: January 23, 2011, 09:36:17 pm »
Quote
unit Unit1;
{$mode objfpc}{$H+} 

interface

uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation
uses unit2;

procedure TForm1.Button1Click(Sender: TObject);
begin
Form2.Show;
Form2.Label1.Caption := 'this is label1 in form_2 called by form_1';
end;

initialization
  {$I unit1.lrs}

end.

Quote
unit Unit2;

{$mode objfpc}{$H+} 

interface

uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
  StdCtrls;

type
  TForm2 = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }

  end;

var
  Form2: TForm2;

implementation
uses unit1;

procedure TForm2.Button1Click(Sender: TObject);
begin
Form1.Show;
Form1.Label1.Caption := 'this is label1 in form_1 called by form_2';
end;

initialization
  {$I unit2.lrs}

end.

IndianaJones

  • Hero Member
  • *****
  • Posts: 509
Re: Form2.Show Exception Error
« Reply #4 on: January 23, 2011, 10:22:17 pm »

@xenablaise
What would be the case if it is not triggered from the Button click event?

@Blaazen
  Form2:=TForm2.Create(nil);
  Form2.Show;

works in the oncreate event, but Form2 lost its focus, Form1 activated after this procedure executed.

Thanks

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Form2.Show Exception Error
« Reply #5 on: January 23, 2011, 10:44:46 pm »
OK, I didn't know you create Form2 this way. So you have at least two possibilities...

1) This will show Form2 and after you close it, Form1 will be shown.
Code: [Select]
procedure TForm1.FormCreate(Sender: TObject);
begin
 Form2:=TForm.Create(nil);
 Form2.ShowModal;
end;   


2) Show Form2 later
Code: [Select]
procedure TForm1.FormActivate(Sender: TObject);
begin
  Form2.Show;
  Form1.OnActivate:=nil;  //this ensures that OnActivate is raised only once
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
 Form2:=TForm.Create(nil);
end; 
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

IndianaJones

  • Hero Member
  • *****
  • Posts: 509
Re: Form2.Show Exception Error
« Reply #6 on: January 23, 2011, 11:53:01 pm »
Thanks Blaazen for the answer, the case was solved.
« Last Edit: January 24, 2011, 12:02:57 am by IndianaJones »

xenblaise

  • Sr. Member
  • ****
  • Posts: 358
Re: [SOLVED]Form2.Show Exception Error
« Reply #7 on: January 24, 2011, 09:57:57 am »
Quote
What would be the case if it is not triggered from the Button click event?

procedure TForm1.FormCreate(Sender: TObject);
begin
Form2.Show;
//or
//Form2.ShowModal;
Form2.Caption := 'this is form_2 called by form_1';
end;

You don't need to create the form2 to show it, because it is made already in your main project .lpr
 :D
« Last Edit: January 24, 2011, 10:02:55 am by xenablaise »

 

TinyPortal © 2005-2018