* * *

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

luca

  • Jr. Member
  • **
  • Posts: 51
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: 7087
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.
inline variables like in D10.3 are a bit like Brexit: if you are given the wrong information it sounds like a good idea. Every kid loves candy, but it makes you fat and your teeth will disappear.

HeavyUser

  • Full Member
  • ***
  • Posts: 169
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: 908
Re: Form's subclass
« Reply #3 on: November 04, 2018, 05:00:53 pm »
Maybe he should be looking into TFRAMES or something ?

lucamar

  • Sr. Member
  • ****
  • Posts: 316
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.
Been there, done that ... barely kept the timelines.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 1.8.4/fpc 3.0.4 - Ubuntu 10, Kubuntu 14, Windows XP SP3 (Home & Prof.) and various DOS incarnations.

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus