[Solved!] TPageControl: OnChange event not firing lazarus / 61-Beginners ---------------------------------------------------------------------- heebiejeebies · 2021-05-12 10:00 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! ---------------------------------------------------------------------- wp · 2021-05-12 10:04 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_b y_code). ---------------------------------------------------------------------- heebiejeebies · 2021-05-12 10:23 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!!!! ---------------------------------------------------------------------- howardpc · 2021-05-12 10:34 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 · 2021-05-12 10:42 OK so now I've got another weird problem.  Here's the event code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.MainScreenChange(Sender: TObject);begin  if MainScreen.ActivePage = MainHome then  begin  MainLogo.Visible:=true;  Blurb.Visible := false;  end  else  MainLogo.Visible:=false;  Blurb.Visible:=true;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 ??? ---------------------------------------------------------------------- heebiejeebies · 2021-05-12 10:49 Never mind, I can get the same effect by just setting the Blurb's caption to blank.  Very weird though ?? ---------------------------------------------------------------------- speter · 2021-05-12 11:29 heebiejeebies, I think your lack of indenting is "hurting" you. Your code is:Quote from: heebiejeebies on May 12, 2021, 12:42:19 pmCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.MainScreenChange(Sender: TObject);begin  if MainScreen.ActivePage = MainHome then    begin      MainLogo.Visible:=true;      Blurb.Visible := false;    end  else    MainLogo.Visible:=false;   Blurb.Visible:=true;end; So, you are always setting Blurb to Visible. I think you meant to do: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.MainScreenChange(Sender: TObject);begin  if MainScreen.ActivePage = MainHome then    begin      MainLogo.Visible:=true;      Blurb.Visible := false;    end  else    begin      MainLogo.Visible:=false;      Blurb.Visible:=true;    end;end; cheers S. ---------------------------------------------------------------------- lucamar · 2021-05-12 12:08 And you could further abbreviate it to: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.MainScreenChange(Sender: TObject);begin  MainLogo.Visible := MainScreen.ActivePage = MainHome;  Blurb.Visible := not MainLogo.Visible;end;which leaves no room for mistakes. ---------------------------------------------------------------------- heebiejeebies · 2021-05-13 09:18 Quote from: speter on May 12, 2021, 01:29:27 pmheebiejeebies, I think your lack of indenting is "hurting" you. Your code is:Quote from: heebiejeebies on May 12, 2021, 12:42:19 pmCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.MainScreenChange(Sender: TObject);begin  if MainScreen.ActivePage = MainHome then    begin      MainLogo.Visible:=true;      Blurb.Visible := false;    end  else    MainLogo.Visible:=false;   Blurb.Visible:=true;end; So, you are always setting Blurb to Visible. I think you meant to do: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.MainScreenChange(Sender: TObject);begin  if MainScreen.ActivePage = MainHome then    begin      MainLogo.Visible:=true;      Blurb.Visible := false;    end  else    begin      MainLogo.Visible:=false;      Blurb.Visible:=true;    end;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. ---------------------------------------------------------------------- heebiejeebies · 2021-05-13 09:18 Quote from: lucamar on May 12, 2021, 02:08:32 pmAnd you could further abbreviate it to: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.MainScreenChange(Sender: TObject);begin  MainLogo.Visible := MainScreen.ActivePage = MainHome;  Blurb.Visible := not MainLogo.Visible;end;which leaves no room for mistakes. Ah, good one - thanks! ---------------------------------------------------------------------- heebiejeebies · 2021-05-13 09:37 Okay one last thing about this trivial bit of code and then hopefully I can move on to something important!  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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  if MainScreen.ActivePage = MainHome or Welcome then and Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  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?? ---------------------------------------------------------------------- wp · 2021-05-13 09:40 You must put the = part into parenthesis because "or" has higher priority: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i if (MainScreen.ActivePage = MainHome) or (MainScreen.ActivePage = Welcome)  then ---------------------------------------------------------------------- heebiejeebies · 2021-05-13 09:44 Excellent, thanks again wp!