Programming => General => Topic started by: engine32 on October 26, 2021, 03:35:22 am
Title: ShowModal OnCreate error
Post by: engine32 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.
frm2.mTest.Lines.Add('a');// this line triggers the error
end;
The same happen if I have a timer and want to set time in the OnCreate event.
Thank you.
Title: Re: ShowModal OnCreate error
Post by: dbannon 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
Title: Re: ShowModal OnCreate error
Post by: engine32 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:
Title: Re: ShowModal OnCreate error
Post by: dbannon 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 -
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.
Title: Re: ShowModal OnCreate error
Post by: dbannon 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.
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
Title: Re: ShowModal OnCreate error
Post by: Handoko on October 27, 2021, 05:09:15 am
Sorry if I couldn't understand the problem. Did OP mean doing this: