Recent

Author Topic: Form's subclass  (Read 2808 times)

luca

  • Jr. Member
  • **
  • Posts: 83
Form's subclass
« on: October 24, 2018, 10:16:26 am »
Hi.
I've a Form (TFrmMain) with a button (Btn1) on it.
I created an OnClick event procedure for Btn1 (FrmMainBtn1_OnClick).

Then I created two forms TFrmForm1 and TFrmForm2 both subclasses of TFrmMain.

I inserted a button in TFrmForm1, named it Btn1 (as on TFrmMain) and create a OnClick event procedure (Form1Btn1_OnClick).

When I compiled and error occurred: Duplicate Name: A component named "Btn1" already exists.

I've to remove Btn1 to compile the program.
If I run the program the Btn1 appears on TfrmForm1. When I clicked on this button the Form1Btn1_OnClick procedure is executed (instead of FrmMainBtn1_OnClick)
Is it a normal behaviour?

In any case I found it really usefull to create a hierarchy of Forms and to manage the logic to show button at top level having different procedure linked to same buttons.

Regards
Luca

Thaddy

  • Hero Member
  • *****
  • Posts: 14367
  • Sensorship about opinions does not belong here.
Re: Form's subclass
« Reply #1 on: October 24, 2018, 01:59:17 pm »
Such things can be remedied by using only one form per unit.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

HeavyUser

  • Sr. Member
  • ****
  • Posts: 397
Re: Form's subclass
« Reply #2 on: November 04, 2018, 11:57:09 am »
Hi.
I've a Form (TFrmMain) with a button (Btn1) on it.
I created an OnClick event procedure for Btn1 (FrmMainBtn1_OnClick).

Then I created two forms TFrmForm1 and TFrmForm2 both subclasses of TFrmMain.
How? how did you subclassed form1 and form2 from the frmMain?
I inserted a button in TFrmForm1, named it Btn1 (as on TFrmMain) and create a OnClick event procedure (Form1Btn1_OnClick).
if you have properly subclasses frmMain you should be able to see the button you placed on frmMain on form1 and form2 you will not need to add it again.
When I compiled and error occurred: Duplicate Name: A component named "Btn1" already exists.
ofcourse it exists you already said you have a button with the same name on the parent form.
I've to remove Btn1 to compile the program.
you could also rename it to a non existing name.
If I run the program the Btn1 appears on TfrmForm1. When I clicked on this button the Form1Btn1_OnClick procedure is executed (instead of FrmMainBtn1_OnClick)
Is it a normal behaviour?
Yes it is normal to be able to override the click event on a child form.

In any case I found it really usefull to create a hierarchy of Forms and to manage the logic to show button at top level having different procedure linked to same buttons.
Do it properly then don't short circuit the IDE. Select the menu File\New on the tree on the left select inherited project component and on the list on the right select your form to inherit from and press ok.

jamie

  • Hero Member
  • *****
  • Posts: 6130
Re: Form's subclass
« Reply #3 on: November 04, 2018, 05:00:53 pm »
Maybe he should be looking into TFRAMES or something ?
The only true wisdom is knowing you know nothing

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Form's subclass
« Reply #4 on: November 04, 2018, 05:15:59 pm »
Maybe he should be looking into TFRAMES or something ?

IMHO, frames are a different concept. To better understand what the OP is, I think, looking for think about the relationship between TFindDialog and TReplaceDialog.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018