TButton creation at Run Time - Anchor issue lazarus / 31-LCL ---------------------------------------------------------------------- Tony Stone · 2022-02-15 18:36 So I am using the following code to try and create buttons over the top of all text controls with the text 'pvt' in their name.  This works great and I like it but I had not realized there was an issue until i recently had it overlay buttons on edits where there was no Left anchoring.  So now when i resize my form the buttons only work good over the top of anything with a left anchor.  Am I missing something obvious? Lazarus 2.2.0 (rev lazarus_2_2_0-81-gbbac02ab05) FPC 3.2.3 x86_64-linux-gtk2 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 TfrmMain.procHiderMakeBtns;var  hider: array of TButton;  edtToHid: TComponent;  i: integer;  iPTVcount: integer = 0;begin  for i := 0 to frmMain.ComponentCount - 1 do  begin    if AnsiContainsText(frmMain.Components[i].Name, 'pvt') then    begin    edtToHid := FindComponent(frmMain.Components[i].Name);      if edtToHid is TLabeledEdit then      begin        SetLength(hider, iPTVcount + 1);        hider[iPTVcount] := TButton.Create(frmMain);      hider[iPTVcount].Name := frmMain.Components[i].Name + 'OTFhider';        hider[iPTVcount].Visible := True;        hider[iPTVcount].Parent :=          (FindComponent(frmMain.Components[i].Name) as TLabeledEdit).Parent;         hider[iPTVcount].Align :=          (FindComponent(frmMain.Components[i].Name) as TLabeledEdit).Align;       hider[iPTVcount].Anchors :=          (FindComponent(frmMain.Components[i].Name) as TLabeledEdit).Anchors;         hider[iPTVcount].BoundsRect :=          (FindComponent(frmMain.Components[i].Name) as TLabeledEdit).BoundsRect;         hider[iPTVcount].Caption := 'CLICK TO SHOW';         hider[iPTVcount].OnClick := @HiderButtonClick;      end;      Inc(iPTVcount, 1);    end;  end;end; ---------------------------------------------------------------------- Tony Stone · 2022-02-16 00:14 Ok... so that didn't work.  Maybe create another control for each edit so they can share that parent?  Seems like it would get messy.... 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 [Debugger Exception Notification] Project raised exception class 'EInvalidOperation' with message:Control of class 'TLabeledEdit' can't have control of class 'TButton' as a child  At address 554C00 ---------------------------------------------------------------------- Tony Stone · 2022-02-22 21:56 Actually I did some little hacks similar but you helped me out last time!  lol. Remember you helped be make the tcheckbox behave properly so it didn't execute the onchange when I changed it from code.  Something like that.  I am gonna mess with your example and see what I can come up with!  I have another problem maybe you can help me with...  Some what related to this. So here is my "problem".  I create these "hider buttons" buttons at run time.  They are just used to cover private keys.  SO... in my hider[iPTVcount].OnClick := @HiderButtonClick; procedure i make it hide the button for a few seconds then reappear. Here is the problem and here is my "hider 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.HiderButtonClick(Sender: TObject);var  tickStart: Integer;  btnHider: TButton;begin  btnHider:=Sender as TButton;  tickStart:=GetTickCount64;  //btnHider.Visible := False;  btnHider.SendToBack;  while GetTickCount64-tickStart<5000 do begin    Application.ProcessMessages;  end;  //btnHider.Visible := True;btnHider.BringToFront; end; My problem is this...  if i click a button it hides,  Great!  if I wait a second and click the next it hides.  Still good... wait another second or so and then click another and it hides.  All good. EXCEPT they all reappear at the same time!  lol  After giving it some thought today while at work I THINK i know what the problem is.  My tickStart integer is being reset FOR ALL onclick procedures.... I am experimenting now to see if i am correct.  But I dont know WHY.  Doesn't each onclick event get its own integer of tickStart in memory?  I think I uncovered something that is a possible good teaching moment for me but I am still not fully understanding my code failure. ---------------------------------------------------------------------- Tony Stone · 2022-02-22 22:39 HEY Jamie!     I get to teach the teacher a little something this time!  lol This fixed my anchoring issue. So many functions buried...  and some times it is staring you in the face.  Oh well... 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           h ider[iPTVcount].AnchorSame(akLeft,(FindComponent(Form1.Components[i].N ame) as TEdit));          hider[iPTVcount].AnchorSame(akRight,(FindCom ponent(Form1.Components[i].Name) as TEdit));          hider[iPTVcount] .BorderSpacing:=(FindComponent(Form1.Components[i].Name) as TEdit).BorderSpacing; Quote from: jamie on February 16, 2022, 01:40:10 amoh Well, I don't know what to tell you then.. LCL has some controls restricted.  sub class the control so you can paint a 3D looking button over the edit area when ever you have the control in READ only for example.  I suppose you haven't done any local hacks like that.  Let me see if I can come up with something quick here. ---------------------------------------------------------------------- bytebites · 2022-02-23 05:20 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 edtToHid := FindComponent(frmMain.Components[i].Name); I don't understand what is purpose of FindComponent-function here and onward. Is it same as: 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 edtToHid := frmMain.Components[i];and then later 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 hider[iPTVcount].Parent :=edtToHid ---------------------------------------------------------------------- Tony Stone · 2022-02-23 23:15 Ok, I THINK I cleaned things up and hopefully made more readable.  And as you pointed out I am not gonna keep calling FindComponent. I am still open to advice.  I always want to improve my code and do a better job.....  so thank you!    Below is my improved version. 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 TfrmMain.CreatePrivateMaskButtons;var  btnAsMask: array of TButton;  edtToHide: TLabeledEdit;  i: integer;  i2: integer = 0;begin  for i := 0 to frmMain.ComponentCount - 1 do  begin    if AnsiContainsText(frmMain.Components[i].Name, 'pvt') then    begin    if  frmMain.Components[i] is TLabeledEdit then      begin        edtToHide:=FindComponent(frmMain.Components[i].Name) as TLabeledEdit;        SetLength(btnAsMask, i2 + 1);        // Tried to clean up further using a With block here but with blocks never work when i reference an index in an array.... ?  Maybe doing it wrong?        //with btnAsMask[i2] do begin        btnAsMask[i2] := TButton.Create(frmMain);        btnAsMask[i2].Name := 'btnMaskFor' + frmMain.Components[i].Name;        btnAsMask[i2].Visible := True;      btnAsMask[i2].Parent := edtToHide.Parent; // Still need parent so it perfectly aligns over TLabelEdits in different panels.        btnAsMask[i2].Align := edtToHide.Align;        btnAsMask[i2].AnchorSame(akLeft,(edtToHide));        btnAsMask[i2].AnchorSame(akRight,(edtToHide));        btnAsMask[i2].BorderSpacing:=(edtToHide).BorderSpacing;        btnAsMask[i2].BoundsRect := (edtToHide).BoundsRect;        btnAsMask[i2].Caption := 'CLICK TO SHOW';        btnAsMask[i2].OnClick := @HiderButtonClick;      end;      Inc(i2, 1);    end;  end;end; Quote from: bytebites on February 23, 2022, 06:20:19 amCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i edtToHid := FindComponent(frmMain.Components[i].Name); I don't understand what is purpose of FindComponent-function here and onward. Is it same as: 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 edtToHid := frmMain.Components[i];and then later 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 hider[iPTVcount].Parent :=edtToHid ---------------------------------------------------------------------- Tony Stone · 2022-02-24 07:08 Ok, I took one last look before I go to bed.  This is the final product here.  I don't think it can possibly be made better.  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 TfrmMain.CreatePrivateMaskButtons;var  btnAsMask: array of TButton;  edtToHide: TLabeledEdit;  i, i2: integer;begin  for i := 0 to frmMain.ComponentCount - 1 do  begin    if (frmMain.Components[i] is TLabeledEdit) and (AnsiContainsText(frmMain.Components[i].Name, 'edtpvt')) then    begin      edtToHide:=frmMain.Components[i] as TLabeledEdit;      i2:=Length(btnAsMask);      SetLength(btnAsMask, i2+1);      btnAsMask[i2] := TButton.Create(frmMain);      btnAsMask[i2].Parent := edtToHide.Parent;      btnAsMask[i2].AnchorSame(akLeft,edtToHide);      btnAsMask[i2].AnchorSame(akRight,edtToHide);      btnAsMask[i2].BorderSpacing:=edtToHide.BorderSpacing;      btnAsMask[i2].BoundsRect := edtToHide.BoundsRect;      btnAsMask[i2].Caption := 'CLICK TO SHOW';      btnAsMask[i2].OnClick := @btnMaskForPvtClick;    end;  end;end;