Recent

Author Topic: Accessing parent form from derived class  (Read 2219 times)

VisualLab

  • Hero Member
  • *****
  • Posts: 639
Re: Accessing parent form from derived class
« Reply #15 on: February 11, 2025, 08:52:56 pm »
Hello, I'm trying to learn and use object oriented programming. I'm working on a project which will be able to update another project. It will read and write download links from several servers using text files.

Describe what you want to do, how this program is supposed to work. State the assumptions you have made and the goals you want to achieve. Write it in simple words, it can be a bulleted list. If you have to, try to create a simple program, without writing your own classes (for now). Instead, create a window program using what Lazarus offers. Once you master it, then try creating classes.

I also strongly recommend what my predecessors wrote: get a book (for Lazarus or Delphi).

And for God's sake - don't use AI! The source code that AI produces resembles beautiful graphics also produced by AI, on which people have 6-7 fingers in their hand, are missing a hand or have 3 legs and 2 heads. In the case of programming, it is a toy. And it will remain so for a long time. The current AI is sufficient to “sieve” and “glue” text. But this is mindless text sifting. Maybe in 50 years we will be able to entrust AI with creating simple programs. Certainly not anytime soon.

nikel

  • Full Member
  • ***
  • Posts: 240
Re: Accessing parent form from derived class
« Reply #16 on: February 11, 2025, 09:45:03 pm »
Thanks for the replies. Remy Lebeau, I like you.

nikel

  • Full Member
  • ***
  • Posts: 240
Re: Accessing parent form from derived class
« Reply #17 on: February 12, 2025, 03:26:35 am »
I fixed the error. But now I have another problem. When I click the button, it's adding a new gui to the form. I removed definition from FormCreate and put it in Button's MouseDown event.

Quote
Now, that being said, I just noticed another problem - TDownloadChannel derives from TForm1, and TForm1 has an OnCreate event handler that creates a new instance of TDownloadChannel.  This means you have an endless recursion loop!  Creating a new TForm1 will create a new TDownloadChannel, which will initialize its TForm1 ancestor, which will create a new TDownloadChannel, ... and so on, forever.

Your UI design is flawed.  Either TForm1 should not have an OnCreate event handler that creates TDownloadChannel, or TDownloadChannel should not derive from TForm1.  I lean towards the latter - a download task should not itself BE a UI, but it can update a RELATED UI if needed.  So, you should decouple your download logic from your UI logic.  In this case, make TVersion/TDownloadChannel be a standalone class with no UI ancestor, and then give it a pointer to a separate TForm1 object whose UI it should access.

I'm not sure if you're mentioning this but I'm not getting endless loop. And want to keep Form1 as anchestor.

Attached a screenshot and my current source.

As suggested at this post, I'm following a tutorial to improve my programming. I'm excited because started learning object oriented programming and event driven programming.
« Last Edit: February 12, 2025, 05:11:10 am by nikel »

nikel

  • Full Member
  • ***
  • Posts: 240
Re: Accessing parent form from derived class
« Reply #18 on: February 13, 2025, 03:29:44 am »
When I press the button it's adding a new gui. I attached a screenshot.

TRon

  • Hero Member
  • *****
  • Posts: 4157
Re: Accessing parent form from derived class
« Reply #19 on: February 13, 2025, 03:49:13 am »
When I press the button it's adding a new gui. I attached a screenshot.
Which is exactly as the code instructs to do so. You left out whether or not this is intended or not  :)

I guess it is not intended because there also isn't an actual owner, as well as storing the instance of the form into a (single) variable which means you got some memory leaks there (you can use heaptrace to verify that).

The usual path is to create a Form, and show it at the appropriate time. In order to make sure only one instance is showing there is a method named showmodal that waits for the form to close and then continues the normal code-flow. It is possible to opt for creating (and destroying) the Form on a ad hoc basis though in case that is somthing you are after.

cdbc already pointed your attention to some of those issues.

To provide you with some context, see lazarus form tutorial from the wiki in particular the paragraph on use of a second form

BTW: Drop the AI, really !
« Last Edit: February 13, 2025, 04:09:12 am by TRon »
Today is tomorrow's yesterday.

nikel

  • Full Member
  • ***
  • Posts: 240
Re: Accessing parent form from derived class
« Reply #20 on: February 13, 2025, 05:11:28 am »
Thanks for the reply. I'm working on it.

 

TinyPortal © 2005-2018