Recent

Author Topic: ShowModal OnCreate error  (Read 3489 times)

engine32

  • New Member
  • *
  • Posts: 10
ShowModal OnCreate error
« on: October 26, 2021, 03:35:22 am »
Hi there,

I have a simple project to test the error. One main form with a button on it to create and show modal a second form. The second form is not created by application, it is created at run time. On the second form there is a memo. And it works.

However, it I add a text line to the memo control in the OnCreate handler of the second form, I get an error, and I would like to fix this.

First form button click:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   a: Tfrm2;
  4. begin
  5.   a:= Tfrm2.Create(Application);
  6.   try
  7.     a.ShowModal;
  8.   finally
  9.     a.Free;
  10.   end;
  11. end;

Second form OnCreate:
Code: Pascal  [Select][+][-]
  1. procedure TFrm2.FormCreate(Sender: TObject);
  2. begin
  3.   frm2.mTest.Lines.Add('a'); // this line triggers the error
  4. end;
  5.  

The same happen if I have a timer and want to set time in the OnCreate event.

Thank you.



dbannon

  • Hero Member
  • *****
  • Posts: 1676
    • tomboy-ng, a rewrite of the classic Tomboy
Re: ShowModal OnCreate error
« Reply #1 on: October 26, 2021, 04:18:52 am »
It looks to me as is you have declared 'a' as a Tfrm2 and created it in Form1. Thats cool.

But in the newly created form, you explicitly call frm2.mTest.Lines.Add(..). Now, is frm2 the same as 'a' ?  It sounds to me as if it could be the one declared for you in Unit2 ?

Remove the 'frm2', its unnecessary, without it you will be referring to self.mTest and that has definitely been created.

Davo
Lazarus 2, Linux (and reluctantly Win10, OSX)
My Project - https://github.com/tomboy-notes/tomboy-ng

engine32

  • New Member
  • *
  • Posts: 10
Re: ShowModal OnCreate error
« Reply #2 on: October 26, 2021, 05:25:23 am »
Thank you.

I replaced "frm2" with "self" and the error is gone. However, I am not sure this is all I have to do. Now unit2 looks like this:
Code: Pascal  [Select][+][-]
  1. unit unit2;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, testpas;
  9.  
  10. type
  11.  
  12.   { TFrm2 }
  13.  
  14.   TFrm2 = class(TForm)
  15.     mTest: TMemo;
  16.     procedure FormCreate(Sender: TObject);
  17.   private
  18.  
  19.   public
  20.  
  21.   end;
  22.  
  23. var
  24.   Frm2: TFrm2;
  25.  
  26. implementation
  27.  
  28. {$R *.lfm}
  29.  
  30. { TFrm2 }
  31.  
  32. procedure TFrm2.FormCreate(Sender: TObject);
  33. begin
  34.   self.mTest.Lines.Add('a');
  35. end;
  36.  
  37. end.
  38.  

Is this right ?

dbannon

  • Hero Member
  • *****
  • Posts: 1676
    • tomboy-ng, a rewrite of the classic Tomboy
Re: ShowModal OnCreate error
« Reply #3 on: October 26, 2021, 06:46:13 am »
It will do no harm to put 'self' there but its not the conventional thing to do. Just remove it, the memo is in the Form's class, the compiler will assume you mean that one anyway. so, all you need is -
Code: Pascal  [Select][+][-]
  1. mTest.Lines.Add('a');

If you were back in Unit1, you would need to be more explicate, because you declared and created a form, 'a' of type TFrm2, you would say

Code: Pascal  [Select][+][-]
  1. a.mTest.Lines.Add('a');


Davo

Lazarus 2, Linux (and reluctantly Win10, OSX)
My Project - https://github.com/tomboy-notes/tomboy-ng

dseligo

  • Sr. Member
  • ****
  • Posts: 384
Re: ShowModal OnCreate error
« Reply #4 on: October 26, 2021, 06:34:58 pm »
And you don't need
Code: Pascal  [Select][+][-]
  1. var
  2.   Frm2: TFrm2;

engine32

  • New Member
  • *
  • Posts: 10
Re: ShowModal OnCreate error
« Reply #5 on: October 27, 2021, 02:02:56 am »
Thank you.
But my trouble is not over yet. Now I want to add a new line to the memo in a separate procedure like in the following:
Code: Pascal  [Select][+][-]
  1. unit unit2;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, testpas;
  9.  
  10. type
  11.  
  12.   { TFrm2 }
  13.  
  14.   TFrm2 = class(TForm)
  15.     mTest: TMemo;
  16.     procedure FormCreate(Sender: TObject);
  17.   private
  18.  
  19.   public
  20.  
  21.   end;
  22.  
  23.  
  24. implementation
  25.  
  26. {$R *.lfm}
  27.  
  28. { TFrm2 }
  29. procedure pa;
  30. begin
  31.   mTest.Lines.Add('a');
  32. end;
  33.  
  34. procedure TFrm2.FormCreate(Sender: TObject);
  35. begin
  36.   pa;
  37. end;
  38.  
  39. end.
  40.  

I get an error saying that it does not know mTest. Adding "self" in front of it does not help either. Any help is much appreciated.

dbannon

  • Hero Member
  • *****
  • Posts: 1676
    • tomboy-ng, a rewrite of the classic Tomboy
Re: ShowModal OnCreate error
« Reply #6 on: October 27, 2021, 04:23:13 am »
The issue there is that your procedure, pa (you really need to work on your identifier names) is not a class member of the TFrm2 class.  So, it does not know about the TMemo.

Make that procedure a class member (untested) -

Code: Pascal  [Select][+][-]
  1. unit unit2;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, testpas;
  9.  
  10. type
  11.  
  12.   { TFrm2 }
  13.  
  14.   TFrm2 = class(TForm)
  15.     mTest: TMemo;
  16.     procedure FormCreate(Sender: TObject);
  17.   private
  18.     procedure pa;
  19.   public
  20.  
  21.   end;
  22.  
  23.  
  24. implementation
  25.  
  26. {$R *.lfm}
  27.  
  28. { TFrm2 }
  29. procedure TFrm2.pa;
  30. begin
  31.   mTest.Lines.Add('a');
  32. end;
  33.  
  34. procedure TFrm2.FormCreate(Sender: TObject);
  35. begin
  36.   pa;
  37. end;

I have made two changes, firstly, declared pa up in the private section of TForm2's declaration. Second, I have added "TFrm2." in front of "pa" in the implementation. That way, Memo1 and pa, both being in the same class can see each other.

You probably need to have a bit of a read about the class/object syntax.

One suggestion, when posting demos of problems its a great idea to strip it down as you have done, good. But by using the names that the IDE gives these things by default, Unit2, Memo1, makes it easier for other people to see whats happening. (But, obviously, in your real app, give them sensible, descriptive, names).

Davo

Lazarus 2, Linux (and reluctantly Win10, OSX)
My Project - https://github.com/tomboy-notes/tomboy-ng

Handoko

  • Hero Member
  • *****
  • Posts: 4382
  • My goal: build my own game engine using Lazarus
Re: ShowModal OnCreate error
« Reply #7 on: October 27, 2021, 05:09:15 am »
Sorry if I couldn't understand the problem. Did OP mean doing this:

Code: Pascal  [Select][+][-]
  1. unit Unit2;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, Forms, Controls, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm2 }
  13.  
  14.   TForm2 = class(TForm)
  15.     Memo1: TMemo;
  16.     procedure FormCreate(Sender: TObject);
  17.   end;
  18.  
  19. var
  20.   Form2: TForm2;
  21.  
  22. implementation
  23.  
  24. {$R *.lfm}
  25.  
  26. { TForm2 }
  27.  
  28. procedure TForm2.FormCreate(Sender: TObject);
  29. begin
  30.   Memo1.Lines.Add('Testing');
  31. end;
  32.  
  33. end.

 

TinyPortal © 2005-2018