Recent

Author Topic: [Solved!] TPageControl: OnChange event not firing  (Read 1520 times)

heebiejeebies

  • Jr. Member
  • **
  • Posts: 51
[Solved!] TPageControl: OnChange event not firing
« on: May 12, 2021, 12:00:03 pm »
Hi all,

I have a TPageControl that holds all the different screens of my program.  I need to show a logo when the user is on certain screens only, and hide it on others.  For some bizarre reason, the OnChange event does not fire when the page is changed via code - only when the user changes pages via switching tabs.

I even tried putting code in the MouseUp events for, firstly the PageControl and then the form itself.  The PageControl one worked, but again only if the user clicks on the tabs.  The form event did not seem to work at all.

Surely there must be a way for me to do this without having to add extra code to every button that changes pages?  I have a lot of those...

Btw the logo is not on the PageControl itself, it is in the bottom 20% of the screen where the PageControl does not extend.  This part of the screen contains various controls that only change minimally between pages.

Thanks!
« Last Edit: May 12, 2021, 12:23:30 pm by heebiejeebies »
Ubuntu 18.04/Lazarus 2.0.10 FPC 3.2.0

wp

  • Hero Member
  • *****
  • Posts: 8359
Re: TPageControl: OnChange event not firing
« Reply #1 on: May 12, 2021, 12:04:30 pm »
Add nboDoChangeOnSetIndex to the PageControl.Options to fire OnChange when the pageindex is set by code (https://wiki.freepascal.org/Lazarus_1.8.0_release_notes#TCustomTabControl_setting_TabIndex_or_PageIndex_by_code).
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

heebiejeebies

  • Jr. Member
  • **
  • Posts: 51
Re: TPageControl: OnChange event not firing
« Reply #2 on: May 12, 2021, 12:23:13 pm »
GENIUS!!

I googled the heck out of this before I posted and seemingly no-one but you in the entire universe knows that option exists.  Did not realise it'd be that easy.  THANK YOU!!!!  :D
Ubuntu 18.04/Lazarus 2.0.10 FPC 3.2.0

howardpc

  • Hero Member
  • *****
  • Posts: 3715
Re: [Solved!] TPageControl: OnChange event not firing
« Reply #3 on: May 12, 2021, 12:34:58 pm »
It's one of those cases where for sake of Delphi compatibility the LCL opts to to set a default which is inconvenient for most of us (and can break early LCL code), and unfortunately the way to change that default behaviour is not intuitive or obvious.

heebiejeebies

  • Jr. Member
  • **
  • Posts: 51
Re: [Solved!] TPageControl: OnChange event not firing
« Reply #4 on: May 12, 2021, 12:42:19 pm »
OK so now I've got another weird problem.  Here's the event code:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.MainScreenChange(Sender: TObject);
  2. begin
  3.   if MainScreen.ActivePage = MainHome then
  4.   begin
  5.   MainLogo.Visible:=true;
  6.   Blurb.Visible := false;
  7.   end
  8.   else
  9.   MainLogo.Visible:=false;
  10.   Blurb.Visible:=true;
  11. end;

When it shows the logo, it needs to hide a TLabel called 'Blurb' that normally occupies that same spot.  It successfully shows the logo but it doesn't hide the Blurb.  What the ???
Ubuntu 18.04/Lazarus 2.0.10 FPC 3.2.0

heebiejeebies

  • Jr. Member
  • **
  • Posts: 51
Re: [Solved!] TPageControl: OnChange event not firing
« Reply #5 on: May 12, 2021, 12:49:06 pm »
Never mind, I can get the same effect by just setting the Blurb's caption to blank.  Very weird though ??
Ubuntu 18.04/Lazarus 2.0.10 FPC 3.2.0

speter

  • Full Member
  • ***
  • Posts: 210
Re: [Solved!] TPageControl: OnChange event not firing
« Reply #6 on: May 12, 2021, 01:29:27 pm »
heebiejeebies, I think your lack of indenting is "hurting" you. ;)

Your code is:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.MainScreenChange(Sender: TObject);
  2. begin
  3.   if MainScreen.ActivePage = MainHome then
  4.     begin
  5.       MainLogo.Visible:=true;
  6.       Blurb.Visible := false;
  7.     end
  8.   else
  9.     MainLogo.Visible:=false;
  10.  
  11.   Blurb.Visible:=true;
  12. end;

So, you are always setting Blurb to Visible.

I think you meant to do:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.MainScreenChange(Sender: TObject);
  2. begin
  3.   if MainScreen.ActivePage = MainHome then
  4.     begin
  5.       MainLogo.Visible:=true;
  6.       Blurb.Visible := false;
  7.     end
  8.   else
  9.     begin
  10.       MainLogo.Visible:=false;
  11.       Blurb.Visible:=true;
  12.     end;
  13. end;

cheers
S.
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

Laz 2.0.10 / FPC 3.2.0 / Windows 10 (64bit)

lucamar

  • Hero Member
  • *****
  • Posts: 4009
Re: [Solved!] TPageControl: OnChange event not firing
« Reply #7 on: May 12, 2021, 02:08:32 pm »
And you could further abbreviate it to:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.MainScreenChange(Sender: TObject);
  2. begin
  3.   MainLogo.Visible := MainScreen.ActivePage = MainHome;
  4.   Blurb.Visible := not MainLogo.Visible;
  5. end;
which leaves no room for mistakes. ;D
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.

heebiejeebies

  • Jr. Member
  • **
  • Posts: 51
Re: [Solved!] TPageControl: OnChange event not firing
« Reply #8 on: May 13, 2021, 11:18:14 am »
heebiejeebies, I think your lack of indenting is "hurting" you. ;)

Your code is:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.MainScreenChange(Sender: TObject);
  2. begin
  3.   if MainScreen.ActivePage = MainHome then
  4.     begin
  5.       MainLogo.Visible:=true;
  6.       Blurb.Visible := false;
  7.     end
  8.   else
  9.     MainLogo.Visible:=false;
  10.  
  11.   Blurb.Visible:=true;
  12. end;

So, you are always setting Blurb to Visible.

I think you meant to do:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.MainScreenChange(Sender: TObject);
  2. begin
  3.   if MainScreen.ActivePage = MainHome then
  4.     begin
  5.       MainLogo.Visible:=true;
  6.       Blurb.Visible := false;
  7.     end
  8.   else
  9.     begin
  10.       MainLogo.Visible:=false;
  11.       Blurb.Visible:=true;
  12.     end;
  13. end;

cheers
S.


Thanks!  I actually just didn't realise you had to do 'begin' and 'end' in a multi-line else statement.  Makes total sense but for some reason it never registered. %)
Ubuntu 18.04/Lazarus 2.0.10 FPC 3.2.0

heebiejeebies

  • Jr. Member
  • **
  • Posts: 51
Re: [Solved!] TPageControl: OnChange event not firing
« Reply #9 on: May 13, 2021, 11:18:57 am »
And you could further abbreviate it to:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.MainScreenChange(Sender: TObject);
  2. begin
  3.   MainLogo.Visible := MainScreen.ActivePage = MainHome;
  4.   Blurb.Visible := not MainLogo.Visible;
  5. end;
which leaves no room for mistakes. ;D

Ah, good one - thanks!
Ubuntu 18.04/Lazarus 2.0.10 FPC 3.2.0

heebiejeebies

  • Jr. Member
  • **
  • Posts: 51
Re: [Solved!] TPageControl: OnChange event not firing
« Reply #10 on: May 13, 2021, 11:37:40 am »
Okay one last thing about this trivial bit of code and then hopefully I can move on to something important!  :D

Google informs me that in Pascal you can combine two conditions in a single if statement by using or.

I've tried both
Code: Pascal  [Select][+][-]
  1.  if MainScreen.ActivePage = MainHome or Welcome then

and

Code: Pascal  [Select][+][-]
  1.  if MainScreen.ActivePage = MainHome or MainScreen.ActivePage = Welcome then

And in both cases Lazarus spits the dummy because "Error: Operator is not overloaded: "TTabSheet" or "TTabSheet".  You'd think something not being overloaded would be a good thing, surely??  %)
Ubuntu 18.04/Lazarus 2.0.10 FPC 3.2.0

wp

  • Hero Member
  • *****
  • Posts: 8359
Re: [Solved!] TPageControl: OnChange event not firing
« Reply #11 on: May 13, 2021, 11:40:42 am »
You must put the = part into parenthesis because "or" has higher priority:

Code: Pascal  [Select][+][-]
  1. if (MainScreen.ActivePage = MainHome) or (MainScreen.ActivePage = Welcome)  then
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

heebiejeebies

  • Jr. Member
  • **
  • Posts: 51
Re: [Solved!] TPageControl: OnChange event not firing
« Reply #12 on: May 13, 2021, 11:44:26 am »
Excellent, thanks again wp! :D
Ubuntu 18.04/Lazarus 2.0.10 FPC 3.2.0

 

TinyPortal © 2005-2018