Recent

Author Topic: [Solved] Calling objects from other units  (Read 4280 times)

heebiejeebies

  • Jr. Member
  • **
  • Posts: 59
[Solved] Calling objects from other units
« on: June 07, 2021, 12:06:19 pm »
Hi all,

I've added a new unit to my app that functions as a help screen.  As my main unit is one huge tabsheet with about 70 tabs, the help screen needs to read which tab is active, and then bring up its own corresponding help tab.

Just as a made-up example, supposing the user is on the 'vegetables' tab in the main screen, when the user opens the help screen from the menubar, it needs to be on the vegetables help screen.

Definitely not the most efficient way of doing it, but this is what I've come up with for the moment. 

Code: Pascal  [Select][+][-]
  1. procedure TForm1.ShowQuestionsPromptClick(Sender: TObject);
  2. begin
  3.   if HelperScreen.visible = false then
  4.   begin
  5.   if MainScreen.ActivePage = Vegetables then HelperScreen.HelperScreenPages.ActivePage := VegetablesHelp;
  6. // and so on for all the other tabs on the main screen.
  7.   HelperScreen.visible :=true;
  8.   end
  9.   else
  10.   if HelperScreen.visible = true then HelperScreen.visible :=false;
  11. end;
  12.          

The HelperScreen, unit 2, shows and hides with no problems.  I have also been able to successfully change the properties of a label object on the HelperScreen, from the main screen, unit 1 - which is proof of concept.  But when I try and change the ActivePage of the tabsheet on the HelperScreen, it complains that there is no identifier VegetablesHelp.  Which there is.  %)

Any thoughts?  Thanks
« Last Edit: June 12, 2021, 07:38:46 am by heebiejeebies »
Ubuntu 18.04/Lazarus 2.0.10 FPC 3.2.0

jamie

  • Hero Member
  • *****
  • Posts: 4742
Re: Calling objects from other units
« Reply #1 on: June 07, 2021, 01:41:07 pm »
Put a method in unit 2 that accepts your ttabsheet control as a parameter. Since  tab sheet is a common control you should be able to gain access to the control that way..

Unit 2

Procedure ShowHelp(theTabCOntrol:TTabControl,.....);

 You will need also the use the Unit in the users list for that control type, too..

etc..

 I think you get the idea..

The only true wisdom is knowing you know nothing

Paolo

  • Full Member
  • ***
  • Posts: 184
Re: Calling objects from other units
« Reply #2 on: June 07, 2021, 01:47:34 pm »
It seems that such variable is ot seen, did you have declared it in the interface or implementation section ?

heebiejeebies

  • Jr. Member
  • **
  • Posts: 59
Re: Calling objects from other units
« Reply #3 on: June 08, 2021, 10:42:54 am »
Put a method in unit 2 that accepts your ttabsheet control as a parameter. Since  tab sheet is a common control you should be able to gain access to the control that way..

Unit 2

Procedure ShowHelp(theTabCOntrol:TTabControl,.....);

 You will need also the use the Unit in the users list for that control type, too..

etc..

 I think you get the idea..




Thanks Jamie! I get the basic idea of what you're saying but I'm going to have to brush up on creating procedures over the weekend when I have time.  Unfortunately I am not a natural programmer by temperament so I usually implement everything in the simplest way possible, which is perhaps not the 'correct' way.  In other words I normally avoid creating procedures and just code everything manually in the relevant event.  :-[ The only part I didn't get at all was ' You will need also the use the Unit in the users list for that control type, too..'

I have listed Unit 2 in the 'uses' section of unit 1, but not sure what you mean by the 'users list for that control type' ... ?
Ubuntu 18.04/Lazarus 2.0.10 FPC 3.2.0

heebiejeebies

  • Jr. Member
  • **
  • Posts: 59
Re: Calling objects from other units
« Reply #4 on: June 08, 2021, 10:43:59 am »
It seems that such variable is ot seen, did you have declared it in the interface or implementation section ?

Thanks for your reply, not quite sure what you mean though ...  %)
Ubuntu 18.04/Lazarus 2.0.10 FPC 3.2.0

Paolo

  • Full Member
  • ***
  • Posts: 184
Re: Calling objects from other units
« Reply #5 on: June 08, 2021, 03:42:52 pm »
It is just a possibility: move the declared VegetableHelps from implementation section  of unit2 to the interface section of  unit2.
« Last Edit: June 08, 2021, 03:45:02 pm by Paolo »

heebiejeebies

  • Jr. Member
  • **
  • Posts: 59
Re: Calling objects from other units
« Reply #6 on: June 09, 2021, 12:33:29 pm »
Thanks Paolo!  It's not actually a variable though, it's just the name of the Tabsheet in the Pagecontrol.
Ubuntu 18.04/Lazarus 2.0.10 FPC 3.2.0

Paolo

  • Full Member
  • ***
  • Posts: 184
Re: Calling objects from other units
« Reply #7 on: June 09, 2021, 01:37:46 pm »
try this (I am supposing you have the form2=HelperScreen, HelperScreenPages = Pagecontrol on which there is a tabsheet named VegetablesHelp).

Code: Pascal  [Select][+][-]
  1.     procedure TForm1.ShowQuestionsPromptClick(Sender: TObject);
  2.     begin
  3.       if HelperScreen.visible = false then
  4.       begin
  5.       if MainScreen.ActivePage = Vegetables then HelperScreen.HelperScreenPages.ActivePage := HelperScreen.HelperScreenPages.VegetablesHelp;
  6.     // and so on for all the other tabs on the main screen.
  7.       HelperScreen.visible :=true;
  8.       end
  9.       else
  10.       if HelperScreen.visible = true then HelperScreen.visible :=false;
  11.     end;
  12.              

Paolo

  • Full Member
  • ***
  • Posts: 184
Re: Calling objects from other units
« Reply #8 on: June 09, 2021, 01:46:43 pm »
if you have page by page syncronised index-by-index you can better do (not tested)

Code: Pascal  [Select][+][-]
  1. procedure TForm1.ShowQuestionsPromptClick(Sender: TObject);
  2. begin
  3.     if not(HelperScreen.visible) then
  4.        HelperScreen.ActivePAgeIndex:=MainScreen.ActivePAgeIndex;
  5.      HelperScreen.visible:=not(HelperScreen.visible);
  6. end;
  7.  

dseligo

  • Sr. Member
  • ****
  • Posts: 315
Re: Calling objects from other units
« Reply #9 on: June 09, 2021, 04:33:36 pm »
Try to add HelperScreen in front of VegetablesHelp like that:
Code: Pascal  [Select][+][-]
  1. if MainScreen.ActivePage = Vegetables then HelperScreen.HelperScreenPages.ActivePage := HelperScreen.VegetablesHelp;

It's because you are referring to VegetablesHelp which is defined in class HelperScreen.
« Last Edit: June 09, 2021, 04:36:27 pm by dseligo »

heebiejeebies

  • Jr. Member
  • **
  • Posts: 59
Re: Calling objects from other units
« Reply #10 on: June 12, 2021, 07:38:30 am »
Thanks Paolo and dseligo!  It was that simple, eh?  Works fine. Cheers  :D
Ubuntu 18.04/Lazarus 2.0.10 FPC 3.2.0

 

TinyPortal © 2005-2018