Recent

Author Topic: Type casting an unkown object inside a known parent.  (Read 1810 times)

Raul_ES

  • Full Member
  • ***
  • Posts: 183
  • My interests: Healthcare & Computers
    • My Linkedin Profile, you can add me to stay in contact.
Type casting an unkown object inside a known parent.
« on: June 08, 2020, 10:19:34 pm »
Hello,

I have a notebook with pages and this pages contain one of five possible different frames. This pages are generated at run-time.

TFrameA
TFrameB
TFrameC....


There are 5 variables to count the number of frames of a type:

ACounter: Integer;
BCounter: Integer....


This variables increase and decrease when pages are added or deleted.
Before adding a new page, it's counter will be checked to see if has arrived to the limit. If it is under the limit, the page is added containing FrameX the XCounter is updated.

(i.e: I only want to allow 2 pages with frame B opened at the same time.)


When deleting a page, you remove the page and then you have to decrease it's XCounter.
Heres the problem. I am not sure how to attach the event of deleting the page and decreasing the XCounter.

I was thinking in the following solution:


When clicking the delete page button, the pseudocode to execute would be something like this:

Code: Pascal  [Select][+][-]
  1. onclickDeleteButton()
  2.  
  3.    if Page contains Frame =isInstanceOf(TframeA) then decreaseCounterA();
  4.  
  5.    if Page contains Frame=isInstanceOf(TframeB) then decreaseCounterB();
  6.  
  7.   (...)
  8.  
  9.  

The Question:
How I do this in Pascal? I need to access the (unknown) frame inside a known page (Notebook.Pages.Item) and type cast it to check if it belongs to TFrameA, TFrameB....





Pharmacy + Chemistry + Biology + Healthcare + Computing

If you have any interest or project related with these areas feel free to contact me!

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Type casting an unkown object inside a known parent.
« Reply #1 on: June 08, 2020, 10:29:09 pm »
I would simply use TPage.Tag property. When you create a new page with frame, set Tag property to 1..5. Then on deleting you know what kind of frame it is.
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/

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12010
  • Debugger - SynEdit - and more
    • wiki
Re: Type casting an unkown object inside a known parent.
« Reply #2 on: June 08, 2020, 11:12:47 pm »
Loop through either...
Code: Pascal  [Select][+][-]
  1.   TabSheet1.ComponentCount;
  2.   TabSheet1.Components[];
  3.   TabSheet1.ControlCount;
  4.   TabSheet1.Controls[];
  5.  

TFrame is a Component, so Components will be fine.

Controls includes components and more.

If you know there is only one and exactly one component on the tabsheet (i.e. only your frame)
Code: Pascal  [Select][+][-]
  1. if   TabSheet1.Components[0] is TSomeFrame then ....;

Raul_ES

  • Full Member
  • ***
  • Posts: 183
  • My interests: Healthcare & Computers
    • My Linkedin Profile, you can add me to stay in contact.
Re: Type casting an unkown object inside a known parent.
« Reply #3 on: June 09, 2020, 05:09:33 pm »
Loop through either...
Code: Pascal  [Select][+][-]
  1.   TabSheet1.ComponentCount;
  2.   TabSheet1.Components[];
  3.   TabSheet1.ControlCount;
  4.   TabSheet1.Controls[];
  5.  

TFrame is a Component, so Components will be fine.

Controls includes components and more.

If you know there is only one and exactly one component on the tabsheet (i.e. only your frame)
Code: Pascal  [Select][+][-]
  1. if   TabSheet1.Components[0] is TSomeFrame then ....;

Thank you very much Martin :-)

I still have a bit of confusion to distinguish between Component and Control.
Pharmacy + Chemistry + Biology + Healthcare + Computing

If you have any interest or project related with these areas feel free to contact me!

Raul_ES

  • Full Member
  • ***
  • Posts: 183
  • My interests: Healthcare & Computers
    • My Linkedin Profile, you can add me to stay in contact.
Re: Type casting an unkown object inside a known parent.
« Reply #4 on: June 09, 2020, 05:11:49 pm »
I would simply use TPage.Tag property. When you create a new page with frame, set Tag property to 1..5. Then on deleting you know what kind of frame it is.

Thanks Blaazen, I dind't know about the existence of this Tag property. It looks handy.



P.S: Blaazen, are you the author of Eye-Candy controls?
Pharmacy + Chemistry + Biology + Healthcare + Computing

If you have any interest or project related with these areas feel free to contact me!

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12010
  • Debugger - SynEdit - and more
    • wiki
Re: Type casting an unkown object inside a known parent.
« Reply #5 on: June 09, 2020, 05:27:09 pm »
Controls includes components and more.
I still have a bit of confusion to distinguish between Component and Control.

First of all, I swapped the order: "Controls includes components and more."
Correct is Components include Controls

Components is all that can go onto a form.

e.g T....Dialog  (save/open) inherits from TComponent

TControl inherits from TComponent
TControl  are visual stuff (buttons, labels...)

Just google Lazarus or Delphi class hierarchy.






Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Type casting an unkown object inside a known parent.
« Reply #6 on: June 09, 2020, 06:49:03 pm »
@ P.S: Blaazen, are you the author of Eye-Candy controls?

Yes.
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/

Raul_ES

  • Full Member
  • ***
  • Posts: 183
  • My interests: Healthcare & Computers
    • My Linkedin Profile, you can add me to stay in contact.
Re: Type casting an unkown object inside a known parent.
« Reply #7 on: June 11, 2020, 11:36:07 pm »
Thanks again Martin and Blaazen,


regards

Pharmacy + Chemistry + Biology + Healthcare + Computing

If you have any interest or project related with these areas feel free to contact me!

 

TinyPortal © 2005-2018