Recent

Author Topic: Aligning a form with a button  (Read 442 times)

ironphil

  • Jr. Member
  • **
  • Posts: 50
Aligning a form with a button
« on: December 27, 2019, 04:05:23 pm »
I have a borderless form that I want to appear right under a button when I press it. So far I can only do it by adding numbers by trial and error. I supposed there should be reliable way to do it by only using the Left and Top properties of the different controls involved. Here is what I am doing right now:
I want frmPeriodic to be align verticaly with btnPeriodic and right under it. frmMain is the main form on which btnPeriodic is located. There is a panel (pnlTop) above the area where btnPeriodic is located.
Code: Pascal  [Select][+][-]
  1. procedure TfrmPeriodic.FormShow(Sender: TObject);
  2. begin
  3.     Left:= frmMain.Left + frmMain.btnPeriodic.Left;
  4.     Top:= frmMain.Top + frmMain.btnPeriodic.Top + frmMain.btnPeriodic.Height + frmMain.pnlTop.Height;
  5. end;    
  6.  

It is close but the frame is still too high and not enough to the right.

howardpc

  • Hero Member
  • *****
  • Posts: 3585
Re: Aligning a form with a button
« Reply #1 on: December 27, 2019, 06:23:13 pm »
You could try this, though it may not work well on all platforms:
Code: Pascal  [Select][+][-]
  1. uses LCLIntf, LCLType;
  2.  
  3. procedure TfrmPeriodic.FormShow(Sender: TObject);
  4. begin
  5.   SetInitialBounds(frmMain.Left + frmMain.btnPeriodic.Left,
  6.                    frmMain.Top + frmMain.pnlTop.Top + frmMain.pnlTop.Height +
  7.                      frmMain.btnPeriodic.Height + GetSystemMetrics(SM_CYCAPTION),
  8.                    Width,
  9.                    Height);
  10. end;


Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 6720
  • Debugger - SynEdit - and more
    • wiki
Re: Aligning a form with a button
« Reply #2 on: December 27, 2019, 06:29:33 pm »
Use ClientToScreen.

Code: Pascal  [Select][+][-]
  1. xy := ClientToScreen(Point(btn.Left, btn.Bottom));
  2. Form.Top := xy.y;
  3. Form.Left := xy.x;

 

TinyPortal © 2005-2018