Forum > General

ShowModal OnCreate error

(1/2) > >>

engine32:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.Button1Click(Sender: TObject);var  a: Tfrm2;begin  a:= Tfrm2.Create(Application);  try    a.ShowModal;  finally    a.Free;  end;end;
Second form OnCreate:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TFrm2.FormCreate(Sender: TObject);begin  frm2.mTest.Lines.Add('a'); // this line triggers the errorend; 
The same happen if I have a timer and want to set time in the OnCreate event.

Thank you.


dbannon:
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

engine32:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit unit2; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, testpas; type   { TFrm2 }   TFrm2 = class(TForm)    mTest: TMemo;    procedure FormCreate(Sender: TObject);  private   public   end; var  Frm2: TFrm2; implementation {$R *.lfm} { TFrm2 } procedure TFrm2.FormCreate(Sender: TObject);begin  self.mTest.Lines.Add('a');end; end. 
Is this right ?

dbannon:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---a.mTest.Lines.Add('a');

Davo

dseligo:
And you don't need
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var  Frm2: TFrm2;

Navigation

[0] Message Index

[#] Next page

Go to full version